Matplotlib Seaborn relplots创建重复的轴
我正在尝试创建两个情节-一个在另一个与seabornMatplotlib Seaborn relplots创建重复的轴,matplotlib,seaborn,Matplotlib,Seaborn,我正在尝试创建两个情节-一个在另一个与seaborn 我的代码: 但这将创建4个轴,而不是2个!看: 我不再阅读这些额外的2轴-需要帮助。 下面是创建数据的代码: df ={'label': {0: 'top_5', 1: 'first_page', 2: 'win_ratecard', 4: 'switched_off', 5: 'top_5', 6: 'first_page', 7: 'win_ratecard', 9: 'switched_off', 10
我的代码: 但这将创建4个轴,而不是2个!看: 我不再阅读这些额外的2轴-需要帮助。
下面是创建数据的代码:
df ={'label': {0: 'top_5',
1: 'first_page',
2: 'win_ratecard',
4: 'switched_off',
5: 'top_5',
6: 'first_page',
7: 'win_ratecard',
9: 'switched_off',
10: 'top_5',
11: 'first_page'},
'report_date': {0: Timestamp('2018-08-21 00:00:00'),
1: Timestamp('2018-08-21 00:00:00'),
2: Timestamp('2018-08-21 00:00:00'),
4: Timestamp('2018-08-22 00:00:00'),
5: Timestamp('2018-08-22 00:00:00'),
6: Timestamp('2018-08-22 00:00:00'),
7: Timestamp('2018-08-22 00:00:00'),
9: Timestamp('2018-08-23 00:00:00'),
10: Timestamp('2018-08-23 00:00:00'),
11: Timestamp('2018-08-23 00:00:00')},
'sns_codes': {0: 0, 1: 0, 2: 0, 4: 1, 5: 1, 6: 1, 7: 1, 9: 2, 10: 2, 11: 2},
'triad_quantity': {0: 9,
1: 204,
2: 214,
4: 20,
5: 5,
6: 191,
7: 230,
9: 21,
10: 2,
11: 98}}
data_2 = pd.DataFrame(df)
下面是一个可能的解决方案,以摆脱额外的不必要的空图。问题是,当调用
sns.relplot
时,relplot
返回一个class:FacetGrid对象
。这是可以看到的。但是,由于您传递了用于打印的ax1
和ax2
,因此分配变量p1
和p2
的这些面网格显示为空白打印。要消除这些问题,只需添加以下行
plt.close(p1.fig)
plt.close(p2.fig)
RELPROOT是一个函数,因此它将创建地物。如果要在现有matplotlib轴中放置线型图,而不创建无关地物,请使用seaborn的线型图函数,该函数是:
fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True, figsize=(22,8))
p1 = sns.lineplot(x="sns_codes", y="triad_quantity", hue="label", data=data_2, kind="line", ax=ax1)
p2 = sns.lineplot(x="sns_codes", y="triad_quantity", hue="label", data=data_2, kind="line", ax=ax2)
作为示例,您给出的两个绘图似乎做了相同的事情,但是如果您试图做多个绘图,这些绘图沿着数据帧中表示为列的某个维度变化,那么您不必自己指定子绘图。您可以使用seaborn通过sns.replot实现这一点,并使用一个row(facet)参数指定您的绘图变化的
row=“a\u column\u on”
。请参阅以获取说明。这不是我的梦想,但它确实有效。谢谢你为什么这么做relplot
是一个图形级函数,它将创建自己的figure
实例和可能的几个子图。要求它绘制特定轴的图有什么意义?这似乎没有道理。
fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True, figsize=(22,8))
p1 = sns.lineplot(x="sns_codes", y="triad_quantity", hue="label", data=data_2, kind="line", ax=ax1)
p2 = sns.lineplot(x="sns_codes", y="triad_quantity", hue="label", data=data_2, kind="line", ax=ax2)