【问题标题】:X-axis labels not aligning with values on seaborn plotX 轴标签与 seaborn 图上的值不对齐
【发布时间】:2022-11-10 22:53:24
【问题描述】:

我在下面有以下代码。我正在尝试创建一个线图。但是,当我标记 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)

非常感谢您的帮助!

【问题讨论】:

    标签: python pandas numpy matplotlib seaborn


    【解决方案1】:

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

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-10
      • 2014-08-28
      • 1970-01-01
      • 2022-10-07
      • 1970-01-01
      • 2017-03-18
      • 1970-01-01
      • 2019-07-12
      相关资源
      最近更新 更多