简体   繁体   English

如何使用 matplotlib 在此条形图的相应条形上方显示这些值? 我的尝试不起作用

[英]How do I display these values above their respective bars on this bar chart with matplotlib? My attempts are not working

I need to display the values above their respective bars.我需要在它们各自的条形上方显示值。 Can't quite figure it out.完全想不通。 I have been trying for loops, and was told to possibly use patches but I'm pretty new at this still and having some trouble.我一直在尝试 for 循环,并被告知可能使用补丁,但我对此还是很陌生并且遇到了一些麻烦。 Help would be appreciated.帮助将不胜感激。 The pictures include the short bit of code from the jupyter notebook.图片包括来自 jupyter notebook 的一小段代码。

Here is the pseudocode这是伪代码

df=read_csv
df.sort_values
df = df/2233*100.round

ax=df.plot(bar)
ax.set_title
ax.tick_params
ax.legend

for i, value in enumerate(df):
    label=str(value)
    ax.annotate(label, xy=(i, value))

First step第一步

Second step第二步

The actual code I try,我尝试的实际代码,

for i, value in enumerate(df_dstopics):
    label = str(value)
    ax.annotate(label, xy=(i, value))

Parameter xy in ax.annotate() needs to be a tuple which represents a point in coordinate system. ax.annotate()中的参数xy需要是一个元组,表示坐标系中的一个点。 However, the value in your for i, value in enumerate(df) is column name, which is definitely not a valid constant.但是,您的for i, value in enumerate(df) value的值是列名,这绝对不是有效的常量。

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

df = pd.DataFrame({'Very interested':np.random.rand(2), 'Somewhat interested':np.random.rand(2)+1, 'Not interested':np.random.rand(2)+2})

ax = df.plot(kind='bar', color=['r','b','g']) 

for p in ax.patches:
    ax.annotate(s=np.round(p.get_height(), decimals=2),
                xy=(p.get_x()+p.get_width()/2., p.get_height()),
                ha='center',
                va='center',
                xytext=(0, 10),
                textcoords='offset points')

plt.show()

在此处输入图像描述

Reference:参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 matplotlib 条形图中的条形上方显示 Y 值? - How do I display Y values above the bars in a matplotlib barchart? 如何获取 matplotlib 条形图中的所有条形? - How do I get all bars in a matplotlib bar chart? 如何在Pygal的堆积条形图中为条形添加值? - How do I add values to the bars in a Stacked Bar chart in Pygal? 我在matplotlib(python)中有一个带有误差条的条形图,但我希望误差条位于该条的中间。 我该怎么做呢? 谢谢 - I have a bar chart with error bars in matplotlib (python), but i want the error bars in the middle of the bar. how do i do this? Thanks 在 python 的条形图上的条形上方添加值 - Add values above bars on a bar chart in python 如何将第二个轴添加到 matplotlib/seaborn 条形图并使次要点与正确的条形对齐? - How do I add a second axis to a matplotlib/seaborn bar chart and have the secondary points align with the correct bars? 如何避免在matplotlib中的多条形图中的条形重叠 - How do I avoid overlap between bars in a multi-bar chart in matplotlib 如何控制在matplotlib 2.1.0 barh图表中显示条的顺序? - How do I control the order to display bars in a matplotlib 2.1.0 barh chart? 如何确定matplotlib条形图中条形的顺序 - How to determine the order of bars in a matplotlib bar chart Matplotlib条形图,带有不同的颜色条和显示值的条 - Matplotlib Bar chart with different color bars and bar showing values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM