- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在子图中绘制 2 个 distplots 或散点图效果很好:
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
%matplotlib inline
# create df
x = np.linspace(0, 2 * np.pi, 400)
df = pd.DataFrame({'x': x, 'y': np.sin(x ** 2)})
# Two subplots
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(df.x, df.y)
ax1.set_title('Sharing Y axis')
ax2.scatter(df.x, df.y)
plt.show()
但是当我对 lmplot
而不是其他类型的图表执行相同操作时,我收到错误消息:
AttributeError: 'AxesSubplot' object has no attribute 'lmplot'
有没有办法并排绘制这些图表类型?
最佳答案
您会收到该错误,因为 matplotlib 及其对象完全不知道 seaborn 函数。
将您的轴对象(即 ax1
和 ax2
)传递给 seaborn.regplot
或者您可以跳过定义这些并使用 seaborn.lmplot
的 col
kwarg
使用相同的导入,预定义轴并使用 regplot
如下所示:
# create df
x = np.linspace(0, 2 * np.pi, 400)
df = pd.DataFrame({'x': x, 'y': np.sin(x ** 2)})
df.index.names = ['obs']
df.columns.names = ['vars']
idx = np.array(df.index.tolist(), dtype='float') # make an array of x-values
# call regplot on each axes
fig, (ax1, ax2) = plt.subplots(ncols=2, sharey=True)
sns.regplot(x=idx, y=df['x'], ax=ax1)
sns.regplot(x=idx, y=df['y'], ax=ax2)
使用 lmplot 需要您的 dataframe to be tidy .从上面的代码继续:
tidy = (
df.stack() # pull the columns into row variables
.to_frame() # convert the resulting Series to a DataFrame
.reset_index() # pull the resulting MultiIndex into the columns
.rename(columns={0: 'val'}) # rename the unnamed column
)
sns.lmplot(x='obs', y='val', col='vars', hue='vars', data=tidy)
关于python - 如何并排绘制 2 个 seaborn lmplots?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33049884/
如何设置 lmplot 不仅每个变量都有不同的色调,而且还有不同的标记? 例如,您如何根据这些点所属的“类别”获得不同的标记? import pandas as pd import seaborn a
seaborn 中的 lmplot 通过截距拟合回归模型。然而,有时我想拟合回归模型无截距,即通过原点回归。 例如: In [1]: import numpy as np ...: import
from cycler import cycler markers = cycler(marker=['*', 'o'] * 100) g = sns.lmplot(x='x', y='y', df,
我正在尝试使用 seaborn 线性模型图绘制随时间变化的值图,但出现错误 TypeError: invalid type promotion 我读到无法绘制 pandas 日期对象,但这看起来真的很
对于regplot,这是有效的: sns_reg_plot = sns.regplot(x="X", y="y", data=df) sns_reg_fig = sns_reg_plot.get_fi
这个问题在这里已经有了答案: How to set some xlim and ylim in Seaborn lmplot facetgrid (2 个答案) 关闭 4 年前。 大多数 seabo
我正在尝试在 Searbon lmplot 上添加标题。 ax = plt.axes() sns.lmplot(x, y, data=df, hue="hue", ax=ax) ax.set_titl
如何在 seaborn 中更改 lmplot 的图形大小? 这是我当前的代码,但显然 figsize 不被接受。 sns.lmplot(x="x", y="y", hue="category", da
Seaborn 中的函数 lmplot 可以在对数刻度上绘制吗?这是正常比例的 lmplot import numpy as np import pandas as pd import seaborn
在 seaborn lmplot 函数中,我们有 hue 参数,它会自动在右侧绘制一个颜色条。如何禁用/删除颜色条? 下面是生成颜色条的示例代码 df = pd.DataFrame({'x':[1,2
我试图绘制一个 PairGrid 在对角线上使用密度估计,在上三角部分和下三角中的成对线性回归模型部分。这是我的数据文件: df.head() 这是我的代码: g = sns.PairGrid(df,
我试图让我的seaborn情节看起来像这样: 如果我使用 lmplot 和 z 轴作为色调,我会得到以下结果: 图中的lmplot基本上就是我想要的,但是我需要右侧的颜色条而不是实际值。 我尝试使用热
在我的常规数据分析工作中,自从 seaborn 包可用后,我已经切换到使用 100% python。非常感谢这个美妙的包裹。但是,我想念的一个 excel 图表功能是在使用 lmplot() 函数时显
在子图中绘制 2 个 distplots 或散点图效果很好: import matplotlib.pyplot as plt import numpy as np import seaborn as
我正在尝试更改 seaborn 中 lmplot 标记的大小。我尝试将“s”或“size”作为参数传递,但它们都不起作用。 lm = sns.lmplot(x="totalX",y="NormI",
我想使用 Seaborn.lmplot 来分散 Dataframe 的“第 1 列” 与 “第 2 列” ,但根据 Seaborn 文档: Seaborn.lmplot(x, y, data, hue
查看lmplot documentation ,它显示 Parameters: x, y : strings, optional Input variables;
Jupyter 笔记本,使用 Python 3: import matplotlib.pyplot as plt import seaborn as sns %matplotlib inline sn
我想知道为什么 seaborn lmplot 和 regplot 只能选择做 logx。我经常使用线性拟合 log(y) ~ x 并且 Y 轴应始终采用对数刻度以显示相关性(作为惯例)。 这是可以在
我对 Python、pandas DataFrames 和 Seaborn 还很陌生。当我试图更好地理解 Seaborn,尤其是 sns.lmplot 时,我发现由相同数据构成的两个图形之间存在差异,
我是一名优秀的程序员,十分优秀!