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

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

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

fig, axes = plt.subplots(nrows=2,figsize=(15, 15))
fig.tight_layout(pad=10)

newerdf = newdf.copy()
bins = [18,28,38,48,58]
names = ['<28','28-37.99','38-47.99','48-57.99','58+']
d = dict(enumerate(names, 1))
newerdf['age'] = np.digitize(newerdf['age'], bins)
newerdf['age'] = newerdf['age'].map(d)
Graph1 = sns.lineplot(data=newerdf,x="age", y="distance",errorbar ='se',err_style='bars',ax=axes[0])
Graph2 = sns.lineplot(data=newerdf,x="age", y="duration",errorbar ='se',err_style='bars',ax=axes[1])
Graph1.set_xlabel( "Age",labelpad = 10,weight='bold')
Graph2.set_xlabel( "Age",labelpad = 10,weight='bold')
Graph1.set_ylabel("Wayfinding Distance",labelpad = 10,weight='bold')
Graph2.set_ylabel("Wayfinding Duration",labelpad = 10,weight='bold')

f8rj6qna

f8rj6qna1#

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

fig, ax = plt.subplots(nrows=1,figsize=(15, 15))

bins = [18,28,38,48,58]
names = ['<28','28-37.99','38-47.99','48-57.99','58+']
loc_x_axis = np.arange(0,len(names))
ax.plot(loc_x_axis,your_y_data)
# Tip try to ensure that the given y data is ordered accordingly to what you want, so ensure the ytick matchs the xtick location
# This will create your plot, without labels

ax.set_xticks(loc_x_axis,names)
# To set labels accordingly to your wanted arrangement

相关问题