如何更具体地绘制seaborn线图 [英] How to graph a seaborn lineplot more specifically

查看:71
本文介绍了如何更具体地绘制seaborn线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个海量数据帧 df:

年计数1980年-231980年-41981 101982 01982 4...2007 272008 02008 02009年-72009 5

先按 year 排序,然后按 count 排序.(显示值任意更改)

我想可视化 count 如何随着 year 的增加而不同地分布,这可以通过百分位图最有效地显示出来.但是,由于我的数据是在 DataFrame 中给出的,我认为更可行(坦率地说,更简单)的方法是使用 seaborn.lineplot:

将 matplotlib.pyplot 导入为 plt将 seaborn 作为 sns 导入图, ax = plt.subplots(figsize=[16,12])plt.axhline(y=0, color='black', linestyle='dotted')sns.lineplot(x=年",y=计数",ax=ax,数据=df,颜色=红色")

返回:

此图在某种程度上是有用途的,尽管我希望显示具有更多的可变性,而不仅仅是单个百分位梯度.(一个很好的例子是下面有 10 个百分位梯度的图,从这个链接复制:

我想知道是否有办法使用 seaborn.lineplot 来实现如此详细的绘图,如果没有,是否有办法从 pandas DataFrame 数据.

解决方案

一旦您生成了第一个置信区间或仅生成了这条线,您就可以使用 matplotlib,如下面的

Given a mass DataFrame df:

year        count
1980        -23
1980        -4
1981        10
1982        0
1982        4
...
2007        27
2008        0
2008        0
2009        -7
2009        5

with values sorted by year first, and then count. (the values displayed are arbitrarily changed)

I'd like to visualize how the count distributes differently as year increases, which can be most effectively displayed by a percentile plot. However, since my data are given in a DataFrame, I thought a more feasible (and quite frankly, simpler) way would be to use seaborn.lineplot:

import matplotlib.pyplot as plt
import seaborn as sns

fig, ax = plt.subplots(figsize=[16,12])

plt.axhline(y=0, color='black', linestyle='dotted')
sns.lineplot(x="year", y="count", ax=ax, data=df, color='red')

which returns:

This graph somewhat serves a purpose, although I would like the display to have more variabilities than just a single percentile gradient. (A good example would be a figure below with 10 percentile gradients, copied from this link: Using percentiles of a timeseries to set colour gradient in Python's matplotlib)

I'd like to know if there is a way to achieve such detailed graphing using seaborn.lineplot, and if not, if there is a way to do so from a pandas DataFrame data.

解决方案

Once you have generated the first confidence interval or just the line, you can use matplotlib, as shown in this post to create multiple confidence interval.

The other option is to plot on the same figure using sns.lineplot, though I think seaborn is not meant for this. Using dataset flights as an example, first we plot the median or mean line:

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

flights = sns.load_dataset("flights")
fig,ax = plt.subplots(1,1)

sns.lineplot(data=flights, x="year", y="passengers",ax=ax,ci=None,color="black")

Then we set up a color palette and keep adding the bands without the lines (setting linestyle = ''):

cm = sns.color_palette("Blues",9)

for ix,ci in enumerate(range(10,90,10)):
    sns.lineplot(data=flights, x="year", y="passengers",
                 ci = ci,
                 ax=ax,linestyle='',
                 hue = ci,palette={ci:cm[ix]})

Gives something like this:

这篇关于如何更具体地绘制seaborn线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆