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();
