First, I will answer your more general question. The rules of the functions that you can pass to FacetGrid.map are:
- They should take massive inputs as positional arguments, with the first argument corresponding to the x axis and the second argument corresponding to the y axis (although, rather, on the second condition in the near future
- They should also take two keyword arguments:
color and label . If you want to use the hue variable, than they should be passed to the basic charting function, although you can just catch **kwargs and do nothing with them, if this does not apply to the specific plot you are doing. - When invoked, they must draw a plot on the "currently active" matplotlib axes.
There are times when your function draws a graph that looks right without taking x , y , position inputs. I think that basically what happens here when you use plt.plot . Then it may be easier to just call, for example, g.set_axis_labels("Date", "Stat") after using the map , which will correctly rename your axes. You can also do g.set(xticklabels=dates) to get more meaningful ticks.
There is also a more general function, FacetGrid.map_dataframe . The rules are similar here, but the function you pass must accept an input data file in the data parameter, and instead of accepting positional inputs like an array, a string corresponding to the variables in this data frame is required. At each iteration through the face, the function will be called with an input framework masked only by the values ββfor this combination of row , col and hue levels.
So, in your specific case, you need to write a function that we can call plot_by_date , which should look something like this:
def plot_by_date(x, y, color=None, label=None): ...
(I would be more useful for the body, but actually I do not know how much to do with dates and matplotlib). The end result is that when you call this function, it should display the current active axes. Then do
g.map(plot_by_date, "Date", "Stat")
And it should work, I think.
mwaskom
source share