import matplotlib.pyplot as plt
import numpy as np
ax = plt.subplot(111)
ax.set_xlim(-0.2, 3.2)
ax.grid(b=True, which='major', color='k', linestyle=':', lw=.5, zorder=1)
# x,y data
x = np.arange(4)
y = np.array([5, 12, 3, 7])
# Define upper y limit leaving space for the text above the bars.
up = max(y) * .03
ax.set_ylim(0, max(y) + 3 * up)
ax.bar(x, y, align='center', width=0.2, color='g', zorder=4)
# Add text to bars
for xi, yi, l in zip(*[x, y, list(map(str, y))]):
ax.text(xi - len(l) * .02, yi + up, l,
bbox=dict(facecolor='w', edgecolor='w', alpha=.5))
ax.set_xticks(x)
ax.set_xticklabels(['text1', 'text2', 'text3', 'text4'])
ax.tick_params(axis='x', which='major', labelsize=12)
plt.show()
没有直接的方法来注释条形图(据我所知),前段时间我需要注释一个,所以我写了这篇文章,也许你可以根据你的需要调整它。![enter image description here](https://i.stack.imgur.com/O5fav.png)
您可以使用较小的条形图作为目标和基准指标。Pandas不能自动为条添加注释,但是您可以简单地循环这些值并使用matplotlib的
pyplot.annotate
。在相关问题 更多 >
编程相关推荐