5-2 线图

df.plot():默认绘制的就是线图,实际上是对matplotlib.pyplot.plot()的打包调用。

matplotlib.pyplot.plot():默认绘制的也是线图,事先计算好汇总数值即可。

seaborn.lineplot(): seaborn 0.9版本新增,绘制各类线图。

seaborn.pointplot(): 绘制x轴为有序分类变量的线图,可叠加绘制误差图。

用matplotlib绘制

sumdata = ccss.groupby('time').index1.mean()
sumdata

在这里插入图片描述

sumdata.plot()

在这里插入图片描述

plt.plot(sumdata)
# 将数值索引转换为字符串格式
sumdata.index.astype('str')

在这里插入图片描述

plt.plot(sumdata.index.astype('str'), sumdata, 'bo-')

在这里插入图片描述

用lineplot()函数绘制

seaborn.lineplot()针对的x轴为理论上可以连续取值的变量(如时间),因此默认计算出的CI为连贯的条带。

如果x轴是有序分类变量,则应当慎用该函数

seaborn.lineplot(

x, y, hue : names of variables in data
data : 用于绘图的数据框
order, hue_order : 分类变量/hue变量各类别取值的绘图顺序

estimator = ‘mean’ : 对y变量的汇总方式,为None时绘制所有原始值
sort = True: 绘图前数据是否按照x和y轴变量排序,否则将按照数据原始顺序绘制
绝大多数情况下,按照原始数据绘制的图形并无意义
legend = ‘brief’ : 图例的显示方式
“brief”, “full”, or False, optional

格式设定:
size : 线段宽度所对应的变量/数值
sizes : list, dict, or tuple, 用于进一步设置线宽如何确定
size_norm : 进一步指定数值的标准化方法用于线段宽度
size_order : list, 线宽在各线段中的使用顺序

style : 线段形状所对应的变量/数值
dashes = True : 针对style变量的线形绘制设定
    boolean, list, or dictionary
style_order : list, 线形的使用顺序

markers : 数据点的显示方式
    boolean, list, or dictionary, optional

palette : hue变量所对应的调色盘设定,dict/seaborn调色盘格式
hue_norm : 当hue变量为数值时,可进一步指定数值的标准化方法用于颜色映射

可信区间计算:
ci = 95 : float or “sd” or None, 希望绘制的可信区间宽度
n_boot = 1000 : 计算CI时的bootstrap抽样次数
units : 用于确定抽样单元大小的变量
err_style = ‘band’ : “band” or “bars”, 代表CI的误差条的显示方式
err_band : dict of keyword arguments,进一步控制误差条的显示方式
具体参数来自于ax.fill_between或ax.errorbar
)
在这里插入图片描述

# 错误地使用lineplot
# 从seaborn 0.12版起,必须严格使用如下格式才能正确运行:sns.lineplot(data = ccss, x = 'time', y = 'index1')
sns.lineplot(ccss.time, ccss.index1)

在这里插入图片描述

# 正确的使用lineplot,x轴变量设定为分类,误差修改为离散条状
# 如果time列被读入为字符串,则需要先进行转换
dftmp = ccss[['time', 'index1']].copy()
dftmp.time = dftmp.time.astype('str')
sns.lineplot(data = dftmp, x = 'time', y = 'index1', err_style ='bars')
# 只绘制线图
sns.lineplot(data = ccss, x = 's3', y = 'index1', ci = None)

在这里插入图片描述

# 绘制分组线图
sns.lineplot(data = ccss, x  = 's3', y ='index1', hue = 's2', ci = None)

在这里插入图片描述

# 使用线形
sns.lineplot(x = 's4', y = 'index1', data = ccss, 
             style = 's0', size = 's0', ci = None)

在这里插入图片描述

# 在图例中同时包含颜色和线形
sns.lineplot(x = 's4', y = 'index1', hue = 's2', data = ccss,
             style = 's0', ci = None)

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

juicy-hua

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值