如何在loglog sns.regplot中实现直线回归?

2024-09-28 05:15:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在Python中重新创建使用R创建的绘图:

enter image description here

这就是我得到的:

enter image description here

这是我使用的代码:

from matplotlib.ticker import ScalarFormatter

fig, ax = plt.subplots(figsize=(10,8))

sns.regplot(x='Platform2',y='Platform1',data=duplicates[['Platform2','Platform1']].dropna(thresh=2), scatter_kws={'s':80, 'alpha':0.5})
plt.ylabel('Platform1', labelpad=15, fontsize=15)
plt.xlabel('Platform2', labelpad=15, fontsize=15)
plt.title('Sales of the same game in different platforms', pad=30, size=20)

ax.set_xscale('log')
ax.set_yscale('log')
ax.set_xticks([1,2,5,10,20])
ax.set_yticks([1,2,5,10,20])
ax.get_xaxis().set_major_formatter(ScalarFormatter())
ax.get_yaxis().set_major_formatter(ScalarFormatter())
ax.set_xlim([0.005, 25.])
ax.set_ylim([0.005, 25.])

plt.show()

我想我在这里绘制的对数值背后缺少一些概念知识。因为我没有改变数值本身,而是改变了图形的比例,所以我认为我做错了什么。当我试图改变价值观时,我没有成功

我想要的是显示回归线,就像R图中的回归线一样,还显示x轴和y轴上的0。绘图的对数性质不允许我在x轴和y轴上添加0限制。我找到了这个StackOverflow条目:LINK,但我无法使它工作。也许如果有人能重新措辞,或者有人对如何前进有任何建议,那就太好了

谢谢


Tags: log绘图getformatterpltax数值set
1条回答
网友
1楼 · 发布于 2024-09-28 05:15:17

Seaborn的regplot在线性空间(y ~ x)中创建一条直线,或者(使用logx=True)形式的线性回归。您的问题要求采用log(y) ~ log(x)形式的线性回归

这可以通过使用输入数据的log调用regplot来实现。 但是,这将更改显示数据的log的数据轴,而不是数据本身。使用特殊的记号格式设置器(利用值的幂),这些记号值可以再次转换为原始数据格式

请注意,对set_xticks()set_xlim()的调用都需要将它们的值转换为日志空间才能工作。需要删除对set_xscale('log')的调用

下面的代码还changesmostplt.调用ax.调用,并将ax作为参数添加到sns.regplot(..., ax=ax)

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

sns.set()
p1 = 10 ** np.random.uniform(-2, 1, 1000)
p2 = 10 ** np.random.uniform(-2, 1, 1000)
duplicates = pd.DataFrame({'Platform1': 0.6 * p1 + 0.4 * p2, 'Platform2': 0.1 * p1 + 0.9 * p2})

fig, ax = plt.subplots(figsize=(10, 8))

data = duplicates[['Platform2', 'Platform1']].dropna(thresh=2)
sns.regplot(x=np.log10(data['Platform2']), y=np.log10(data['Platform1']),
            scatter_kws={'s': 80, 'alpha': 0.5}, ax=ax)
ax.set_ylabel('Platform1', labelpad=15, fontsize=15)
ax.set_xlabel('Platform2', labelpad=15, fontsize=15)
ax.set_title('Sales of the same game in different platforms', pad=30, size=20)

ticks = np.log10(np.array([1, 2, 5, 10, 20]))
ax.set_xticks(ticks)
ax.set_yticks(ticks)
formatter = lambda x, pos: f'{10 ** x:g}'
ax.get_xaxis().set_major_formatter(formatter)
ax.get_yaxis().set_major_formatter(formatter)
lims = np.log10(np.array([0.005, 25.]))
ax.set_xlim(lims)
ax.set_ylim(lims)

plt.show()

example plot

要创建类似于R中示例的jointplot(要设置地物大小,请使用sns.jointplot(...., height=...),地物将始终为正方形):

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

sns.set()
p1 = 10 ** np.random.uniform(-2.1, 1.3, 1000)
p2 = 10 ** np.random.uniform(-2.1, 1.3, 1000)
duplicates = pd.DataFrame({'Platform1': 0.6 * p1 + 0.4 * p2, 'Platform2': 0.1 * p1 + 0.9 * p2})

data = duplicates[['Platform2', 'Platform1']].dropna(thresh=2)
g = sns.jointplot(x=np.log10(data['Platform2']), y=np.log10(data['Platform1']),
                  scatter_kws={'s': 80, 'alpha': 0.5}, kind='reg', height=10)

ax = g.ax_joint
ax.set_ylabel('Platform1', labelpad=15, fontsize=15)
ax.set_xlabel('Platform2', labelpad=15, fontsize=15)

g.fig.suptitle('Sales of the same game in different platforms', size=20)

ticks = np.log10(np.array([.01, .1, 1, 2, 5, 10, 20]))
ax.set_xticks(ticks)
ax.set_yticks(ticks)
formatter = lambda x, pos: f'{10 ** x:g}'
ax.get_xaxis().set_major_formatter(formatter)
ax.get_yaxis().set_major_formatter(formatter)
lims = np.log10(np.array([0.005, 25.]))
ax.set_xlim(lims)
ax.set_ylim(lims)
plt.tight_layout()
plt.show()

example of jointplot

相关问题 更多 >

    热门问题