Discrete legend in seaborn heatmap plot

Well, there’s definitely more than one way to accomplish this. In this case, with only three colors needed, I would pick the colors myself by creating a LinearSegmentedColormap instead of generating them with cubehelix_palette. If there were enough colors to warrant using cubehelix_palette, I would define the segments on colormap using the boundaries option of … Read more

plotting value_counts() in seaborn barplot

In the latest seaborn, you can use the countplot function: seaborn.countplot(x=’reputation’, data=df) To do it with barplot you’d need something like this: seaborn.barplot(x=df.reputation.value_counts().index, y=df.reputation.value_counts()) You can’t pass ‘reputation’ as a column name to x while also passing the counts in y. Passing ‘reputation’ for x will use the values of df.reputation (all of them, not … Read more

countplot with normalized y axis per group

With newer versions of seaborn you can do following: import numpy as np import pandas as pd import seaborn as sns sns.set(color_codes=True) df = sns.load_dataset(‘titanic’) df.head() x,y = ‘class’, ‘survived’ (df .groupby(x)[y] .value_counts(normalize=True) .mul(100) .rename(‘percent’) .reset_index() .pipe((sns.catplot,’data’), x=x,y=’percent’,hue=y,kind=’bar’)) output Update: Also show percentages on top of barplots If you also want percentages, you can do … Read more

Matplotlib: avoiding overlapping datapoints in a “scatter/dot/beeswarm” plot

Extending the answer by @user2467675, here’s how I did it: def rand_jitter(arr): stdev = .01 * (max(arr) – min(arr)) return arr + np.random.randn(len(arr)) * stdev def jitter(x, y, s=20, c=”b”, marker=”o”, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, hold=None, **kwargs): return scatter(rand_jitter(x), rand_jitter(y), s=s, c=c, marker=marker, cmap=cmap, norm=norm, vmin=vmin, vmax=vmax, alpha=alpha, linewidths=linewidths, **kwargs) The stdev … Read more

Stop seaborn plotting multiple figures on top of one another

You have to start a new figure in order to do that. There are multiple ways to do that, assuming you have matplotlib. Also get rid of get_figure() and you can use plt.savefig() from there. Method 1 Use plt.clf() import seaborn as sns import matplotlib.pyplot as plt iris = sns.load_dataset(‘iris’) length_plot = sns.barplot(x=’sepal_length’, y=’species’, data=iris) … Read more

Line plot over bar plot using Seaborn – Line plot won’t render

The command plt.subplots(figsize = (10, 10)), indicates that you want to divide canvas and create the subplots on it. For your current requirement, you could do something like – ax = sns.barplot(x=’Submission Date’, y=’Count Handled’, data=df_cd) ax2 = ax.twinx() ax2.plot(ax.get_xticks(), df_cd.Rating) Here, you are creating the barplot first and adding the line plot over it … Read more