1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| def bar_advanced_plot(): """ bar advanced plot """ # 生成测试数据 means_men = np.array((20, 35, 30, 35, 27, 25, 32, 34, 20, 25)) means_women = np.array((25, 32, 34, 20, 25, 20, 35, 30, 35, 27))
# 设置标题 plt.title("高级柱状图", fontproperties=myfont)
# 设置相关参数 index = np.arange(len(means_men)) bar_width = 0.8
# 画柱状图(两种:X轴以上/X轴以下) plt.bar(index, means_men, width=bar_width, alpha=0.4, color="b", label="Men") plt.bar(index, -means_women, width=bar_width, alpha=0.4, color="r", label="Women")
# 画折线图(两种,和柱状图对应) plt.plot(index, means_men, marker="o", linestyle="-", color="r", label="Men line") plt.plot(index, -means_women, marker=".", linestyle="--", color="b", label="Women line")
# 设置图形标示(两种,和柱状图对应) for x, y in zip(index, means_men): plt.text(x, y+1, y, ha="center", va="bottom") for x, y in zip(index, means_women): plt.text(x, -y-1, y, ha="center", va="top")
# 设置Y轴和图例位置 plt.ylim(-45, 80) plt.legend(loc="upper left", shadow=True)
# 图形显示 plt.show() return
|