How to add hatches to boxplots with sns.boxplot or sns.catplot

Iterate through each subplot / FacetGrid with for ax in g.axes.flat:. ax.patches contains matplotlib.patches.Rectangle and matplotlib.patches.PathPatch, so the correct ones must be used. Caveat: all hues must appear for each group in each Facet, otherwise the patches and hatches will not match. In this case, manual or conditional code will probably be required to correctly … Read more

How to do waffle charts in python? (square piechart)

I spent a few days to build a more general solution, PyWaffle. You can install it through pip install pywaffle The source code: https://github.com/gyli/PyWaffle PyWaffle does not use matshow() method, but builds those squares one by one. That makes it easier for customization. Besides, what it provides is a custom Figure class, which returns a … Read more

How to scale Seaborn’s y-axis with a bar plot

Considering your question mentions barplot I thought I would add in a solution for that type of plot also as it differs from the factorplot in @Jules solution. import matplotlib.pyplot as plt import seaborn as sns sns.set(style=”whitegrid”) xs = [“First”, “First”, “Second”, “Second”, “Third”, “Third”] hue = [“Female”, “Male”] * 3 ys = [1988, 301, … Read more

Subplot for seaborn boxplot

We create the figure with the subplots: f, axes = plt.subplots(1, 2) Where axes is an array with each subplot. Then we tell each plot in which subplot we want them with the argument ax. sns.boxplot( y=”b”, x= “a”, data=df, orient=”v” , ax=axes[0]) sns.boxplot( y=”c”, x= “a”, data=df, orient=”v” , ax=axes[1]) And the result is:

How do I create a multiline plot using seaborn?

Seaborn favors the “long format” as input. The key ingredient to convert your DataFrame from its “wide format” (one column per measurement type) into long format (one column for all measurement values, one column to indicate the type) is pandas.melt. Given a data_preproc structured like yours, filled with random values: num_rows = 20 years = … Read more

Bar labels in matplotlib/Seaborn

You can loop through the containers and call ax.bar_label(…) for each of them. Note that seaborn creates one set of bars for each hue value. The following example uses the titanic dataset and sets ci=None to avoid the error bars overlapping with the text (if error bars are needed, one could set a lighter color, … Read more

How to plot a mean line on a kdeplot between 0 and the y value of the mean

Update for the latest versions of matplotlib (3.3.4) and seaborn (0.11.1): the kdeplot with shade=True now doesn’t create a line object anymore. To get the same outcome as before, setting shade=False will still create the line object. The curve can then be filled with ax.fill_between(). The code below is changed accordingly. (Use the revision history … Read more