简体   繁体   English

我想在每个条的顶部制作带有值的垂直条

[英]I want to make vertical bars with values on top of each bar

the plot is horizontal without values I tried this code plot 是水平的,没有值我试过这段代码

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
g = sns.barplot(
x="score",
y="subject",
hue="pipeline",
#col="dataset",
data=results,
palette="viridis", 
)

I tried this for values我试过这个值

ax = g.axes[0, 0]
ax.bar_label(ax.containers[0])

I had this error 'AxesSubplot' object is not subscriptable我有这个错误 'AxesSubplot' object is not subscriptable

You can try this by using the bar chart from pandas. If you want the plot horizontal use.barh If you provide a snippet from your df it would be easier to fit the data, though.您可以使用 pandas 中的条形图来尝试此操作。如果您想要 plot 水平 use.barh 如果您提供来自 df 的片段,那么拟合数据会更容易。 If you want to use a hue you can just pivot your data to the right format.如果您想使用色调,您只需将 pivot 数据转换为正确的格式即可。

ax = (df 
        .plot.bar(#insert your x and y
    )
    )
    for c in ax.containers:
    
        # Optional: if the segment is small or 0, customize the labels
        labels = [v.get_height() if v.get_height() > 0 else '' for v in c]
        
        # remove the labels parameter if it's not needed for customized labels
        ax.bar_label(c, labels=labels, label_type='edge')

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

相关问题 如何在每个垂直条上添加许多水平条? - How to add many horizontal bars to each vertical bar? Matplotlib条形图,条形图相互重叠,如何创建空间 - Matplotlib bar plot, bars is on top of each other, how to create space 如何在条形顶部添加唯一值而不是条形值? - How to add unique values on top of bars that is not the value of the bar? 对于 python 我想让列表中的每个值响应另一个值 - For python I want to make each values in a list respond to another value 使条形图中的条形随着值的减小而淡出 - Make bars in a bar chart fade out as the values decrease 我在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 Matplotlib 单杠 Plot (barh):为什么单杠在上面而不是并排? - Matplotlib Horizontal Bar Plot (barh): Why are the bars on top and not beside each other? 如何在Pygal的堆积条形图中为条形添加值? - How do I add values to the bars in a Stacked Bar chart in Pygal? 在条形图中的条形上显示值 - show values on bars in bar charts 在散景中的条形图顶部添加误差线 - Adding error bars on top of bar charts in Bokeh
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM