Installation
pip install matplotlib
Usage
Matplotlib is the core library for plotting graphs. See full documentation.
import matplotlib.pyplot as plt
Examples
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y, color='red', linewidth=2)
ax.scatter([2, 4, 6], [3, 9, 16], color='blue', marker='^')
ax.set_xlim(0, 6.5)
plt.savefig('test.png')
plt.show()
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
plt.plot(x, y, color='orange', linewidth=1)
plt.show()
Prepare
x = np.linspace(0, 10, 50)
y = np.sin(x)
Create
fig = plt.figure()
fig.add_axes()
# row-col-num (2-2-1)
ax = fig.add_subplot(221)
fig2, ax2 = plt.subplots(nrows=2, ncols=2)
Customize
Markers
plt.plot(x, y, marker='o')
Linestyles
plt.plot(x, y, ls='solid')
plt.plot(x, y, ls='--')
plt.plot(x, y, ls='solid', x**2, y**2, ls='--'))
Text
# location (x,y)
plt.text(1,1,'Function')
plt.annotate(
'Local Max',
xy=(3.3, 1),
xytext=(3, 1.8),
arrowprops=dict(facecolor='green', shrink=0.05),
)
Legends
plt.title('Title')
plt.legend(loc='max')
plt.legend(loc='upper left')
ax.set(title='Axes', xlabel='x', ylabel='y')
Limits
ax.axis('equal')
ax.margins(x=0.0, y=0.1)
ax.set(xlim=[0,1], ylim=[0,10])
ax.set_xlim([0,1])
Ticks
ax.xaxis.set(
ticks=range(1, 10),
ticklabels=[1, 'foo', -10, 'bar'],
)