How to customize matplotlib plots in Python
Introduction
- Matplotlib is a graph-plotting, Python library that acts as a visualization library for the data.
- It is an
open-sourcelibrary and is free to use for all types of metadata. Matplotliblibrary is mostly used in Python, however, a few segments are written in C, C++, and JavaScript.
Installation of the Matplotlib Library
If you have python and pip! installed on your desktop, then you can install the Matplotlib library with ease.
Install the library using the command:
pip install matplotlib
Once matplotlib is installed, you can import it in your executed or compiler using the following command:
import matplotlib
Now, matplotlib is imported and is ready to use.
To check the version of your matplotlib library, use the following command:
print(matplotlib.__version__)
Pyplot
Pyplot is a sub-module of matplotlib that is imported simultaneously with matplotlib. Usually, it is imported as plt:
import matplotlib.pyplot as plt
Now ,the pyplot package can be referred as plt.
Example
Let’s plot a graph using the matplotlib and pyplot package:
import matplotlibimport matplotlib.pyplot as pltimport numpy as npxpoints=np.array([0,5])ypoints=np.array([0,200])plt.plot(xpoints,ypoints)plt.show
Plotting on the graph
- The
plot()function is used to draw points(markers) on the graph. - By default, the
plotfunction draws a line from one point to the another. - This function takes
parametersfor specifying points in the graph. - Two parameters are required, i.e., two arrays consisting of points for x-axis and y-axis.
#Positioning (2,4) and (10,10) on the graphimport matplotlib.pyplot as pltimport numpy as npxpoints = np.array([2, 10])ypoints = np.array([4, 10])plt.plot(xpoints, ypoints)plt.show()
Plotting without line
In order to plot a graph without a line:
- You only plot the points, also known as markers, on the plane.
- This can be done using
(also notated as “o”).string rotation parameters they are basically dots
#plotting two points on graph without joining#let us take any two random points on the plane say (2,5) and (9,9)import matplotlib.pyplot as pltimport numpy as npxpoints = np.array([2, 9])ypoints = np.array([5, 9])plt.plot(xpoints, ypoints, 'o')plt.show()
Various types of markers
| Marker | Description |
|---|---|
| o | Dots |
| d | Diamonds |
| * | Stars |
| s | Squares |
| p | Pentagon |
| v | Triangles |
| , | Pixels |
| . | Points |
| h | Hexagon |
These are some of the markers used. There are many more though.
#marking points on y-axis using the marker star.import matplotlib.pyplot as pltimport numpy as npypoints = np.array([2, 4, 1, 8])plt.plot(ypoints, marker = '*')plt.show()
Marker size
You can also adjust the size of the markers with the ms command.
Let’s use the same example from above:
#marking points on y-axis using the marker star.import matplotlib.pyplot as pltimport numpy as npypoints = np.array([2, 4, 1, 8])plt.plot(ypoints, marker = '*',ms = 20)plt.show()
Similarly, you can also change the marker outline and inside color using the mec and mfc commands.