Skip to content Skip to sidebar Skip to footer

Plotting Time Series Using Seaborn Facetgrid

I have a DataFrame (data) with a simple integer index and 5 columns. The columns are Date, Country, AgeGroup, Gender, Stat. (Names changed to protect the innocent.) I would like

Solution 1:

I'll answer your more general question first. The rules for functions that you can pass to FacetGrid.map are:

  • They must take array-like inputs as positional arguments, with the first argument corresponding to the x axis and the second argument corresponding to the y axis (though, more on the second condition shortly
  • They must also accept two keyword arguments: color, and label. If you want to use a hue variable than these should get passed to the underlying plotting function, though you can just catch **kwargs and not do anything with them if it's not relevant to the specific plot you're making.
  • When called, they must draw a plot on the "currently active" matplotlib Axes.

There may be cases where your function draws a plot that looks correct without taking x, y, positional inputs. I think that's basically what's going on here with the way you're using plt.plot. It can be easier then to just call, e.g., g.set_axis_labels("Date", "Stat") after you use map, which will rename your axes properly. You may also want to do g.set(xticklabels=dates) to get more meaningful ticks.

There is also a more general function, FacetGrid.map_dataframe. The rules here are similar, but the function you pass must accept a dataframe input in a parameter called data, and instead of taking array-like positional inputs it takes strings that correspond to variables in that dataframe. On each iteration through the facets, the function will be called with the input dataframe masked to just the values for that combination of row, col, and hue levels.

So in your specific case, you'll need to write a function that we can call plot_by_date that should look something like this:

defplot_by_date(x, y, color=None, label=None):

    ...

(I'd be more helpful on the body, but I don't actually know how to do much with dates and matplotlib). The end result is that when you call this function it should plot on the currently-active Axes. Then do

g.map(plot_by_date, "Date", "Stat")

And it should work, I think.

Post a Comment for "Plotting Time Series Using Seaborn Facetgrid"