Python 如何在seaborn条形图中将条形图的名称居中,并向条形图添加缩放选项? 我想把XLable的中心放在前,品迟查,Guayas,Maabi在每个酒吧中间。此外,我想知道是否有可能给这个数字一个动态缩放,并增加显示所有省份及其颜色的栏大小。请检查下面,我的seaborn酒吧的图片 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()

Python 如何在seaborn条形图中将条形图的名称居中,并向条形图添加缩放选项? 我想把XLable的中心放在前,品迟查,Guayas,Maabi在每个酒吧中间。此外,我想知道是否有可能给这个数字一个动态缩放,并增加显示所有省份及其颜色的栏大小。请检查下面,我的seaborn酒吧的图片 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(),python,plot,seaborn,Python,Plot,Seaborn,请帮帮我,我只是在学习 解决方案: 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

请帮帮我,我只是在学习

解决方案:

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

在调用
barplot()
时删除
hue='Provinces'


色调嵌套应该是x之外的。因此,每个x值可以有多个类别。这里,您将同一列传递给
x=
hue=
,因此seaborn试图为每个x值拟合N个省份。但由于每个x值都已经是一个省,所以这不起作用。

请发布。要居中对齐x轴标签,我的初步猜测是,
hue
变量Province中缺少值。不确定缩放是什么意思,但matplotlib生成静态打印图像。我没有删除色调,但你给了我一个解决问题的线索:首先,我将色调设置为data_x=list(ecuador_df['Provinces'])->这包含所有省份,然后,我将dodge=False设置为关于缩放,我认为这可能是一个事件的结果,但这需要更多的代码实现和优化。谢谢Diziet Asahi。