简体   繁体   English

Y 轴刻度未使用 Seaborn 定义

[英]Y-axis ticks not defined using Seaborn

We have a simple dataframe defined as;我们有一个简单的 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']
})

The goal is to get faceted bar plots for each city, Baltimore as well as New Orleans.目标是获得每个城市、巴尔的摩和新奥尔良的多面条形图。 Here is the code to obtain the incidents_100000 corresponding to each year;这里是获取每年对应的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()

This code gives following plot;此代码给出以下 plot;

在此处输入图像描述

As we can see we are not seeing values on y-axis ticks.正如我们所见,我们没有看到 y 轴刻度上的值。 But when we comment out the line gnew3.set_yticklabels(fontdict=font) in the code;但是当我们注释掉代码中的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()

This gives following plot;这给出了以下 plot;

在此处输入图像描述

Now we can see the y-axis ticks.现在我们可以看到 y 轴刻度。 My question is;我的问题是; how come when we comment the line gnew3.set_yticklabels(fontdict=font) we get to see the values on y-axis ticks.为什么当我们注释行gnew3.set_yticklabels(fontdict=font)时,我们会看到 y 轴刻度上的值。 This seems quite contrary to me.这似乎与我完全相反。 Feedback is appreciated.反馈表示赞赏。

You missed to set your labels while calling set_yticklabels() .您在调用set_yticklabels()时错过了设置标签。 Change this line:更改此行:

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

Output Output

在此处输入图像描述

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

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