使用数据框中的列为海洋图的背景着色 [英] Colorize the background of a seaborn plot using a column in dataframe

查看:63
本文介绍了使用数据框中的列为海洋图的背景着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题



如何为



所需的输出



我想拥有的东西新列背景 和任何

解决方案

ax.axvspan()可以工作对您来说,假设背景不会在时间点上重叠。

 将numpy导入为np 
进口seaborn为sns; sns.set()
导入matplotlib.pyplot as plt
fmri = sns.load_dataset( fmri)
fmri.sort_values('timepoint',inplace = True)
arr = np.ones(len(fmri))
arr [:300] = 0
arr [600:] = 2
fmri ['background'] = arr
fmri [ 'background'] = fmri ['background']。astype(int).astype(str).map(lambda x:'C'+ x)

ax = sns.lineplot(x = timepoint,y = signal,hue = event,data = fmri)
范围= fmri.groupby('background')['timepoint']。agg(['min','max'] )
for i,range.iterrows()中的行:
ax.axvspan(xmin = row ['min'],xmax = row ['max'],facecolor = i,alpha = 0.3)


Question

How to shade or colorize the background of a seaborn plot using a column of a dataframe?

Code snippet

import numpy as np
import seaborn as sns; sns.set()
import matplotlib.pyplot as plt
fmri = sns.load_dataset("fmri")
fmri.sort_values('timepoint',inplace=True)
ax = sns.lineplot(x="timepoint", y="signal", data=fmri)
arr = np.ones(len(fmri))
arr[:300] = 0
arr[600:] = 2
fmri['background'] = arr

ax = sns.lineplot(x="timepoint", y="signal", hue="event", data=fmri)

Which produced this graph:

Desired output

What I'd like to have, according to the value in the new column 'background' and any palette or user defined colors, something like this:

解决方案

ax.axvspan() could work for you, assuming backgrounds don't overlap over timepoints.

import numpy as np
import seaborn as sns; sns.set()
import matplotlib.pyplot as plt
fmri = sns.load_dataset("fmri")
fmri.sort_values('timepoint',inplace=True)
arr = np.ones(len(fmri))
arr[:300] = 0
arr[600:] = 2
fmri['background'] = arr
fmri['background'] = fmri['background'].astype(int).astype(str).map(lambda x: 'C'+x)

ax = sns.lineplot(x="timepoint", y="signal", hue="event", data=fmri)
ranges = fmri.groupby('background')['timepoint'].agg(['min', 'max'])
for i, row in ranges.iterrows():
    ax.axvspan(xmin=row['min'], xmax=row['max'], facecolor=i, alpha=0.3)

这篇关于使用数据框中的列为海洋图的背景着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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