Python Seaborn 图表 - 阴影区域 [英] Python Seaborn Chart - Shadow Area

查看:52
本文介绍了Python Seaborn 图表 - 阴影区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉我的菜鸟问题,但是如何在 seaborn 图表的上下线之间添加阴影区域/颜色?

我正在处理的主要代码如下:

plt.figure(figsize=(18,10))sns.set(style="darkgrid")调色板= sns.color_palette(mako_r",3)sns.lineplot(x=日期",y=值",hue='Std_Type',style='Value_Type',尺寸=(.25,2.5),调色板=调色板,数据=tbl4)

我们的想法是获得如下效果(来自 seaborn 网站的示例):但是我无法复制效果,尽管我的数据结构与 fmri(seaborn 示例)

的方式几乎相同

来自 seaborn

你有什么想法吗?我尝试更改图表样式,但是如果我转到 distplotrelplot,例如,x_axis 无法显示时间范围...

解决方案

检查此代码:

# 导入将 numpy 导入为 np导入 matplotlib.pyplot 作为 plt将 seaborn 作为 sns 导入将熊猫导入为 pdsns.set(style = 'darkgrid')# 数据生成时间 = pd.date_range(开始 = '2006-01-01',结束 = '2020-01-01',频率 = 'M')tbl4 = pd.DataFrame({'Date': 时间,'向下':1 - 0.5*np.random.randn(len(time)),'向上':4 + 0.5*np.random.randn(len(time))})tbl4 = tbl4.melt(id_vars = '日期',value_vars = ['down', 'up'],var_name = 'Std_Type',value_name = '值')# 图形图, ax = plt.subplots(figsize=(18,10))sns.lineplot(ax = ax,x = '日期',y = '值',色调 = 'Std_Type',数据 = tbl4)# 填充区域plt.fill_between(x = tbl4[tbl4['Std_Type'] == 'down']['Date'],y1 = tbl4[tbl4['Std_Type'] == 'down']['Value'],y2 = tbl4[tbl4['Std_Type'] == 'up']['Value'],α = 0.3,facecolor = '绿色')plt.show()

这给了我这个情节:

由于我无法访问您的数据,因此我生成了随机数据.用你的替换它们.
阴影区域使用 plt.fill_between 完成(文档 此处),其中指定x数组(两条曲线通用),区域的上限和下限为y1y2 和可选的颜色及其透明度,分别带有 facecoloralpha 参数.


您不能通过 ci 参数来实现,因为它用于显示 置信区间 您的数据.

Sorry to my noob question, but how can I add a shadow area/color between the upper and lower lines in a seaborn chart?

The primary code I've working on is the following:

plt.figure(figsize=(18,10))
sns.set(style="darkgrid")
palette = sns.color_palette("mako_r", 3)
sns.lineplot(x="Date", y="Value",  hue='Std_Type', style='Value_Type', sizes=(.25, 2.5), palette = palette, data=tbl4)

The idea is to get some effect like below (the example from seaborn website): But I could not replicate the effect although my data structure is pretty much in the same fashion as fmri (seaborn example)

from seaborn link:

 import seaborn as sns
 sns.set(style="darkgrid")

 # Load an example dataset with long-form data
 fmri = sns.load_dataset("fmri")

 # Plot the responses for different events and regions
 sns.lineplot(x="timepoint", y="signal",
         hue="region", style="event",
         data=fmri)

Do you have some ideas? I tried to change the chart style, but if I go to a distplot or relplot, for example, the x_axis cannot show the timeframe...

解决方案

Check this code:

# import
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
sns.set(style = 'darkgrid')

# data generation
time = pd.date_range(start = '2006-01-01', end = '2020-01-01', freq = 'M')
tbl4 = pd.DataFrame({'Date': time,
                     'down': 1 - 0.5*np.random.randn(len(time)),
                     'up': 4 + 0.5*np.random.randn(len(time))})

tbl4 = tbl4.melt(id_vars = 'Date',
                 value_vars = ['down', 'up'],
                 var_name = 'Std_Type',
                 value_name = 'Value')

# figure plot
fig, ax = plt.subplots(figsize=(18,10))

sns.lineplot(ax = ax,
             x = 'Date',
             y = 'Value',
             hue = 'Std_Type',
             data = tbl4)

# fill area
plt.fill_between(x = tbl4[tbl4['Std_Type'] == 'down']['Date'],
                 y1 = tbl4[tbl4['Std_Type'] == 'down']['Value'],
                 y2 = tbl4[tbl4['Std_Type'] == 'up']['Value'],
                 alpha = 0.3,
                 facecolor = 'green')

plt.show()

which gives me this plot:

Since I do not have access to your data, I generated random ones. Replace them with yours.
The shadow area is done with plt.fill_between (documentation here), where you specify the x array (common to both curves), the upper and lower limits of the area as y1 and y2 and, optionally a color and its transparency with the facecolor and alpha parameters respectively.


You cannot do it through ci parameter, since it is used to show the confidence interval of your data.

这篇关于Python Seaborn 图表 - 阴影区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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