如何按百分比增加图形的大小但保持原始纵横比? [英] How to increase the size of the figure by percentage but keep the original aspect ratio?

查看:56
本文介绍了如何按百分比增加图形的大小但保持原始纵横比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来绘制图形

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)

我阅读了类似问题的答案,他们使用选项 plt.figure(figsize=(20,10)).我想保持原来的方面(宽与长的比例),但将图形的大小增加一些百分比以获得更好的外观.

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?

我忘了在上面的代码中添加一行%config InlineBackend.figure_format ='svg'.不幸的是,当我在下面添加此行时,答案不起作用.

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.

推荐答案

首先,由 scatterplot()返回的对象是 Axes ,而不是图形. scatterplot()使用当前轴绘制图.如果没有当前轴,则matplotlib会在当前图形中自动创建一个.如果没有当前图形,则 matplotlib 会自动创建一个新图形.

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.

此图形的大小由 rcParams ['figure.figsize'] 中的值确定.因此,您应该创建一个具有与该变量之前调用图相同的长宽比的图形.

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.

例如,下面的代码创建的图形是默认图形的2倍.

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)

这篇关于如何按百分比增加图形的大小但保持原始纵横比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆