Why cannot see error bar cap , when I move the position using ax.axes[0].lines[2].set_xdata()

Viewed 23

I try to move the bar and error bar to the center of tickmark. using ax.axes.patches, move bar by bar.set_x(current_pos+(current_width)), and move the error bar by ax.axes.lines[0].set_xdata(current_pos+(current_width*1.5)).

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

%matplotlib inline
df = sns.load_dataset("penguins")

ax = sns.barplot(data=df, x="island", y="body_mass_g", ci = "sd", capsize = 0.2, hue = "species")


for i, bar in enumerate(ax.axes.patches): 

     # move the missing to the centre
    current_width = bar.get_width()
    current_pos = bar.get_x()
    if i == 0:
        #move bar
        bar.set_x(current_pos+(current_width))
        
        #change color and move error bar
        ax.axes.lines[0].set_color("red")
        ax.axes.lines[0].set_xdata(current_pos+(current_width*1.5))
        
        #change color of error bar upper cap
        ax.axes.lines[2].set_color("red")

plt.show();

when I change the position of the error bar upper cap (ax.axes.lines2.set_xdata(current_pos+(current_width*1.5))), the cap is gone. How can I set error cap visible or move the error cap?

ax = sns.barplot(data=df, x="island", y="body_mass_g", ci = "sd", capsize = 0.2, hue = "species")


for i, bar in enumerate(ax.axes.patches): 

     # move the missing to the centre
    current_width = bar.get_width()
    current_pos = bar.get_x()
    if i == 0:
        #move bar
        bar.set_x(current_pos+(current_width))
        
        #change color and move error bar
        ax.axes.lines[0].set_color("red")
        ax.axes.lines[0].set_xdata(current_pos+(current_width*1.5))
        
        #change color of error bar upper cap
        ax.axes.lines[2].set_color("red")

        #change position of error bar upper cap
        ax.axes.lines[2].set_xdata(current_pos+(current_width*1.5))
plt.show();
1 Answers

The errorbar lines have two coordinates for start and end. For the two horizontal lines (line1 and line[2]), you should be using the get_xdata() and then adding the offset. In the below code, I have updated the the same by reading the data into a temp variable called newline, then updating it and finally updating it back using set_xdata(). I think you might want to do it for the vertical line (line[0]) as well, but since it works without this, perhaps you can leave it as is. Below is the code...

df = sns.load_dataset("penguins")

ax = sns.barplot(data=df, x="island", y="body_mass_g", ci = "sd", capsize = 0.2, hue = "species")

for i, bar in enumerate(ax.axes.patches): 
     # move the missing to the centre
    current_width = bar.get_width()
    current_pos = bar.get_x()
    if i == 0:
        #move bar
        bar.set_x(current_pos+(current_width))
        
        #change color and move error bar
        ax.axes.lines[0].set_color("red")
        ax.axes.lines[0].set_xdata(current_pos+(current_width*1.5))

        #change color of error bar upper cap
        ax.axes.lines[1].set_color("red")
        newline=ax.axes.lines[1].get_xdata() ## Read the xdata
        newline[0] += current_width  ## Add offset to first parameter
        newline[1] += current_width  ## Add offset to second parameter
        ax.axes.lines[1].set_xdata(newline)  ## Set data of the line
        
        ax.axes.lines[2].set_color("red")  ##Do the same for second line
        newline=ax.axes.lines[2].get_xdata()
        newline[0] += current_width
        newline[1] += current_width
        ax.axes.lines[2].set_xdata(newline)
plt.show();

enter image description here

Related