Matplotlib绘制Broken Barh图表

2020-08-07  本文已影响0人  鹿呀鹿呀快开门

代码如下所示,在其中进行注释

import matplotlib.pyplot as plt

# 设置中文显示
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']

fig, ax = plt.subplots()

```
 ax.broken_barh参数:
- [(110, 30), (150, 10)],意思是两个Barh分别为(开始x坐标110,长度30)和(开始x坐标150,长度10)
- (10, 9),表示Barh的开始y坐标为10,高度为9
```
ax.broken_barh([(110, 30), (150, 10)], (10, 9), facecolors='tab:blue')
ax.broken_barh([(10, 50), (100, 20), (130, 10)], (20, 9),
               facecolors=('tab:orange', 'tab:green', 'tab:red'))
ax.set_ylim(0, 35)  # 设置y轴的范围
ax.set_xlim(0, 200)  # 设置x轴的范围
ax.set_xlabel('seconds since start')
ax.set_yticks([15, 25])
ax.set_yticklabels(['Bill', 'Jim'])
ax.grid(True)

```
设置箭头指示,第一个参数为标注内容,第2个为箭头的坐标,第三个为文本的坐标
```
ax.annotate('[(110,30),(10,9)]:\n(110,10),\n长度为30,高度为9,',(110, 10),
           xytext=(90, 9),
           arrowprops=dict(facecolor='black', shrink=0.05, width=1,
                           headwidth=5,  headlength=3),
           fontsize=10, 
           horizontalalignment='right', verticalalignment='top')
plt.show()

最终图形如下所示:


Broken Barh
上一篇 下一篇

猜你喜欢

热点阅读