Python Seaborn图表-阴影区域 [英] Python Seaborn Chart - Shadow Area
问题描述
对不起我的菜鸟问题,但是如何在海洋图表的上下线之间添加阴影区域/颜色?
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)
这个想法是要获得如下效果(来自seaborn网站的示例): 但是,尽管我的数据结构几乎与 fmri (季节性示例)
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)
来自seaborn 链接:
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)
您有什么想法吗?
我试图更改图表样式,但是例如,如果我转到distplot
或relplot
,则x_axis
无法显示时间范围...
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...
推荐答案
检查此代码:
# 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()
这给了我这个情节:
由于我无权访问您的数据,因此生成了随机数据.用您的替换.
阴影区域由plt.fill_between
(文档此处)完成,在其中指定x
数组(两条曲线均相同)的情况下,该区域的上限和下限分别为y1
和y2
,以及可选的颜色和其透明度,分别使用facecolor
和alpha
参数
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.
您无法通过ci
参数执行此操作,因为它用于显示数据的置信区间.
You cannot do it through ci
parameter, since it is used to show the confidence interval of your data.
这篇关于Python Seaborn图表-阴影区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!