Sorted bar charts with pandas/matplotlib or seaborn

An easy trick might be to invert the y axis of your plot, rather than futzing with the data: s = pd.Series(np.random.choice(list(string.uppercase), 1000)) counts = s.value_counts() ax = counts.iloc[:10].plot(kind=”barh”) ax.invert_yaxis() Seaborn barplot doesn’t currently support horizontally oriented bars, but if you want to control the order the bars appear in you can pass a list … Read more

How to add a mean and median line to a Seaborn displot

Using FacetGrid directly is not recommended. Instead, use other figure-level methods like seaborn.displot seaborn.FacetGrid.map works with figure-level methods. seaborn: Building structured multi-plot grids Tested in python 3.8.11, pandas 1.3.2, matplotlib 3.4.3, seaborn 0.11.2 Option 1 Use plt. instead of ax. In the OP, the vlines are going to ax for the histplot, but here, the … Read more

Plotting errors bars from dataframe using Seaborn FacetGrid

When using FacetGrid.map, anything that refers to the data DataFrame must be passed as a positional argument. This will work in your case because yerr is the third positional argument for plt.errorbar, though to demonstrate I’m going to use the tips dataset: from scipy import stats tips_all = sns.load_dataset(“tips”) tips_grouped = tips_all.groupby([“smoker”, “size”]) tips = … Read more

Facetgrid change xlabels

You can access the individual axes of the FacetGrid using the axes property, and then use set_xlabel() on each of them. For example: import seaborn as sns import matplotlib.pyplot as plt sns.set(style=”ticks”, color_codes=True) tips = sns.load_dataset(“tips”) g = sns.FacetGrid(tips, col=”time”, hue=”smoker”) g = g.map(plt.scatter, “total_bill”, “tip”, edgecolor=”w”) g.axes[0,0].set_xlabel(‘axes label 1’) g.axes[0,1].set_xlabel(‘axes label 2’) plt.show() Note … Read more

Seaborn factor plot custom error bars instead of bootstrapping

Tested in python 3.8.12, pandas 1.3.4, matplotlib 3.4.3, seaborn 0.11.2 You could do something like import seaborn as sns import matplotlib.pyplot as plt tips = sns.load_dataset(“tips”) tip_sumstats = (tips.groupby([“day”, “sex”, “smoker”]) .total_bill .agg([“mean”, ‘sem’]) .reset_index()) def errplot(x, y, yerr, **kwargs): ax = plt.gca() data = kwargs.pop(“data”) data.plot(x=x, y=y, yerr=yerr, kind=”bar”, ax=ax, **kwargs) g = sns.FacetGrid(tip_sumstats, … Read more

Is it possible to add hatches to each individual bar in seaborn.barplot?

You can loop over the bars created by catching the AxesSubplot returned by barplot, then looping over its patches. You can then set hatches for each individual bar using .set_hatch() Here’s a minimal example, which is a modified version of the barplot example from here. import matplotlib.pyplot as plt import seaborn as sns # Set … Read more

heatmap-like plot, but for categorical variables in seaborn

You can use a discrete colormap and modify the colorbar, instead of using a legend. value_to_int = {j:i for i,j in enumerate(pd.unique(df.values.ravel()))} # like you did n = len(value_to_int) # discrete colormap (n samples from a given cmap) cmap = sns.color_palette(“Pastel2”, n) ax = sns.heatmap(df.replace(value_to_int), cmap=cmap) # modify colorbar: colorbar = ax.collections[0].colorbar r = colorbar.vmax … Read more

Plotting multiple boxplots in seaborn

Consider first assigning a grouping column like Trial for each corresponding dataframe, then pd.concat your dataframes, and finally pd.melt the data for a indicator/value long-wise dataframe before plotting with seaborn. Below demonstrates with random data: import pandas as pd import numpy as np from matplotlib import pyplot as plt import seaborn as sns np.random.seed(44) # … Read more