【问题标题】:How to add an extra number on top of the each bar on barchart如何在条形图的每个条形顶部添加一个额外的数字
【发布时间】:2018-12-02 02:21:14
【问题描述】:

根据解释为什么这个问题和这个link不同 据我了解,此链接从图表中获取高度,但在我的情况下,图表中根本没有 numpatients6month 这一列,我只是在数据框中有它。

所以我有一个条形图。它为每个x-axis 包含两个条形图,其中每个条形图从不同的数据帧中读取。

这是我绘制条形图的代码。

import seaborn as sns
import matplotlib.pyplot as plt
plt.rcParams['axes.prop_cycle'] = ("cycler('color', 'rg')")
dffinal['CI-noCI']='Cognitive Impairement'
nocidffinal['CI-noCI']='Non Cognitive Impairement'
res=pd.concat([dffinal,nocidffinal])

sns.barplot(x='6month',y='final-formula',data=res,hue='CI-noCI').set_title(fs)
plt.xticks(fontsize=8, rotation=45)

plt.show()

如您所见,有two data frame。我用颜色greennocidffinal 用颜色red 绘制dffinal

这是情节的结果:

更多解释:dffinal是基于(6month, final-formula)nocidffinal也是基于(6month,final-formula)

这是我的nocidffinal 数据框:

          6month  final-formula  numPatients6month
137797.0       1       0.035934                974
267492.0       2       0.021705                645
269542.0       3       0.022107                769
271950.0       4       0.020000                650
276638.0       5       0.015588                834
187719.0       6       0.019461                668
218512.0       7       0.011407                789
199830.0       8       0.008863                677
269469.0       9       0.003807                788
293390.0      10       0.009669                724
254783.0      11       0.012195                738
300974.0      12       0.009695                722

dffinal:

          6month  final-formula  numPatients6month
166047.0       1       0.077941                680
82972.0        2       0.057208                437
107227.0       3       0.057348                558
111330.0       4       0.048387                434
95591.0        5       0.033708                534
95809.0        6       0.036117                443
98662.0        7       0.035524                563
192668.0       8       0.029979                467
89460.0        9       0.009709                515
192585.0      10       0.021654                508
184325.0      11       0.017274                521
85068.0       12       0.010438                479

如您所见,此数据框中有 numPatients6month 列,我想将其显示在每个条形图的顶部。 我确实NOT 想要更改条形图并根据此列对其进行分组,而不是我只想将此数字作为额外信息显示给每个条形顶部的用户。

感谢您的宝贵时间:)

【问题讨论】:

  • @ImportanceOfBeingErnest 你能取消标记为重复吗?我添加了解释它与您的链接有何不同!!!
  • 我重新打开了。我认为您还应该添加this question 没有帮助的程度。
  • @ImportanceOfBeingErnest 确定我会添加更多信息,因为我尝试了我在堆栈中找到的三种不同的东西。谢谢:)

标签: pandas matplotlib visualization seaborn data-analysis


【解决方案1】:

如果您将 numPatients6month 列放在一个可迭代项中,并按顺序显示在图表中,然后使用另一个 stackoverflow 答案(也在文档 here 中),您可以正确地将文本放在顶部。

我使用了下面的代码(改编自 this SO 答案)。它一行接一行地组合多列(即,将按图表顺序获取您的所有 numPatients6month 列)

vals = pd.concat([nocidffinal.numPatients6month, dffinal.numPatients6month], axis=1)
vals = vals.stack().reset_index(level=[0,1], drop=True)

这是我的完整代码

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['axes.prop_cycle'] = ("cycler('color', 'rg')")
dffinal['CI-noCI']='Cognitive Impairement'
nocidffinal['CI-noCI']='Non Cognitive Impairement'
res=pd.concat([dffinal,nocidffinal])

# Copied to clipboard from SO question above
# Comment out if you already have your dataframes
nocidffinal = pd.read_clipboard().reset_index() 
dffinal = pd.read_clipboard().reset_index() 

# This will merge columns in order of the chart
vals = pd.concat([nocidffinal.numPatients6month, dffinal.numPatients6month], axis=1)
vals = vals.stack().reset_index(level=[0,1], drop=True)

# Plot the chart
ax = sns.barplot(x='6month', y='final-formula', data=res, hue='CI-noCI')
_ = plt.xticks(fontsize=8, rotation=45)

# Add the values on top of each correct bar
for idx, p in enumerate(ax.patches):
    height = p.get_height()
    ax.text(p.get_x()+p.get_width()/2.,
            height + height*.01,
            vals[idx],
            ha="center")

【讨论】:

  • 我希望你能像你一样帮助我启动结果。我在 nocidffinal = pd.read_clipboard().reset_index() 处遇到错误
  • 为响应干杯
  • 是的,很高兴它有效。 read_clipboard 用于数据帧nocidffinaldffinal,我从上面复制到剪贴板。对你来说,你已经有了这些,所以你可以把这些行注释掉:)
  • 是的,我知道它是如何工作的以及它们是什么,再次感谢:)
猜你喜欢
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 2019-01-25
  • 1970-01-01
  • 1970-01-01
  • 2020-09-02
  • 1970-01-01
相关资源
最近更新 更多