一、柱状图(bar/barh)
改变柱子属性
每个柱子都是一个 rectangle 对象,其上包含很多对象,包含设置可见度,边框,hatch(图文样式)等等属性。
柱子添加高度值
其实原生函数没有这个方法,只能通过 plt.text 或者 plt.annotate 方式进行。
import matplotlibimport matplotlib.pyplot as pltimport numpy as nplabels = ['G1', 'G2', 'G3', 'G4', 'G5']men_means = [20, 34, 30, 35, 27]women_means = [25, 32, 34, 20, 25]x = np.arange(len(labels)) # the label locationswidth = 0.35 # the width of the barsfig, 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')# Add some text for labels, title and custom x-axis tick labels, etc.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 offsettextcoords="offset points",ha='center', va='bottom')autolabel(rects1)autolabel(rects2)fig.tight_layout()plt.show()

