Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn/Matplotlib: how to access line values in FacetGrid?

I'm trying to shade the area between two lines in a Seaborn FacetGrid. The fill_between method will do this, but I need to access the values of each line in each subplot to pass them in.

Here's my code:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

data = [{'Change': 0.0,  'Language': 'Algonquin',  'Type': 'Mother tongue',  'Year': '2011'}, {'Change': 0.0,  'Language': 'Algonquin',  'Type': 'Spoken at home',  'Year': '2011'}, {'Change': -21.32,  'Language': 'Algonquin',  'Type': 'Mother tongue',  'Year': '2016'}, {'Change': -34.84,  'Language': 'Algonquin',  'Type': 'Spoken at home',  'Year': '2016'}, {'Change': 0.0,  'Language': 'Atikamekw',  'Type': 'Mother tongue',  'Year': '2011'}, {'Change': 0.0,  'Language': 'Atikamekw',  'Type': 'Spoken at home',  'Year': '2011'}, {'Change': 5.41,  'Language': 'Atikamekw',  'Type': 'Mother tongue',  'Year': '2016'}, {'Change': 19.15,  'Language': 'Atikamekw',  'Type': 'Spoken at home',  'Year': '2016'}, {'Change': 0.0,  'Language': 'Blackfoot',  'Type': 'Mother tongue',  'Year': '2011'}, {'Change': 0.0,  'Language': 'Blackfoot',  'Type': 'Spoken at home',  'Year': '2011'}, {'Change': -1.4,  'Language': 'Blackfoot',  'Type': 'Mother tongue',  'Year': '2016'}, {'Change': 61.42,  'Language': 'Blackfoot',  'Type': 'Spoken at home',  'Year': '2016'}, {'Change': 0.0,  'Language': 'Carrier',  'Type': 'Mother tongue',  'Year': '2011'}, {'Change': 0.0,  'Language': 'Carrier',  'Type': 'Spoken at home',  'Year': '2011'}, {'Change': -20.38,  'Language': 'Carrier',  'Type': 'Mother tongue',  'Year': '2016'}, {'Change': -18.91,  'Language': 'Carrier',  'Type': 'Spoken at home',  'Year': '2016'}, {'Change': 0.0,  'Language': 'Chilcotin',  'Type': 'Mother tongue',  'Year': '2011'}, {'Change': 0.0,  'Language': 'Chilcotin',  'Type': 'Spoken at home',  'Year': '2011'}, {'Change': -13.82,  'Language': 'Chilcotin',  'Type': 'Mother tongue',  'Year': '2016'}, {'Change': 7.41,  'Language': 'Chilcotin',  'Type': 'Spoken at home',  'Year': '2016'}, {'Change': 0.0,  'Language': 'Cree languages',  'Type': 'Mother tongue',  'Year': '2011'}, {'Change': 0.0,  'Language': 'Cree languages',  'Type': 'Spoken at home',  'Year': '2011'}, {'Change': -11.52,  'Language': 'Cree languages',  'Type': 'Mother tongue',  'Year': '2016'}, {'Change': 6.57,  'Language': 'Cree languages',  'Type': 'Spoken at home',  'Year': '2016'}]

langs = pd.DataFrame(data)
g = sns.FacetGrid(langs, col='Language', hue='Type', col_wrap = 4, size=2)
g.map(plt.plot, 'Year', 'Change').set_titles('{col_name}')
g.set(xticks=[2011, 2016], yticks = [-40, 0, 70] )

This results in a chart like this: enter image description here

Now how do I access the values of each line? I'm guessing something with g.axes, but nothing in the docs is helping.

like image 601
robroc Avatar asked Sep 15 '17 22:09

robroc


People also ask

How do you use FacetGrid in Seaborn?

Plotting Small Multiples of Data SubsetsFacetGrid object takes a dataframe as input and the names of the variables that will form the row, column, or hue dimensions of the grid. The variables should be categorical and the data at each level of the variable will be used for a facet along that axis.

What is the primary goal when using FacetGrid in Seaborn?

FacetGrid() : FacetGrid class helps in visualizing distribution of one variable as well as the relationship between multiple variables separately within subsets of your dataset using multiple panels.

How do you plot multiple variables in Seaborn?

In Seaborn, we will plot multiple graphs in a single window in two ways. First with the help of Facetgrid() function and other by implicit with the help of matplotlib. data: Tidy dataframe where each column is a variable and each row is an observation.


2 Answers

Flat axes array of FacetGrid then get all lines of an axis object by ax.lines. Iterate over these lines with calls of get_xdata, get_ydata to request data of a line than do what you want with these data. Sample code:

...
for ax in g.axes.flat:
    print (ax.lines)
    for line in ax.lines:
        print (line.get_xdata())
        print (line.get_ydata())

Output of your code data:

[<matplotlib.lines.Line2D object at 0x10c5facc0>, <matplotlib.lines.Line2D object at 0x10c5fa940>]
['2011' '2016']
[  0.   -21.32]
['2011' '2016']
[  0.   -34.84]
[<matplotlib.lines.Line2D object at 0x10c39a160>, <matplotlib.lines.Line2D object at 0x10c5a4828>]
['2011' '2016']
[ 0.    5.41]
['2011' '2016']
[  0.    19.15]
[<matplotlib.lines.Line2D object at 0x10c5ff6d8>, <matplotlib.lines.Line2D object at 0x10c67c630>]
['2011' '2016']
[ 0.  -1.4]
['2011' '2016']
[  0.    61.42]
[<matplotlib.lines.Line2D object at 0x10c637358>, <matplotlib.lines.Line2D object at 0x10c65ada0>]
['2011' '2016']
[  0.   -20.38]
['2011' '2016']
[  0.   -18.91]
[<matplotlib.lines.Line2D object at 0x10c613668>, <matplotlib.lines.Line2D object at 0x10c6134e0>]
['2011' '2016']
[  0.   -13.82]
['2011' '2016']
[ 0.    7.41]
[<matplotlib.lines.Line2D object at 0x10c5ffd30>, <matplotlib.lines.Line2D object at 0x10c4f5dd8>]
['2011' '2016']
[  0.   -11.52]
['2011' '2016']
[ 0.    6.57]
like image 173
Serenity Avatar answered Nov 14 '22 23:11

Serenity


Extending on @Serenity's. Using line information get_xdata, get_ydata you can use fill_between like

g = sns.FacetGrid(langs, col='Language', hue='Type', col_wrap=4, size=3)
g.map(plt.plot, 'Year', 'Change').set_titles('{col_name}')
g.set(xticks=[2011, 2016], yticks = [-40, 0, 70] )
for ax in g.axes.flat:
    ax.fill_between(ax.lines[0].get_xdata().astype(int),
                    ax.lines[0].get_ydata(0), ax.lines[1].get_ydata(),
                    facecolor='#ffdec1')

enter image description here

like image 36
Zero Avatar answered Nov 14 '22 22:11

Zero