matplotlib - Seaborn relplots 创建重复的轴
问题描述
我正在尝试创建两个情节 - 一个下一个与 seaborn !
我的代码:
fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True, figsize=(22,8))
p1 = sns.relplot(x="sns_codes", y="triad_quantity", hue="label", data=data_2, kind="line", ax=ax1)
p2 = sns.relplot(x="sns_codes", y="triad_quantity", hue="label", data=data_2, kind="line", ax=ax2)
但这会创建 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 object
. 这可以在这里看到。但是由于您通过ax1
并ax2
进行绘图,这些 FacetGrids 被分配了变量p1
并p2
显示为空白图。要摆脱这些,只需添加以下几行
plt.close(p1.fig)
plt.close(p2.fig)
推荐阅读
- reactjs - 如何将 Spring Boot 与 React 集成
- docker - 如何在现有的 docker swarm 中生成交互式容器?
- reactjs - React Table 如何添加/更改单个单元格
- r - 闪亮的 DT 表中的 unbindalll 问题
- c++ - 编译 woff2 时出现错误“需要 libbrotli”
- java - 如何从用户输入数组中调用某些信息
- firebase - Flutter - firebase_analytics 请求权限
- python - 使用 kivy-ios 构建并让 pyrebase/pycryptodome(x) 工作(解决方案)
- c# - Winform 使用通用方法将按钮文本复制到文本框
- flutter - Appbar 标题文本 fontSize 不变