python - 无法使用 seaborn 绘制双轴
问题描述
当我尝试在 jupyter notebook 中使用 seaborn 绘制双轴图时遇到问题重要说明:该代码在 Python 2 中运行良好。
使用 anaconda 升级到 Python 3 后,我收到了一个愚蠢的错误消息:
/Users/enyi/opt/anaconda3/lib/python3.7/site-packages/seaborn/categorical.py:3720: UserWarning: catplot is a figure-level function and does not accept target axes. You may wish to try countplot
warnings.warn(msg, UserWarning)
这是我的代码的输出图像:
我的代码:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv('tips.csv')
fig, ax = plt.subplots(1,2,figsize = (10,5))
sns.catplot(x='sex', hue = 'group', data= df, kind = 'count', ax=ax[0])
sns.catplot(x='sex', y='conversion',hue = 'group', data= df, kind = 'bar',ax=ax[2])
plt.show()
解决方案
我看不出您的代码如何与 Python2 一起工作,但这无关紧要。错误消息清楚地告诉您catplot
不带ax=
参数。如果要在子图上绘图,则必须使用底层绘图功能(在第一种情况下,countplot
如错误所示)
fig, ax = plt.subplots(1,2,figsize = (10,5))
sns.countplot(x='sex', hue = 'group', data= df, ax=ax[0])
sns.barplot(x='sex', y='conversion',hue = 'group', data= df,ax=ax[1])
推荐阅读
- symfony - 在登录表单之前重定向到页面 Symfony 3.4 正在自定义侦听器中解析为 liip_imagine 路由
- android - 如果 10 秒内没有响应,如何取消 volley 请求
- python - matplotlib:使用样式“fivethirtyeight”时,如何在条形图和图例后面绘制网格线
- class - 从 x86-64 可执行文件中查找类和函数名称
- multiple-inheritance - 我可以依靠 ISE Eiffel 作为一种编程语言通过数据库连接提供 Web 服务吗
- android - React Native 重复资源
- php - PHP 联系表单 - 基于选定表单值的动态电子邮件列表
- oracle - Oracle XE (11g) 分页失败。原因:偏移获取失败并出现错误:命令未正确结束
- c# - Entity Core 2.0.3 例外:必须是可简化节点
- c# - 与 C# 泛型、T、抽象有点混淆?