0

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

My plot

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

enter image description here

Rox
  • 37
  • 7
  • Please post [repoducible example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). To center justify x-axis lables, my initial guess is you have missing values in the `hue` variable, Province. Not sure what you mean by zoom but matplotlib generates static plot images. – Parfait Nov 05 '20 at 20:18

1 Answers1

1

remove hue='Provinces' in your call to barplot().

hue-nesting is supposed to be in addition to x. So that you can have several categories per x-value. Here you pass the same column to x= and hue= so seaborn is trying to fit N provinces for each x value. But since each x-value is already a province, that does not work.

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
  • I did not remove the hue, but you give me a clue to resolve my problem: – Rox Nov 06 '20 at 01:55
  • First, I set hue to data_x=list(ecuador_df['Provinces']) ->This contains all the provinces, and then, I set dodge= False – Rox Nov 06 '20 at 01:56
  • About the zoom, I think it could be implemented as a consequence of an event, but that requires more implementation and optimization of the code. Thanks Diziet Asahi. – Rox Nov 06 '20 at 02:01