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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:39:27  来源:igfitidea点击:

Log-log lmplot with seaborn

pythonseaborn

提问by sjdh

Can the function lmplotfrom Seaborn plot on a log-log scale? This is lmplot on a normal scale

lmplotSeaborn 中的函数可以在对数尺度上绘制吗?这是正常规模的 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('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})

enter image description here

在此处输入图片说明

If you need to use lmplotfor 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")

enter image description here

在此处输入图片说明

回答by Paul H

Call the seaborn function first. It returns a FacetGridobject which has an axesattribute (a 2-d numpy array of matplotlib Axes). Grab the Axesobject 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')