1

I want to have a contour plot where different color bands represent different confidence intervals of a line plot. consider the code below from this page:

import seaborn as sns; sns.set()
import matplotlib.pyplot as plt
fmri = sns.load_dataset("fmri")
ax = sns.lineplot(x="timepoint", y="signal", data=fmri)

which results in:

        
                     Fig.1 - image courtesy of pydata.org.                

now if I'm not mistaken those error bands represent a confidence band (e.g. 95%). What I want is to have different confidence intervals (e.g. given a list like [50, 60, 70, 80, 90]) represented as a contour plot around the centerline.

P.S.1. It doesn't have to be seaborn or even matplotlib for that matter. Even non-Python solutions are also ok for me as far as it is Free, Libre and Open Source Software (FLOSS).

P.S.2. I think the solution should include the ci parameter of the lineplot function. Maybe I can have a for loop going over the list above and plot different confidence intervals on top of each other?

P.S.3. So far I have realized that putting the last line of the above code inside a for loop over the mentioned list of confidence intervals would plot them on top of each other:

for ii in [10, 50, 90]:
    ax = sns.lineplot(x="timepoint", y="signal", data=fmri, ci=ii)
        
                     Fig.2 - putting the plotting command in a for loop.                

However, I don't know how to control the hue/color-map of those transparent bands and don't know how to add a legend based on those values.

Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193
  • 1
    One solution which comes to my mind is to first compute the upper and lower limits for 50%, 60%, 70% and so on. Then, you plot them using continuous line plots. Finally you fill the areas between the adjacent lines to have a different shading for each confidence region. – Sheldore Jul 26 '19 at 09:55
  • @Sheldore that seems like brute force solution to me. I was thinking maybe I can plot different `ci` parameters using different error band style on top of each other somehow. – Foad S. Farimani Jul 26 '19 at 10:01
  • 1
    You can call the same plot with different `ci` definitely, using a for loop perhaps. – Sheldore Jul 26 '19 at 10:06
  • @Sheldore that actually worked. I just put the last line in a `for loop` and it plotted on top of each other. however, I can't control the style of the transparent band. maybe I could control the `hue` somehow? – Foad S. Farimani Jul 26 '19 at 10:36
  • @Sheldore I also don't know how to make the legend based on those `ci` values – Foad S. Farimani Jul 26 '19 at 10:58
  • 1
    [This](https://stackoverflow.com/questions/39500265/manually-add-legend-items-python-matplotlib) and several other similar posts (search for them) shows how to add legends manually. You may include the code you tried in your question so that people can provide solution for the legend problem – Sheldore Jul 26 '19 at 11:15

1 Answers1

0

I think I have found what I'm looking for:

import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
fmri = sns.load_dataset("fmri")

confidenceIntervals = [10, 30, 50, 70, 90]

colorPalette = sns.color_palette("ch:2.5,-.2,dark=.4", n_colors=len(confidenceIntervals))[::-1]

for jj, ii in enumerate(confidenceIntervals):
    ax = sns.lineplot(x="timepoint", y="signal", data=fmri, ci=ii, label=str(ii), 
                      color=colorPalette[jj])

ax.legend(fancybox=True, framealpha=1, shadow=True, borderpad=1)

and the result is:

             
               Fig.3 - final results with the Seaborn color palette and legend.                

more color options can be found here. Basically, the sns.lineplot function has a kwargs parameter which according to the documentation:

passes down arguments to plt.plot at draw time

Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193