直接绘图
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4], [5, 14, 2, 39], color='red', linewidth=10)
plt.xlim(0.5, 4.5)
plt.show()
单图框架
import matplotlib.pyplot as plt
fig = plt.figure() #配置要素
ax = fig.add_subplot(111) #添加表,第一行第一列第一个位置
ax.set(xlim=[0, 9], ylim=[-4, 4], title='first',
ylabel='Y', xlabel='X')
plt.show()
多图框架
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=2, ncols=2)
axes[0,0].set(title='Upper Left')
axes[0,1].set(title='Upper Right')
axes[1,0].set(title='Lower Left')
axes[1,1].set(title='Lower Right')
plt.show()
饼图
import matplotlib.pyplot as plt
labels = 'work', 'sleep', 'study', 'other'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0)
fig1, (ax1, ax2) = plt.subplots(2)
ax1.pie(sizes, labels=labels, autopct='%1.1f%%', shadow=True) #格式化百分比输出
ax1.axis('equal')
ax2.pie(sizes, autopct='%1.2f%%', shadow=True, startangle=90, explode=explode,
pctdistance=0.8) #百分比距离圆心距离
ax2.axis('equal')
ax2.legend(labels=labels, loc='upper right')
plt.show()
图注释
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4], [10, 20, 25, 30], label='energy')
ax.plot([1, 2, 3, 4], [30, 23, 13, 4], label='oil')
ax.scatter([1, 2, 3, 4], [12, 20, 22, 17], label='Point')
ax.set(ylabel='Mount', xlabel='Time', title='sale for two teams')
ax.legend()
plt.show()
问题参考:
Python--Matplotlib(基本用法)_Gingkens的博客-CSDN博客_matplotlib
Python绘图问题:Matplotlib中%matplotlib inline是什么、如何使用?_LthID的博客-CSDN博客_%matplotlib inline