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)

运行正常。

相关推荐
飞翔的佩奇13 分钟前
【完整源码+数据集+部署教程】二维码与查找模式检测系统源码和数据集:改进yolo11-CSwinTransformer
python·yolo·计算机视觉·数据集·yolo11·二维码与查找模式检测
大霞上仙19 分钟前
实现自学习系统,输入excel文件,能学习后进行相应回答
python·学习·excel
啊阿狸不会拉杆28 分钟前
《算法导论》第 32 章 - 字符串匹配
开发语言·c++·算法
Caven7728 分钟前
【pytorch】reshape的使用
pytorch·python
无规则ai30 分钟前
动手学深度学习(pytorch版):第四章节—多层感知机(5)权重衰减
人工智能·pytorch·python·深度学习
你知道网上冲浪吗2 小时前
【原创理论】Stochastic Coupled Dyadic System (SCDS):一个用于两性关系动力学建模的随机耦合系统框架
python·算法·数学建模·数值分析
钢铁男儿2 小时前
Python 正则表达式核心元字符全解析
python
武当豆豆2 小时前
C++编程学习(第25天)
开发语言·c++·学习
杨荧2 小时前
基于Python的宠物服务管理系统 Python+Django+Vue.js
大数据·前端·vue.js·爬虫·python·信息可视化
CodeCraft Studio2 小时前
在 Python 中操作 Excel 文件的高效方案 —— Aspose.Cells for Python
python·ui·excel·报表·aspose·aspose.cells