How to shade or colorize the background of a seaborn plot using a column of a dataframe?
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:
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:
set_facecolor() method is used to change the inner background color of the plot. figure(facecolor='color') method is used to change the outer background color of the plot.
Seaborn provides an API on top of Matplotlib that offers sane choices for plot style and color defaults, defines simple high-level functions for common statistical plot types, and integrates with the functionality provided by Pandas DataFrame s.
The despine() is a function that removes the spines from the right and upper portion of the plot by default. sns. despine(left = True) helps remove the spine from the left.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With