繁体   English   中英

Y 轴刻度未使用 Seaborn 定义

[英]Y-axis ticks not defined using Seaborn

我们有一个简单的 dataframe 定义为;

df=pd.DataFrame({
'year':[2013,2014,2015,2016,2017,2018,2013,2014,2015,2016,2017,2018],
'incidents_100000':[0.8,122.03,146.28,147.56,178.71,37.73,1.04,197.23,168.87,215.71,179.28,36.69],
'City':['Baltimore','Baltimore','Baltimore','Baltimore','Baltimore','Baltimore','New Orleans','New Orleans',
       'New Orleans','New Orleans','New Orleans','New Orleans']
})

目标是获得每个城市、巴尔的摩和新奥尔良的多面条形图。 这里是获取每年对应的incidents_100000的代码;

gnew3 = sns.catplot(x="year", y="incidents_100000", hue='City', col='City',
             data=df, saturation=.5,
             kind="bar", ci=None, height = 8, aspect=.5)
 font = {'family': 'serif',
    'weight': 'bold',
    'size': 14,
    }
plt.subplots_adjust(top=0.8)
gnew3.fig.suptitle('Variation of Gun Incidents Per 100000 People', fontsize=18)
gnew3.set_xlabels('Year', fontsize=16)
gnew3.set_ylabels('Gun Incidents Per 100000 People', fontsize=16)
gnew3.set_xticklabels(fontdict=font)
gnew3.set_yticklabels(fontdict=font)
gnew3.set_titles(col_template='{col_name}', fontdict=font)
plt.show()

此代码给出以下 plot;

在此处输入图像描述

正如我们所见,我们没有看到 y 轴刻度上的值。 但是当我们注释掉代码中的gnew3.set_yticklabels(fontdict=font)

gnew3 = sns.catplot(x="year", y="incidents_100000", hue='City', col='City',
             data=df, saturation=.5,
             kind="bar", ci=None, height = 8, aspect=.5)
font = {'family': 'serif',
    'weight': 'bold',
    'size': 14,
    }
 plt.subplots_adjust(top=0.8)
 gnew3.fig.suptitle('Variation of Gun Incidents Per 100000 People', fontsize=18)
 gnew3.set_xlabels('Year', fontsize=16)
 gnew3.set_ylabels('Gun Incidents Per 100000 People', fontsize=16)
 gnew3.set_xticklabels(fontdict=font)
 #gnew3.set_yticklabels(fontdict=font)
  gnew3.set_titles(col_template='{col_name}', fontdict=font)
 plt.show()

这给出了以下 plot;

在此处输入图像描述

现在我们可以看到 y 轴刻度。 我的问题是; 为什么当我们注释行gnew3.set_yticklabels(fontdict=font)时,我们会看到 y 轴刻度上的值。 这似乎与我完全相反。 反馈表示赞赏。

您在调用set_yticklabels()时错过了设置标签。 更改此行:

gnew3.set_yticklabels(labels=[0, 50,100,150,200],fontdict=font)

Output

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM