Ruggero Turra
Ruggero Turra

Reputation: 17740

seaborn FacetGrid: plot one category and all the categories

I have a pandas datataframe with some data, some columns are categories, in this case (isPassed) just a boolean.

I am plotting some histograms, I would like to plot all the entries and to superimpose the one where isPassed is True. For example:

g = sns.FacetGrid(df, col=col_quantity, hue="isPassed")
g = g.map(plt.hist, hist_quantity, bins=np.linspace(-5, 5, 50), normed=False)

This supeimpose, with different color, the histogram of the hist_quantity for entries with isPassed == 0 and for entries with isPassed == 1. As I said I would like to superimpose entries without any check on isPassed (True or False) and the one with isPassed == 1.

Of course there are many ways to do that, but I would like to do with with FacetGrid. One solution would be to duplicate the entries and to have isPassed == 2 for the duplicated ones and to plot the one with isPassed == 1 and isPassed == 2. I don't like this solution, the best solution would be to have the hue value to accept a callable, or an expression as pandas.DataFrame.query. Other solutions?

Upvotes: 0

Views: 616

Answers (1)

Ruggero Turra
Ruggero Turra

Reputation: 17740

Here my solution, I hope someone has a better one:

g = sns.FacetGrid(df, col=col_quantity)
def p(x, passed, **kwargs):
    bins = kwargs.get('bins', None)
    plt.hist(x, bins=bins, normed=False)
    plt.hist(x[passed == 1], bins=bins, normed=False)
g = g.map(p, hist_quantity, "isPassed", bins=np.linspace(-5, 5, 50))

Upvotes: 1

Related Questions