Visualizing Data in Python Using Matplotlib

120 VIEWS

·

Matplotlib is a graphical library for plotting mathematical functions and data in Python. The library is capable of producing a wide range of plots; however, this article will focus on generating 2D and 3D plots, as this is a common use case.

Installing the prerequisites

In addition to matplotlib, we will be using NumPy in this tutorial. You might be wondering, “Why not use Python’s built-in data structures?” We can’t use it for complex examples because the built-in data structures don’t have all the functions we need. (More on this later.)

Matplotlib can easily be installed on Python 2 and 3 with the following command:

Please ensure you reference your desired Python version to avoid any surprises. (You can get more help from the official website if this does not work for you.)

If you don’t have NumPy installed already, you can follow these instructions to install it for your operating system.

Hello World in matplotlib

Now that you have successfully installed matplotlib, let’s jump into a simple example to give us a feel for how the library works. First, copy and paste the code below in your IDE and run it.

You should see a new window like the one below with a graph of a straight line. Note that the graph will appear in the window of your IDE if you are using PyCharm with scientific mode activated.

Note that the x and y axis have the names we provided in the xlabel and ylabel methods of matplotlib. You need to call plt.show() if you want to see the graph after running the code.

Plotting multiple functions on one graph

We now have an idea of how matplotlib works, so let’s move on to a more complex and practical use case: plotting multiple functions in the same graph. Note that we will be using NumPy from now on.

The code below plots two quadratic functions on the same graph as seen in the image that follows.


If you are not familiar with NumPy, you might be surprised by the simplicity of the mathematical expressions in the code. Let’s try doing the same thing with Python’s lists using the code below.

Not only is the code more complex, but matplotlib also produces an error if you run the code. This is because matplotlib attempts to perform operations that lists do not support. As a rule of thumb, you should avoid using lists in mathematically intensive situations (or just don’t use them when plotting).

You may have observed that matplotlib was smart enough to use different colors for each function. You can control this behavior by providing special options to the plot method, like so:

The extra option requires a character representing a color and another character that represents the symbol that would be plotted at each point on the function. This is particularly useful for scatter plots where the data may not be continuous. Replacing the calls to plot in the previous example with this code produces a graph like that shown below.

3D plots

Finally, I will show an example of creating a three-dimensional plot using a helix. A helix is simply the spiral shape formed by a spring. To make a 3D plot, we need to create a three-dimensional axis and supply it with data for the x, y and z axis. The code below plots a helix.

Similar to 2D plots, you can make 3D scatter plots by replacing ax.plot(x, y, z, zdir=’z’, lw=2) with ax.scatter(x, y, z, zdir=’z’, lw=2, c=’g’, marker=’^’). The images below show the results of the plot and scatter methods respectively.

As you might expect, you can rotate the 3D graph by clicking and dragging on the plot.

Resources

https://matplotlib.org/users/pyplot_tutorial.html
https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html

Do you think you can beat this Sweet post?

If so, you may have what it takes to become a Sweetcode contributor... Learn More.

Stephan is a third-year student at Ashesi pursuing an undergraduate degree in computer engineering. Stephan is passionate about technology and is focused on continuous learning to gain new skills. He has worked in software development, and has worked on IoT projects and designing products for the health sector.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu