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):
Set x1,x2 value (4,9) and y1,y2 value (6,16):
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):
Check out the example diagram below, where two locations are shown at coordinates (25, 7) and (7, 7):
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):
Implement the above example without a line diagram:
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:
Utilize the above example and draw the diagram with a ‘ring’:
In the example above, the x-points are [0, 1, 2, 3, 4].
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:
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)
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:
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)):
We can also save your Matplotlib plots to a file using the savefig function.
plt.plot(x, y) plt.savefig('mrx_mplot.png')
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.