Matplotlib: 相同高度的Broken_barh图

0 投票
1 回答
1210 浏览
提问于 2025-04-17 14:33

我正在使用broken_barh图表。有没有办法让单个broken_barh的高度固定?这样图像在垂直方向上会变大,但比例应该保持不变。

这里有一个简单的例子。

import matplotlib.pyplot as plt
import matplotlib as mlp

fig = plt.figure()
ax = fig.add_subplot(111)

broken_barh(self, xranges, yrange, **kwargs)
ax.broken_barh([(110, 30), (150, 10)], (0, 10), facecolors='blue')
ax.broken_barh([(10, 50), (100, 20), (130, 10)] , (10, 10),
               facecolors=('red', 'yellow', 'green'))
ax.broken_barh([(50, 30), (85, 10)], (20, 10), facecolors='black')

ax.set_xlim(0,200)
ax.set_xlabel('seconds since start')
ax.set_yticks([0,10,20])
ax.set_yticklabels(['Bill', 'Jim', 'Jeff'])
ax.grid(True)

plt.savefig('broken_barh_example.png', bbox_inches='tight') 
plt.show()

如果我生成两个图,一个有两个broken_barh,另一个有三个,它们看起来是这样的:

有2个broken_barh http://imageshack.us/a/img195/747/brokenbarhexample2.png

有3个broken_barh http://img341.imageshack.us/img341/5650/brokenbarhexamplenoyran.png

1 个回答

1

这个渲染会把所有内容都放进可用的空间里。如果你想让图形的大小随着你添加更多行而变大,可以手动设置,方法是

 fig.set_size_inches(w, h * num_rows, forward=True)

这样可以强制设置一个固定的条形高度。

(文档)

撰写回答