首页 > 解决方案 > matplotlib ax.bar() 调用后哪些条形图可见(显示)?

问题描述

假设我们在 matplotlib 中绘制条形图。

当值在同一范围内时,我们可以看到所有条形。

但是,当某些值太高时,我们看不到值非常低的条形图。

有没有办法知道在调用 ax.bar() 之后使用其返回的类型为“matplotlib.container.BarContainer”的对象显示(可见)哪些条?

import numpy as np
import matplotlib.pyplot as plt

N = 5
# menMeans = (20, 35, 30, 35, 27) # All 5 bars are shown
menMeans = (20, 35000, 30, 35, 27) # Only 2nd bar is shown
menStd =   (2, 3, 4, 1, 2)

ind = np.arange(N)  
width = 0.35       

fig = plt.figure()
ax = fig.add_subplot(111)
rects1 = ax.bar(ind, menMeans, width, color='royalblue', yerr=menStd)

ax.set_ylabel('Scores')
ax.set_title('Scores')
ax.set_xticks(ind)
ax.set_xticklabels( ('a1', 'a2', 'a3', 'a4', 'a5') )

plt.show()

这些命令写入所有条形图。但是,它们并未显示在最终情节中。

for i in rects1.patches: 
  print(i.get_height(),i.get_bbox(), i.get_visible()) 

在此处输入图像描述 在此处输入图像描述

标签: matplotlibbar-chart

解决方案


推荐阅读