open-source
library and is free to use for all types of metadata.Matplotlib
library is mostly used in Python, however, a few segments are written in C, C++, and JavaScript.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 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
plot()
function is used to draw points(markers
) on the graph.plot
function draws a line from one point to the another.parameters
for specifying points in the graph.#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()
In order to plot a graph without a line:
#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()
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.