import matplotlib.pyplot as plt
import numpy as np
并列的柱状图
labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
x = np.arange(len(labels)) # x locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, men_means, width, label='Men')
rects2 = ax.bar(x + width/2, women_means, width, label='Women')
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()
def autolabel(rects):
"""Attach a text label above each bar in *rects*, displaying its height."""
for rect in rects:
height = rect.get_height()
ax.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom')
autolabel(rects1)
autolabel(rects2)
fig.tight_layout()
# plt.show()
plt.savefig('output.jpg')
双Y轴曲线图
https://www.cnblogs.com/atanisi/p/8530693.html
x = np.arange(0., np.e, 0.01)
y1 = np.exp(-x)
y2 = np.log(x)
fig = plt.figure()
ax1 = fig.add_subplot()
l1=ax1.plot(x, y1, label='l1') # line
ax1.set_ylabel('Y values for exp(-x)')
ax2 = ax1.twinx() # share x axis
# 之后的公共操作,可以只在ax1上进行
l2=ax2.plot(x, y2, 'r', label='l1') # line
# ax2.set_xlim([0, np.e])
ax2.set_ylabel('Y values for ln(x)')
# 公共操作,在ax1上进行
ax1.set_title("Double Y axis")
ax1.set_xlabel('Same X for both exp(-x) and ln(x)')
# 公共图例
ls=l1+l2
labs = [l.get_label() for l in ls]
ax1.legend(lns, labs, loc=0)
plt.show()
小功能
设置坐标轴显示范围
ax2.set_xlim([0, np.e])