3D Plotting in Matplotlib-Basic Line & Scatter Plot #Pt1

in utopian-io •  7 years ago  (edited)

What Will I Learn?

  • Three-Dimensional Plotting in Matplotlib
  • Create 3D Line Plot
  • Create 3D Scatter Plot

Requirements

  • Python3 or Python2
  • Matplotlib library for Python
  • Idle
  • Python Intermediate

Difficulty

  • Intermediate

Tutorial Contents

In this tutorial we'll use Matplotlib packages already installed

what is Matplotlib
Matplotlib is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. Matplotlib can be used in Python scripts, the Python and IPython shells, the Jupyter notebook, web application servers, and four graphical user interface toolkits.

Three-Dimensional Plotting in Matplotlib

In this tutorial, we'll use the following import statements. These abbreviations are semi-standardized, and most tutorials, other scientific python code that you'll find elsewhere will use them as well

from mpl_toolkits.mplot3d import Axes3d
import matplotlib.pyplot as plt

Three-dimensional plots are enabled by importing the mplot3d toolkit, included with the main Matplotlib installation.

fig = plt.figure()
ax = plt.axes(projection='3d')
plt.show()

Once this submodule is imported, a three-dimensional axes can be created by passing the keyword projection='3d' to any of the normal axes creation routines

The line plt.show() says indeed that you want to see the plot. If you execute this line, you’ll see a window popping up. And you’ll see if it looks like what you had in your mind!

3D Plot.JPG

With this three-dimensional axes enabled, we can now plot a variety of three-dimensional plot types.

Basic 3D Line

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

We want to define our figure and define the axes, and so far that's not new and same thing with subplot those same rules apply this is 1 1 1, now what will be new is you're going to have this new parameter called projection, and projection is our way of telling matplotlib that we want to have this be a three - dimensional projection, so ax it's creating the axes in this subplot it's doing so with a three dimensional object in mind.

  • plt.figure() : create a figure
  • add_subplot() : this function use to create axes
  • parameter 1;111 : 111 is equal to 1,1,1, which means that you actually give three arguments to add_subplot(). The three arguments designate the number of rows (1), the number of columns (1) and the plot number (1). So you actually make one subplot
  • parameter2;projection=’3d’ : create three – dimensional projection
X, Y ,Z = [1,2,3,4,5,6,7,8,9,10], [5,6,3,13,4,1,2,4,8,10], [2,3,3,3,5,7,9,11,9,10]

we want to specify the plots that we want to make, so in this case we work we're going to need three right we've got an X a Y and a Z and X Y Z is going to be equal to the plots for three different arrays.

and they have to obviously equal the same right you need 10 X is 10 Y is 10 Z's and so on. You can make up whatever plots you want by the way I'm just going to make some random ones real quick so you should do the same thing you won't have to copy me or anything.

ax.plot_wireframe(X, Y, Z)
plt.show()

That's what we're plotting is this wireframe and then in here you put what you want to plot and our case we want to plot XYZ, and then finally end with a plot.show().

Basic 3D Line.JPG

3D Scatter Plot

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

that you import the pyplot module of the matplotlib library under the alias plt.

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

X = [1,2,3,4,5,6,7,8,9,10]
Y = [5,6,2,3,13,4,1,2,4,8]
Z = [2,3,3,3,5,7,9,11,9,10]

ax.scatter(X, Y, Z, c='r', marker='o')

c for color and we're going to say the color is going to be red, and then now we have marker and this there's all kinds of different markers and in this case we're going to use the just a zero for like a circular marker.

ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.set_zlabel('z axis')
plt.show()

you can also set labels, and within the parameters contained x axis, y axis, z axis.

To change your plot title and axes labels, you can follow one of the following approaches, depending of which container of which you want to make use:
The easiest way to set these things right is by using :

  • ax.set(title="some title", xlabel="x", ylabel="y", zlabel='z') or ax.set_xlim(), ax.set_ylim(), ax.set_zlim() or ax.set_title().
  • If you want to work with the figure, you might also resort to fig.suptitle() to add a title to your plot.
  • If you’re making use of the default settings that the package has to offer, you might want to use plt.title(), plt.xlabel(), plt.ylabel().
  • Define your own style sheet or change the default matplotlibrc settings.

Capture.JPG

here we go and we have our plot, now you can see that instead of like lines they're you know scatter plots on the chart and the red in circles and beautiful, we also have our x axis or y axis and our z axis labeled here for us.

And the other thing you'll note that I think is pretty impressive again it took quite a bit of coding is this visually it's very hard for you to tell which plot is closer.



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Greetings @alucard14,

Unfortunately, your contribution has been rejected due to the fact that it is a lot shorter, less informative than the other tutorial contributions that we have. As I compare yours with them, I can see that the overall concept is already something that an intermediate user should be able to do in the project. Henceforth, I am unable to approve your contribution. You can check already approved Tutorial contributions on our platform to get a glimpse on what kind of tutorials are accepted.

Thank you.


Need help? Write a ticket on https://support.utopian.io.
Chat with us on Discord.

[utopian-moderator]