Is there a parameter in Seaborn catplot to display bars with zero value?

1.1k Views Asked by At

enter image description here

I am using catplot from Seaborn to create this graph to compare the error rate for each x value depending on the complexity. I know that the error rate is zero in some cases hence there is no bar to represent it but is there a way to show that in the graph?

g = sns.catplot(
    data=df, kind="bar",
    x="x", y="incorrect_score", 
    alpha=.7, hue="complexity",
    ci= False
)
g.despine(left=False)
g.set(ylim=(0, 0.9))
g.set_axis_labels("", "Error")
plt.show()
1

There are 1 best solutions below

0
On BEST ANSWER

Probably the easiest would be to show the numbers on top of the bars:

def autolabel(rects, fmt='.2f'):
    # attach some text labels
    for rect in rects:
        height = rect.get_height()
        rect.axes.annotate(f'{{:{fmt}}}'.format(height),
                           xy=(rect.get_x()+rect.get_width()/2., height),
                           xytext=(0, 3), textcoords='offset points',
                           ha='center', va='bottom')

g = sns.catplot(
    data=df, kind="bar",
    x="x", y="incorrect_score", 
    alpha=.7, hue="complexity",
    ci= False
)
g.despine(left=False)
g.set(ylim=(0, 0.9))
g.set_axis_labels("", "Error")

autolabel(g.ax.patches)
plt.show()

enter image description here

Or, if you prefer, on show the labels when the bar is zero:

autolabel([rect for rect in g.ax.patches if rect.get_height()==0.0])

enter image description here