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)