我正在使用Matplotlib绘制一个条形图。我的大部分数据值在-10到+30之间。然而,我有两个数据值大约是-300。
当我绘制我的数据时,-300数据值栏看起来太大了,它隐藏了其他条形图的洞察力。我是否可以在-10到+30的范围内绘制所有的条形图,将-300横条剪辑到-30,然后写上"-300“的标签?
发布于 2016-07-01 13:11:56
使用ax.set_ylim()设置y限制,使用ax.annotate编写标签(如果您愿意,还可以使用箭头)。
例如:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1)
y = [-5, 10, 25, -10, 30, -300, 20, 30, -10, -300, 0, 4]
x = range(len(y))
ax.bar(x, y, width=1, alpha=0.5)
ymin, ymax = -15, 35
ax.set_ylim(ymin, ymax)
for xbar,ybar in zip(x,y):
if ybar < ymin:
ax.annotate(
ybar,
xy=(xbar+0.5, -14),
xytext=(xbar+0.5, -8),
rotation=90, ha='center', va='center',
arrowprops=dict(arrowstyle="->"))
plt.show()

https://stackoverflow.com/questions/38145763
复制相似问题