Python--Matplotlib(基本用法)
- 实例化
import matplotlib.pyplot as plt
fig = plt.figure()
- 子图(类似MATLAB)
ax = fig.add_subplot(221)
- 设置图参数
ax.set(xlim=[0.5, 4.5], ylim=[-2, 8], title='An Example Axes',
ylabel='Y-Axis', xlabel='X-Axis')
- 多子图一次性设置
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')
- 实例化子图,并添加图
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(224)
- 折线
x = np.linspace(0, np.pi) y_sin = np.sin(x) y_cos = np.cos(x)
ax1.plot(x, y_sin)
ax2.plot(x, y_sin, 'go--', linewidth=2, markersize=12)
ax3.plot(x, y_cos, color='red', marker='+', linestyle='dashed')
- 散点图
plt.scatter(x, y, color='red', marker='+')
- 条形图(垂直或水平)
fig, axes = plt.subplots(ncols=2, figsize=plt.figaspect(1./2))
#垂直
vert_bars = axes[0].bar(x, y, color='lightblue', align='center')
#水平
horiz_bars = axes[1].barh(x, y, color='lightblue', align='center')
axes[0].axhline(0, color='gray', linewidth=2)
axes[1].axvline(0, color='gray', linewidth=2)
- 直方图
ax1.hist(x, n_bins, density=True, histtype='barstacked')
ax1.set_title('stacked bar')
- 饼图
ax2.pie(sizes, autopct='%1.2f%%', shadow=True, startangle=90, explode=explode,
pctdistance=1.12)
ax2.axis('equal')
ax2.legend(labels=labels, loc='upper right')
- 箱型图
ax1.boxplot(data)
ax2.boxplot(data2, vert=False) #控制方向
- 泡泡图
area = (30 * np.random.rand(N))**2 # 0 to 15 point radii
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
- 等高线
ax1.contourf(x, y, z)
我的个人博客fangzengye.com, 欢迎来撩哦!
原文博主: 热衷开源的宝藏Boy
版权声明: 自由转载-非商用-禁止演绎-保持署名| CC BY-NC-ND 3.0
浙公网安备 33010602011771号