![](/img/trans.png)
[英]Fitting a dataset with a straight line using polyfit on a log-log plot
[英]How to achieve a straight regression line in a log-log sns.regplot?
我正在嘗試在 Python 中重新創建用 R 創建的這個圖:
這是我得到的地方:
這是我使用的代碼:
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但我無法使它工作。 也許如果有人可以改寫它,或者如果有人對如何前進有任何建議,那就太好了!
謝謝!
Seaborn 的regplot
在線性空間( y ~ x
)中創建一條線,或者(使用logx=True
) y ~ log(x)
形式的線性回歸。 您的問題要求進行log(y) ~ log(x)
形式的線性回歸。
這可以通過使用輸入數據的log
調用regplot
來完成。 但是,這將更改顯示數據log
而不是數據本身的數據軸。 使用特殊的刻度格式器(利用值的力量),這些刻度值可以再次轉換為原始數據格式。
請注意,對set_xticks()
和set_xlim()
的調用都需要將它們的值轉換為日志空間才能工作。 需要刪除對set_xscale('log')
的調用。
下面的代碼也改變了大部分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']),
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()
要創建jointplot
類似於R中的示例(設置數字大小,使用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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.