I want to center the xlables, ex: Pinchicha, Guayas, Manabi in the middle of each bar. Also, I want to know if it is possible to gave to this figure a dynamic zoom and increase the bar size that shows all the provinces and its color. Please check below, the image of my seaborn bar.
new_index = (ecuador['Confirmed cases'].sort_values(ascending=False)).index.values
sorted_data = ecuador.reindex(new_index)
sns.set_theme(style="whitegrid")
plt.figure(figsize=(50,20)) # I want to make this parameter dynamic
plt.xticks(rotation= 90)
# Creating the sea barplot
ax = sns.barplot( x="Provinces", y="Confirmed cases", data=sorted_data, orient='v', hue="Provinces", dodge= True, ci=None, palette=("RdYlGn")) #RdYlGn Red is the most critical value.
#Changin the bar size function
def change_width(ax, new_value) :
for patch in ax.patches :
current_width = patch.get_width()
diff = current_width - new_value
# we change the bar width
patch.set_width(new_value)
# we recenter the bar
patch.set_x(patch.get_x() + diff * .5)
for p in ax.patches:
ax.annotate(format(p.get_height()), #, '.1f'
(p.get_x() + p.get_width() / 2., p.get_height()),
ha = 'center', va = 'center',
xytext = (0, 9),
textcoords = 'offset points')
plt.title("COVID in Ecuador", fontsize=20)
change_width(ax, .35)
ax.autoscale_view()
Please help me, I am just learning.
Solution:
new_index = (ecuador_df['Confirmed cases'].sort_values(ascending=False)).index.values
sorted_data = ecuador_df.reindex(new_index)
sns.set_theme(style="whitegrid")
plt.figure(figsize=(50,20)) # I want to make this parameter dynamic
plt.xticks(rotation= 90)
plt.title("COVID in Ecuador", fontsize=20)
# Creating the sea barplot
ax = sns.barplot( x="Provinces", y="Confirmed cases", data=sorted_data, orient='v', hue=data_x, dodge= False, ci=None,palette=("RdYlGn")) #RdYlGn Red is the most critical value.
#Changin the bar size function
def change_width(ax, new_value) :
for patch in ax.patches :
current_width = patch.get_width()
diff = current_width - new_value
# we change the bar width
patch.set_width(new_value)
# we recenter the bar
patch.set_x(patch.get_x() + diff * .5)
for p in ax.patches:
ax.annotate(format(p.get_height()), #, '.1f'
(p.get_x() + p.get_width() / 2., p.get_height()),
ha = 'center', va = 'center',
xytext = (0, 9),
textcoords = 'offset points')
change_width(ax, .35)
ax.autoscale_view()