不确定如何在matplotlib上重新排序X轴标签

nkhmeac6  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(137)

我有下面的代码。我正在尝试绘制一个图表。
目前,X轴未按升序进行标记:28-37.99应该在38-47.99之前,但我不确定如何做到这一点。
会如此感激你的帮助!

  1. fig, axes = plt.subplots(nrows=2,figsize=(15, 15))
  2. fig.tight_layout(pad=10)
  3. newerdf = newdf.copy()
  4. bins = [18,28,38,48,58]
  5. names = ['<28','28-37.99','38-47.99','48-57.99','58+']
  6. d = dict(enumerate(names, 1))
  7. newerdf['age'] = np.digitize(newerdf['age'], bins)
  8. newerdf['age'] = newerdf['age'].map(d)
  9. Graph1 = sns.lineplot(data=newerdf,x="age", y="distance",errorbar ='se',err_style='bars',ax=axes[0])
  10. Graph2 = sns.lineplot(data=newerdf,x="age", y="duration",errorbar ='se',err_style='bars',ax=axes[1])
  11. Graph1.set_xlabel( "Age",labelpad = 10,weight='bold')
  12. Graph2.set_xlabel( "Age",labelpad = 10,weight='bold')
  13. Graph1.set_ylabel("Wayfinding Distance",labelpad = 10,weight='bold')
  14. Graph2.set_ylabel("Wayfinding Duration",labelpad = 10,weight='bold')

f8rj6qna

f8rj6qna1#

这里的基本思想是seborn中的x_axis不能解释数据的给定顺序

  1. fig, ax = plt.subplots(nrows=1,figsize=(15, 15))
  2. bins = [18,28,38,48,58]
  3. names = ['<28','28-37.99','38-47.99','48-57.99','58+']
  4. loc_x_axis = np.arange(0,len(names))
  5. ax.plot(loc_x_axis,your_y_data)
  6. # Tip try to ensure that the given y data is ordered accordingly to what you want, so ensure the ytick matchs the xtick location
  7. # This will create your plot, without labels
  8. ax.set_xticks(loc_x_axis,names)
  9. # To set labels accordingly to your wanted arrangement

相关问题