Matplotlib is a plotting library
Installing matplotlib
pip install matplotlib
Importing matplotlib
import matplotlib
import sub module pyplot
import matplotlib.pyplot as plt
plot simple graph
import matplotlib.pyplot as plt import numpy as np x=np.array([1,2,3]) y=np.array([10,20,30]) plt.plot(x, y) plt.show()
Markers
plt.plot(x, y, marker="*")
Marker size
plt.plot(x, y, marker="*", ms=10)
Marker Edge Color (mec)
plt.plot(x, y, marker="*", ms=10, mec='r')
Marker face color (mfc)
plt.plot(x, y, marker="*", ms=10, mec='r', mfc='g')
Format string
'MarkerLineColor' plt.plot(x, y, '*-r')
List of Line reference
- | Solid line |
: | Dotted line |
-. | Dashed/Dotted line |
-- | Dashed line |
List of color reference
r | Red |
g | Green |
b | Blue |
w | White |
k | Black |
y | Yellow |
Labels
plt.xlabel() plt.ylabel()
Title
plt.title()
Title with location
plt.title('title', loc='right')
Grid lines
plt.grid()
Grid in x-axis
plt.grid(axis='x')
Create vertical bar
plt.bar(x, y)
Create horizontal bar
plt.barh(x, y)
Bar with color
plt.bar(x, y, color='r')
Pie chart
plt.pie(items)
Label, shadow, color for pie chart
plt.pie(items, labels=custom_labels, colors=custom_colors, shadow=True)
Legend with header
plt.legend(title='Heading')