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

Add density curve on the histogram

distplot has been removed: removed in a future version of seaborn. Therefore, alternatives are to use histplot and displot. sns.histplot import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import numpy as np df = pd.DataFrame(np.random.randn(100, 4), columns=list(‘ABCD’)) X = df[‘A’] sns.histplot(X, kde=True, bins=20) plt.show() sns.displot import pandas as pd import matplotlib.pyplot … Read more

Changing width of bars created with catplot or barplot

In my case, I didn’t have to define a custom function to change the width as suggested above (which btw didn’t work for me as all the bars were unaligned). I simply added the attribute dodge=False to the argument of the seaborn plotting function and this made the trick! e.g. sns.countplot(x=’x’, hue=”y”, data=data, dodge=False); See … Read more

Seaborn: annotate the linear regression equation

You can use coefficients of linear fit to make a legend like in this example: import seaborn as sns import matplotlib.pyplot as plt from scipy import stats tips = sns.load_dataset(“tips”) # get coeffs of linear fit slope, intercept, r_value, p_value, std_err = stats.linregress(tips[‘total_bill’],tips[‘tip’]) # use line_kws to set line label for legend ax = sns.regplot(x=”total_bill”, … Read more

How do I align gridlines for two y-axis scales?

I am not sure if this is the prettiest way to do it, but it does fix it with one line: import matplotlib.pyplot as plt import seaborn as sns import numpy as np import pandas as pd np.random.seed(0) fig = plt.figure() ax1 = fig.add_subplot(111) ax1.plot(pd.Series(np.random.uniform(0, 1, size=10))) ax2 = ax1.twinx() ax2.plot(pd.Series(np.random.uniform(10, 20, size=10)), color=”r”) # … Read more

How to change the color of the axis, ticks and labels

As a quick example (using a slightly cleaner method than the potentially duplicate question): import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) ax.plot(range(10)) ax.set_xlabel(‘X-axis’) ax.set_ylabel(‘Y-axis’) ax.spines[‘bottom’].set_color(‘red’) ax.spines[‘top’].set_color(‘red’) ax.xaxis.label.set_color(‘red’) ax.tick_params(axis=”x”, colors=”red”) plt.show() Alternatively [t.set_color(‘red’) for t in ax.xaxis.get_ticklines()] [t.set_color(‘red’) for t in ax.xaxis.get_ticklabels()]

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:

Seaborn multiple barplots

Yes you need to reshape the DataFrame: df = pd.melt(df, id_vars=”class”, var_name=”sex”, value_name=”survival rate”) df Out: class sex survival rate 0 first men 0.914680 1 second men 0.300120 2 third men 0.118990 3 first woman 0.667971 4 second woman 0.329380 5 third woman 0.189747 6 first children 0.660562 7 second children 0.882608 8 third children … Read more