Akira
Akira

Reputation: 2870

How to increase the size of the figure by percentage but keep the original aspect ratio?

I have the following code to draw a figure

import pandas as pd
import urllib3
import seaborn as sns

decathlon = pd.read_csv("https://raw.githubusercontent.com/leanhdung1994/Deep-Learning/main/decathlon.txt", sep='\t')

fig = sns.scatterplot(data = decathlon,
                      x = '100m', y = 'Long.jump',
                      hue = 'Points', palette = 'viridis')

sns.regplot(data = decathlon,
            x = '100m', y = 'Long.jump',
            scatter = False)

I read answers for similar questions and they use the option plt.figure(figsize=(20,10)). I would like to keep the original aspect (the ration of width to length), but increase the size of the figure by some percentage for better look.

Could you please elaborate on how to do so?


I forgot to add a line %config InlineBackend.figure_format = 'svg' in above code. When I add this line below answer unfortunately does not work.

Upvotes: 2

Views: 731

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40667

First, the object returned by scatterplot() is an Axes, not a figure. scatterplot() uses the current axes to draw the plot. If there is no current axes, then matplotlib automatically creates one in the current figure. If there is not current figure, then matplotlib automatically creates a new figure.

The size of this figure is determined by the value in rcParams['figure.figsize']. Therefore, you should create a figure that has the same aspect ratio as defined in this variable before calling your plots.

For instance, the code below creates a figure that's 2x the size of the default figure.

tips = sns.load_dataset('tips')

fig = plt.figure(figsize= 2 * np.array(plt.rcParams['figure.figsize']))
ax = sns.scatterplot(data=tips, x="total_bill", y="tip", hue="day")
sns.regplot(data=tips, x="total_bill", y="tip", scatter=False, ax=ax)

Upvotes: 5

Related Questions