问题描述
我并排放置了这三个 regplots,但是,我想做这些: - 增加图表的大小 - 将它们分开一点,以便更清楚地看到 y 轴 - 查看右侧两个图的 y 轴上的值。
有谁知道如何有效地做到这一点?谢谢
fig,(ax1,ax2,ax3) = plt.subplots(ncols=3,sharey=True)
sns.regplot(x=Dem['Price'],y=Dem['A'],color="g",ax=ax1)
sns.regplot(x=Dem['Price'],y=Dem['B'],color="b",ax=ax2)
sns.regplot(x=Dem['Price'],y=Dem['C'],color="purple",ax=ax3)
解决方法
-
您可以使用:
fig.set_figwidth(25)
来加宽图形并创建传递您想要的任何数值的空间,例如25
。 -
要标记所有子图的 y 轴刻度,请使用:
for ax in fig.axes: ax.tick_params(axis='y',labelleft=True)
具有 flights
seaborn 数据集的完全可重现的代码示例:
import seaborn as sns
df = sns.load_dataset('flights')
df1 = df[df['year']==1949]
df2 = df[df['year']==1950]
df3 = df[df['year']==1951]
fig,(ax1,ax2,ax3) = plt.subplots(ncols=3,sharey=True)
#1
fig.set_figwidth(25)
sns.regplot(x=df1['year'],y=df1['passengers'],color="g",ax=ax1)
sns.regplot(x=df2['year'],y=df2['passengers'],color="b",ax=ax2)
sns.regplot(x=df3['year'],y=df3['passengers'],color="purple",ax=ax3)
#2
for ax in fig.axes:
ax.tick_params(axis='y',labelleft=True)
您的代码:
fig,sharey=True)
fig.set_figwidth(25)
sns.regplot(x=Dem['Price'],y=Dem['A'],ax=ax1)
sns.regplot(x=Dem['Price'],y=Dem['B'],ax=ax2)
sns.regplot(x=Dem['Price'],y=Dem['C'],ax=ax3)
for ax in fig.axes:
ax.tick_params(axis='y',labelleft=True)