python-docx表格设置阴影_Python Seaborn图表-阴影区域

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.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值