如何在 log-logsns.regplot中实现直线回归线

我试图在 Python 中重新创建这个用 R 创建的情节:

我试图在 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), ter_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但我无法使它工作。也许如果有人可以重新措辞,或者如果有人对如何前进有任何建议,那就太好了!

谢谢

3

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

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

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

下面的代码也changes大多数plt.调用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']),
            ter_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']),
                  ter_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

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(209)
将共享任务列表从MSOutlook导出到 Excel中
上一篇
FunciónquedevuelvaTruesiesvocalydelo contrarioFalse
下一篇

相关推荐

  • 3d大鱼吃小鱼:从3D阵列绘制3D散点图(3d scatter plot online)

    关于3d大鱼吃小鱼的问题,在3d scatter plot online中经常遇到,我目前正在尝试使用 3D 数组绘制 3D 散点图。我在网上找到的关于绘制 3D 散点图的内容看起来像ax.scatter3D(x,y,z),其中x,y,z都是 1D 数组。但是,在我的情况下,我正在使用 numpy 的…

    2022-12-21 02:41:07
    0 72 38

发表评论

登录 后才能评论

评论列表(84条)