Getting Started with Matplotlib Plotting

The purpose of this article is to give you basic to advance understanding of Matplotlib Plotting and how to implement it in order to generate various types of plots.

Before we dive into the details of Matplotlib plotting, you need to make sure that it is installed on our system. If not than follow steps mentioned in article for installing matplotlib.



Matplotlib Plotting X-Y

The plot() function of the Matplotlib package is utilized to display points (markers) in a diagram according to Matplotlib plotting.

In the plot() function, a line is plotted from one point to another by default.

It is possible to indicate points in a diagram by providing parameters to the function.

It is assumed that parameter 1 is an array representing the points along the x-axis.

There is another parameter, parameter 2, which holds an array of points on the y-axis.

The plot function requires two arrays [7, 64] and [125, 217] to be provided to it if we want to plot a line from (7, 125) to (64, 217).

Make a line in a diagram from point (7, 125) to point (64, 217):

Example: 

import matplotlib.pyplot as pt import numpy as npy x_plane = npy.array([7, 64]) y_plane = npy.array([125, 217]) pt.plot(x_plane, y_plane) pt.show()

Set x1,x2 value (4,9) and y1,y2 value (6,16):

Example: 

import numpy as npy import matplotlib.pyplot as pt x_plane = npy.array([4,9]) y_plane = npy.array([6,16]) pt.plot(x_plane, y_plane) pt.show()

Horizontal axes are represented by the x-axis.

A vertical axis is represented by the y-axis.


Plotting Without Line

You can visualize only the markers by utilizing the shortcut string notation parameter ‘o’, which represents ‘rings’.

In the following example diagram, sketch two points, one at coordinates (3, 3) and one at coordinates (3, 20):

Example: 

import numpy as npy import matplotlib.pyplot as pt x_plane = npy.array([3,3]) y_plane = npy.array([3,20]) pt.plot(x_plane, y_plane, 'o') pt.show()

Check out the example diagram below, where two locations are shown at coordinates (25, 7) and (7, 7):

Example: 

import numpy as npy import matplotlib.pyplot as pt x_plane = npy.array([25,7]) y_plane = npy.array([7,7]) pt.plot(x_plane, y_plane, 'o') pt.show()

Multiple Points

In Matplotlib plotting, you can display as many points as you want. Just ensure that they are all on the identical axis.

Make a diagram by connecting (25, 18) with (38, 44), then (15, 53), and lastly (31, 22):

Example: 

import numpy as npy import matplotlib.pyplot as pt x_plane = npy.array([25,38,15, 31]) y_plane = npy.array([18,44,53,22]) pt.plot(x_plane, y_plane) pt.show()

Implement the above example without a line diagram:

Example: 

import numpy as npy import matplotlib.pyplot as pt x_plane = npy.array([25,38,15,31]) y_plane = npy.array([18,44,53,22]) pt.plot(x_plane, y_plane, 'o') pt.show()

Default X-Points

When plotting with Matplotlib, if we do not indicate the points on the x-axis, they will display the default values 0, 1, 2, 3, or anything else that depends on the length of the y-points.

Taking the new example, but excluding the x-points, we produce the following diagram:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([16, 84, 57, 63, 93]) pt.plot(y_plane) pt.show()

Utilize the above example and draw the diagram with a ‘ring’:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([16, 84, 57, 63, 93]) pt.plot(y_plane, 'o') pt.show()

In the example above, the x-points are [0, 1, 2, 3, 4].

Example Explanation

The above example is using the Python libraries numpy and matplotlib to create a simple line plot.

First, an array y_plane is created using numpy.array() function which contains the values [16, 84, 57, 63, 93].

Next, matplotlib.pyplot is imported as pt and the plot() function is used to create a line plot of y_plane. The o argument specifies that each data point in the plot should be marked with a circle.

Finally, show() function is called to display the plot. The resulting plot will have the y-axis values on the vertical axis and the x-axis values on the horizontal axis (which is not specified, so it defaults to the indices of the array starting from 0).


Advanced Matplotlib Plotting

Matplotlib provides advanced plotting options that allow you to create complex plots with multiple subplots, color maps, and 3D visualizations.

Let’s explore some of these methods:

Subplots

We can create multiple subplots in a single figure by using the subplot function.

This will create a figure with four subplots, each displaying a different type of plot:

plt.subplot(2, 2, 1)
plt.plot(x, y)

plt.subplot(2, 2, 2)
plt.scatter(x, y)

plt.subplot(2, 2, 3)
plt.bar(x, y)

plt.subplot(2, 2, 4)
plt.hist(y)

Color Maps

We can use color maps to create plots that represent data using colors.

Matplotlib provides a variety of color maps, such as viridis, plasma, and inferno.

We can use the colormap function to set the color map for our plot.

Lets create a scatter plot where the color of each point represents the value of the corresponding y-coordinate:

Example: 

import numpy as npx = np.linspace(0, 10, 100) y = np.sin(x)plt.scatter(x, y, c=y, cmap='viridis') plt.colorbar()

3D Visualizations

Matplotlib can also create 3D visualizations of data. We can use the mplot3d toolkit to create 3D plots.

Now lets create a 3D surface plot of the function z = sin(sqrt(x^2 + y^2)):

Example: 

from mpl_toolkits import mplot3dfig = plt.figure() ax = plt.axes(projection='3d')x = np.linspace(-5, 5, 100) y = np.linspace(-5, 5, 100) X, Y = np.meshgrid(x, y) Z = np.sin(np.sqrt(X**2 + Y**2))ax.plot_surface(X, Y, Z, cmap='viridis') ax.set_title('3D Surface Plot')

We can also save your Matplotlib plots to a file using the savefig function.

plt.plot(x, y)
plt.savefig('mrx_mplot.png')
Note: This will save the plot to a file named mrx_mplot.png in the current directory.

Matplotlib Plotting Benefits

  • Matplotlib Plotting is a data visualization library for Python that offers many options for creating high-quality visualizations that are ready for publication.
  • It has a simple and user-friendly interface that allows users to create a wide variety of plots, from simple line and scatter plots to more complex visualizations such as heatmaps and 3D plots.
  • It provides extensive customization options that allow users to adjust various properties of the plot, including style, colors, labels, and more, to fit their needs.
  • It is compatible with various data formats and scientific computing tools, including NumPy arrays and Pandas dataframes.
  • It can be used with interactive libraries like IPython and Jupyter Notebook to create interactive visualizations that respond to user input.
  • Matplotlib produces high-quality plots suitable for use in scientific publications and presentations.
  • In matplotlib plotting we have a wide range of plot types, including line plots, scatter plots, bar charts, histograms, pie charts, and more.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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