TypeError: lineplot() takes from 0 to 1 positional arguments but 2 were given

使用pyplot生成图形

python 复制代码
from matplotlib import pyplot as plt
import pandas as pd
import seaborn as sns
import numpy as np

# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei']
sns.set_style({'font.sans-serif':['simhei', 'Arial']})

# 加载数据
hr = pd.read_csv('data/hr.csv', encoding='gbk')

data = hr.head(100)
# 使用Matplotlib库绘图
color_map = dict(zip(data['薪资'].unique(), ['b', 'y', 'r']))
for species, group in data.groupby('薪资'):
    plt.scatter(group['每月平均工作小时数(小时)'],
                group['满意度'],
                color=color_map[species], alpha=0.4,
                edgecolors=None, label=species)
plt.legend(frameon=True, title='薪资')
plt.xlabel('平均每个月工作时长(小时)')
plt.ylabel('满意度水平')
plt.title('满意度水平与平均每个月工作小时')
plt.show()

如图所示

使用seaborn生成图形

python 复制代码
# 使用seaborn库绘图
sns.lmplot(x='每月平均工作小时数(小时)',y='满意度', data=data, hue='薪资', fit_reg=False, height=4)
plt.xlabel('平均每个月工作时长(小时)')
plt.ylabel('满意度水平')
plt.title('满意度水平与平均每个月工作小时')
plt.show()

也没有问题,如图2所示

出现报错

python 复制代码
plt.rcParams['axes.unicode_minus'] = False
x = np.arange(1, 10, 2)
y1 = x + 1
y2 = x + 3
y3 = x + 5
# 绘制3条不同的直线
# 第1部分
plt.title('Matplotlib库的绘图风格')
plt.plot(x, y1)
plt.plot(x, y2)
plt.plot(x, y3)
plt.show()

显示正常。说明pyplot是没有太多参数讲究的。现在问题来了。现在用seabon来绘图。

python 复制代码
# 使用seaborn库绘图
sns.set_style('darkgrid')  # 全黑风格
sns.set_style({'font.sans-serif':['simhei', 'Arial']})
sns.lineplot(x, y1)
sns.lineplot(x, y2)
sns.lineplot(x, y3)
plt.title('seaborn库的绘图风格')
plt.show()
复制代码
TypeError: lineplot() takes from 0 to 1 positional arguments but 2 were given

查找资料后发现,不可以省略x、y参数。更改后:

python 复制代码
sns.lineplot(x=x, y=y1)
sns.lineplot(x=x, y=y2)
sns.lineplot(x=x, y=y3)

运行正常。

相关推荐
打码农的篮球9 分钟前
STL——list
开发语言·c++·list
lkf1971116 分钟前
商品中心—1.B端建品和C端缓存
开发语言·后端·缓存
渣渣盟36 分钟前
JavaScript核心概念全解析
开发语言·javascript·es6
我的ID配享太庙呀1 小时前
Django 科普介绍:从入门到了解其核心魅力
数据库·后端·python·mysql·django·sqlite
java叶新东老师1 小时前
goland编写go语言导入自定义包出现: package xxx is not in GOROOT (/xxx/xxx) 的解决方案
开发语言·后端·golang
@蓝莓果粒茶2 小时前
LeetCode第350题_两个数组的交集II
c++·python·学习·算法·leetcode·职场和发展·c#
FinAnalyzer2 小时前
如何在 InsCodeAI 上搭建并使用 Jupyter Notebook 环境?
ide·python·jupyter
java1234_小锋2 小时前
【NLP舆情分析】基于python微博舆情分析可视化系统(flask+pandas+echarts) 视频教程 - 微博文章数据可视化分析-文章分类下拉框实现
python·自然语言处理·flask
檀越剑指大厂2 小时前
【Python系列】Flask 应用中的主动垃圾回收
开发语言·python·flask
檀越剑指大厂2 小时前
【Python系列】使用 memory_profiler 诊断 Flask 应用内存问题
开发语言·python·flask