Python 使用 seaborn 对数记录 lmplot
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23913151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Log-log lmplot with seaborn
提问by sjdh
Can the function lmplot
from Seaborn plot on a log-log scale?
This is lmplot on a normal scale
lmplot
Seaborn 中的函数可以在对数尺度上绘制吗?这是正常规模的 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)
采纳答案by mwaskom
If you just want to plot a simple regression, it will be easier to use seaborn.regplot
. This seems to work (although I'm not sure where the y axis minor grid goes)
如果您只想绘制一个简单的回归,使用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})
If you need to use lmplot
for other purposes, this is what comes to mind, but I'm not sure what's happening with the x axis ticks. If someone has ideas and it's a bug in seaborn, I'm happy to fix it:
如果您需要lmplot
用于其他目的,这就是我想到的,但我不确定 x 轴刻度发生了什么。如果有人有想法并且这是 seaborn 中的错误,我很乐意修复它:
grid = sns.lmplot('x', 'y', data, size=7, truncate=True, scatter_kws={"s": 100})
grid.set(xscale="log", yscale="log")
回答by Paul H
Call the seaborn function first. It returns a FacetGrid
object which has an axes
attribute (a 2-d numpy array of matplotlib Axes
). Grab the Axes
object and pass that to the call to df1.plot
.
首先调用seaborn函数。它返回一个FacetGrid
具有axes
属性的对象(matplotlib 的二维 numpy 数组Axes
)。抓取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')