【问题标题】:Plotting a horizontal bar graph in matplolib在 matplotlib 中绘制水平条形图
【发布时间】:2015-09-12 08:24:24
【问题描述】:

我有以下数据框:

    strterminationreason    total_trials    %Trials
0   Completed, Negative outcome/primary endpoint(s...   3130    6.390624
1   Completed, Outcome indeterminate    3488    7.121565
2   Completed, Outcome unknown  6483    13.236555
3   Completed, Positive outcome/primary endpoint(s...   15036   30.699498
4   Terminated, Business decision - Drug strategy ...   526 1.073952
5   Terminated, Business decision - Other   1340    2.735922
6   Terminated, Business decision - Pipeline repri...   1891    3.860917
7   Terminated, Early positive outcome  231 0.471640
8   Terminated, Lack of efficacy    1621    3.309649
9   Terminated, Lack of funding 533 1.088244
10  Terminated, Other   1253    2.558291
11  Terminated, Planned but never initiated 4441    9.067336
12  Terminated, Poor enrollment 3201    6.535587
13  Terminated, Safety/adverse effects  993 2.027441
14  Terminated, Unknown 4811    9.82277

我使用以下代码水平绘制条形图,因为正常的不适合上面的文本代码。

df['%Trials']=(df.ix[:,1]/sum(df.ix[:,1]))*100

plt.figure(figsize=(35,20))
plt.barh(df.ix[:,2],df.index,align='edge')
plt.xlim([0,31])
plt.yticks(df.index, df.strterminationreason)
plt.ylabel("TerminationReason",fontsize=20)
plt.xlabel("%Trials",fontsize=20)

但我得到的输出是条形范围不反映数据框中的实际 % 值。就像最高百分比是完成的,积极的结果/主要终点,但它不显示相同。知道为什么吗?

还有人知道如何正确调整每个条形下方的文本,以便没有重叠且干净。

【问题讨论】:

  • 请检查barh 的参数顺序:matplotlib.pyplot.barh(bottom, width, height=0.8, left=None, hold=None, **kwargs) -- bottom 是条形的 y 坐标,width 是 x 轴上条形的宽度
  • 不明白。你能帮忙吗?

标签: python pandas matplotlib


【解决方案1】:

您的情节看起来不正确的原因是您以相反的顺序将参数传递给barh。您可以找到matplotlib.pyplot.barhhere 的文档。这是一个稍微修改的脚本,可以解决您的问题:

bottom = range(len(df.index))
width = df['%Trials']
fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111)

ax.barh(bottom, width,color='r',align='edge')
ax.set_yticks(y_pos)
ax.set_yticklabels(df.index)

plt.show()

关于适合您的长标签,您可能需要调整字体、填充或换行以提高可读性。请参阅herehere

【讨论】: