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

FutureWarning: Pass the following variables as keyword args: x, y

Seaborn 0.12 With seaborn 0.12, the FutureWarning from seaborn 0.11 is now an TypeError. Only data may be specified as the first positional argument for seaborn plots. All other arguments must use keywords (e.g. x= and y=). This applies to all seaborn plotting functions. sns.*plot(data=penguins, x=”bill_length_mm”, y=”bill_depth_mm”) or sns.*plot(penguins, x=”bill_length_mm”, y=”bill_depth_mm”) sns.*plot(data=penguins.bill_length_mm) or sns.*plot(penguins.bill_length_mm) See … Read more

Color a scatter plot by Column Values

Imports and Data import numpy import pandas import matplotlib.pyplot as plt import seaborn as sns 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 … Read more

plot different color for different categorical levels

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 remove the duplicate legend when overlaying boxplot and stripplot

You can get what handles/labels should exist in the legend before you actually draw the legend itself. You then draw the legend only with the specific ones you want. import matplotlib.pyplot as plt import seaborn as sns import pandas as pd tips = sns.load_dataset(“tips”) sns.stripplot(x=”day”, y=”total_bill”, hue=”smoker”, data=tips, jitter=True, palette=”Set2″, dodge=True, linewidth=1, edgecolor=”gray”) # Get … 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

How to add a title to Seaborn Facet Plot

Updating slightly, with seaborn 0.11.1: Seaborn’s relplot function creates a FacetGrid and gives each subplot its own explanatory title. You can add a title over the whole thing: import seaborn as sns tips = sns.load_dataset(‘tips’) rp = sns.relplot(data=tips, x=’total_bill’, y=’tip’, col=”sex”, row=’smoker’, kind=’scatter’) # rp is a FacetGrid; # relplot is a nice organized way … Read more

Why am I getting a line shadow in a seaborn line plot?

There is line shadow showing the confidence interval, because the dataset contains multiple y(team_strikerate) values for each x(season) value. By default, sns.lineplot() will estimate the mean by aggregating over multiple y values at each x value. After aggregation, the mean of y values at each x value will be plotted as a line. The line … Read more