使用seaborn进行双对数lmplot

问题描述 投票:0回答:4

Seaborn 的

lmplot
可以在双对数刻度上绘制吗?

这是带有线性轴的 lmplot:

import numpy as np
import pandas as pd
import seaborn as sns
x =  10**arange(1, 10)
y = 10** arange(1,10)*2
df1 = pd.DataFrame( data=y, index=x )
df2 = pd.DataFrame(data = {'x': x, 'y': y}) 
sns.lmplot('x', 'y', df2)

sns.lmplot('x', 'y', df2)

sns.lmplot [2]:

python seaborn
4个回答
77
投票

如果您只想绘制简单的回归,使用

seaborn.regplot
会更容易。这似乎有效(尽管我不确定 y 轴小网格去哪里)

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

x = 10 ** np.arange(1, 10)
y = x * 2
data = pd.DataFrame(data={'x': x, 'y': y})

f, ax = plt.subplots(figsize=(7, 7))
ax.set(xscale="log", yscale="log")
sns.regplot("x", "y", data, ax=ax, scatter_kws={"s": 100})

enter image description here

如果您需要将

lmplot
用于其他目的,这就是我想到的,但我不确定 x 轴刻度发生了什么。如果有人有想法并且这是seaborn中的一个错误,我很乐意修复它:

grid = sns.lmplot('x', 'y', data, size=7, truncate=True, scatter_kws={"s": 100})
grid.set(xscale="log", yscale="log")

enter image description here


13
投票

从(可能)任何seaborn图制作双对数图的最简单方法是:

plt.xscale('log')
plt.yscale('log')

示例中:

import numpy as np
import pandas as pd
import seaborn as sns
x =  10**np.arange(1, 10)
y = 10** np.arange(1,10)*2
df1 = pd.DataFrame( data=y, index=x )
df2 = pd.DataFrame(data = {'x': x, 'y': y}) 
sns.lmplot('x', 'y', df2)
plt.xscale('log')
plt.yscale('log')

Link to resulting image


6
投票

首先调用seaborn函数。它返回一个

FacetGrid
对象,该对象具有
axes
属性(matplotlib
Axes
的二维 numpy 数组)。获取
Axes
对象并将其传递给对
df1.plot
的调用。

import numpy as np
import pandas as pd
import seaborn as sns

x =  10**np.arange(1, 10)
y = 10**np.arange(1,10)*2
df1 = pd.DataFrame(data=y, index=x)
df2 = pd.DataFrame(data = {'x': x, 'y': y})

fgrid = sns.lmplot('x', 'y', df2)    
ax = fgrid.axes[0][0]
df1.plot(ax=ax)        

ax.set_xscale('log')
ax.set_yscale('log')

0
投票

通常,如果不显式使用 matplotlib,请在绘图函数的

subplot_kws
参数中使用
facet_kws
参数。

来自 seaborn 文档 https://seaborn.pydata.org/ generated/seaborn.lmplot.html

facet_kws:字典

FacetGrid 的关键字参数字典。

示例:

facet_kws={"subplot_kws": {"yscale": "log"}}
表示 log-scale 中的 y 轴。 当然,请确保处理的数据是积极的。

© www.soinside.com 2019 - 2024. All rights reserved.