英文:
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:
英文:
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()
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()
答案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()
英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论