matplotlib X轴标签未与海运图上的值对齐

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

我有下面的代码。我试图创建一个线图。但是,当我标记x轴时,x轴的值似乎被塞满了,我不知道为什么?

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 = 20,fontsize=15,weight='bold')
Graph2.set_xlabel( "Age",labelpad = 20,fontsize=15,weight='bold')
Graph1.set_ylabel("Wayfinding Distance",labelpad = 20,fontsize=15,weight='bold')
Graph2.set_ylabel("Wayfinding Duration",labelpad = 20,fontsize=15,weight='bold')
xticklabels = ['18-28','28-38','38-48','48-58','58+']
Graph1.set_xticklabels(xticklabels,rotation = 30, ha="right",fontsize=12)
Graph2.set_xticklabels(xticklabels,rotation = 30, ha="right",fontsize=12)

x1c 0d1x会非常感谢你的帮助!

oyxsuwqo

oyxsuwqo1#

为了解决这个问题,我手动分配了刻度位置,然后给它们分配了标签:)

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+']
newerdf['age'] = np.digitize(newerdf['age'], bins)
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])
axes[0].set_xticks([1.0,2.0,3.0,4.0,5.0])
axes[1].set_xticks([1.0,2.0,3.0,4.0,5.0])
axes[0].set_xticklabels(names, minor=False)
axes[1].set_xticklabels(names, minor=False)
Graph1.set_xlabel( "Age",labelpad = 20,fontsize=15,weight='bold')
Graph2.set_xlabel( "Age",labelpad = 20,fontsize=15,weight='bold')
Graph1.set_ylabel("Wayfinding Distance",labelpad = 20,fontsize=15,weight='bold')
Graph2.set_ylabel("Wayfinding Duration",labelpad = 20,fontsize=15,weight='bold')
xticklabels = ['18-28','28-38','38-48','48-58','58+']
Graph1.set_xticklabels(xticklabels,rotation = 30, ha="right",fontsize=12)
Graph2.set_xticklabels(xticklabels,rotation = 30, ha="right",fontsize=12)

相关问题