简体   繁体   中英

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

the plot is horizontal without values I tried this code

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

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. If you want to use a hue you can just pivot your data to the right format.

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM