Move seaborn plot legend to a different position

Building on @user308827’s answer: you can use legend=False in factorplot and specify the legend through matplotlib: import seaborn as sns import matplotlib.pyplot as plt sns.set(style=”whitegrid”) titanic = sns.load_dataset(“titanic”) g = sns.factorplot(“class”, “survived”, “sex”, data=titanic, kind=”bar”, size=6, palette=”muted”, legend=False) g.despine(left=True) plt.legend(loc=”upper left”) g.set_ylabels(“survival probability”) plt acts on the current axes. To get axes from a FacetGrid … Read more

Color by Column Values in Matplotlib

Imports and Data import numpy import pandas import matplotlib.pyplot as plt import seaborn seaborn.set(style=”ticks”) numpy.random.seed(0) N = 37 _genders= [‘Female’, ‘Male’, ‘Non-binary’, ‘No Response’] df = pandas.DataFrame({ ‘Height (cm)’: numpy.random.uniform(low=130, high=200, size=N), ‘Weight (kg)’: numpy.random.uniform(low=30, high=100, size=N), ‘Gender’: numpy.random.choice(_genders, size=N) }) Update August 2021 With seaborn 0.11.0, it’s recommended to use new figure level functions … Read more

plot different color for different categorical levels using matplotlib

Imports and Sample DataFrame import matplotlib.pyplot as plt import pandas as pd import seaborn as sns # for sample data from matplotlib.lines import Line2D # for legend handle # DataFrame used for all options df = sns.load_dataset(‘diamonds’) carat cut color clarity depth table price x y z 0 0.23 Ideal E SI2 61.5 55.0 326 … Read more

How to change the figure size of a seaborn axes or figure level plot

You can also set figure size by passing dictionary to rc parameter with key ‘figure.figsize’ in seaborn set method: import seaborn as sns sns.set(rc={‘figure.figsize’:(11.7,8.27)}) Other alternative may be to use figure.figsize of rcParams to set figure size as below: from matplotlib import rcParams # figure size in inches rcParams[‘figure.figsize’] = 11.7,8.27 More details can be … Read more

Plotting with seaborn using the matplotlib object-oriented interface

It depends a bit on which seaborn function you are using. The plotting functions in seaborn are broadly divided into two classes “Axes-level” functions, including regplot, boxplot, kdeplot, and many others “Figure-level” functions, including relplot, catplot, displot, pairplot, jointplot and one or two others The first group is identified by taking an explicit ax argument … Read more

Stacked Bar Chart with Centered Labels

The following method is more succinct, and easily scales. Putting the data into a pandas.DataFrame is the easiest way to plot a stacked bar plot. Using pandas.DataFrame.plot.bar(stacked=True), or pandas.DataFrame.plot(kind=’bar’, stacked=True), is the easiest way to plot a stacked bar plot. This method returns a matplotlib.axes.Axes or a numpy.ndarray of them. Since seaborn is just a … Read more