如何将Matplotlib绘图添加到多个Seaborn绘图中

huangapple go评论55阅读模式
英文:

how to add a matplotlib plot to a multiple seaborn plot

问题

Sure, here's the translated code portion:

我使用seaborn的lmplot绘制了多个图我想要在这个图中添加一条x=y的线你能帮我解决这个问题吗

我的代码

sns.set_theme(style="white")
sns.lmplot(data=data, x='Target', y='Predicted', hue="Type", col='Model', height=5, legend=False, palette=dict(Train="g", Test="m"))
plt.plot([data.iloc[:,0].min(), data.iloc[:,0].max()], [data.iloc[:,0].min(), data.iloc[:,0].max()], "--", label="Perfect model")
plt.legend(loc='upper left')
plt.show()

And your output:

如何将Matplotlib绘图添加到多个Seaborn绘图中

英文:

i plot a multiple plot using seabor lmplot and i want to add a x=y line to this plot, can you help me to solve this problem?

my code :

sns.set_theme(style="white")
sns.lmplot(data=data, x='Target',y='Predicted', hue="Type",col='Model', height=5,legend=False, palette=dict(Train="g", Test="m"))
plt.plot([data.iloc[:,0].min(), data.iloc[:,0].max()], [data.iloc[:,0].min(), data.iloc[:,0].max()], "--", label="Perfect model")
plt.legend(loc='upper left')
plt.show()

and my output:
如何将Matplotlib绘图添加到多个Seaborn绘图中

i plot a multiple plot using seabor lmplot and i want to add a x=y line to this plot, can you help me to solve this problem?

my code :

sns.set_theme(style="white")
sns.lmplot(data=data, x='Target',y='Predicted', hue="Type",col='Model', height=5,legend=False, palette=dict(Train="g", Test="m"))
plt.plot([data.iloc[:,0].min(), data.iloc[:,0].max()], [data.iloc[:,0].min(), data.iloc[:,0].max()], "--", label="Perfect model")
plt.legend(loc='upper left')
plt.show()

and my output:
如何将Matplotlib绘图添加到多个Seaborn绘图中

答案1

得分: 0

The plt.plot() that you are using will only add a line to the last plot. To add a line to each subplot, you will need to use the axes for the lmplot() and plot a line for each of the subplots. Since I don't have your data, I used the standard penguins dataset to demonstrate this. Hope this helps...

data = sns.load_dataset('penguins') ## 我的数据
sns.set_theme(style="white")

g = sns.lmplot(data=data, x='bill_length_mm', y='bill_depth_mm', hue="species", col="sex", height=5, legend=False, palette=dict(Adelie="g", Chinstrap="m", Gentoo='orange'))

axes = g.fig.axes ## 获取所有子图的轴
for ax in axes: ## 对于每个子图,绘制线
    ax.plot([data.iloc[:,2].min(), data.iloc[:,2].max()], [data.iloc[:,3].min(), data.iloc[:,3].max()], "--", label="Perfect model")

plt.legend(loc='upper left')
plt.show()

如何将Matplotlib绘图添加到多个Seaborn绘图中

英文:

The plt.plot() that you are using will only add line to the last plot. Do add the line to each line, you will need to use the axes for the lmplot() and plot a line for each of the subplots. As I don't have your data, used the standard penguins dataset to show this. Hope this helps...

data = sns.load_dataset('penguins') ## My data
sns.set_theme(style="white")

g=sns.lmplot(data=data, x='bill_length_mm',y='bill_depth_mm', hue="species", col="sex", height=5,legend=False, palette=dict(Adelie="g", Chinstrap="m", Gentoo='orange'))

axes = g.fig.axes ## Get the axes for all the subplots
for ax in axes: ## For each subplot, draw the line
    ax.plot([data.iloc[:,2].min(), data.iloc[:,2].max()], [data.iloc[:,3].min(), data.iloc[:,3].max()], "--", label="Perfect model")

plt.legend(loc='upper left')
plt.show()

如何将Matplotlib绘图添加到多个Seaborn绘图中

huangapple
  • 本文由 发表于 2023年2月8日 17:04:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定