python - 如何将在函数内创建的 seaborn 图添加到我在外部创建的 matplotlib 子图对象
问题描述
我像这样创建子图对象:
fig, ax = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(18, 10))
然后调用我的自定义函数,在其中创建一个像这样的 seaborn 散点图:
prediction_plot(real_val=y_test,predic_val=y_predict,ax_lim_low=0.1,ax_lim_high=1,majr_tick=0.1,mnr_tick=0.05,ax_label='Mean Value')
在自定义函数中,我设置了各种美学属性并绘制了如下图:
graph=sns.regplot(data=df, x="actual", y="predicted",ci=95,x_bins=40, scatter_kws={"color": "black",'s':50},
line_kws={"color": "black"})
解决方案
我看到在您创建 sns.regplot 的行中,您没有为它指定任何轴。
我不知道您在自定义函数中还做了什么,但您可以将子图轴传递给您的函数,如下所示:
prediction_plot(axis= ax, real_val=y_test,predic_val=y_predict,ax_lim_low=0.1,ax_lim_high=1,majr_tick=0.1,mnr_tick=0.05,ax_label='Mean Value')
然后在您的函数中,您可以将这些轴分配给 seaborn.regplot,如下所示:
graph=sns.regplot(data=df, x="actual", y="predicted",ci=95,x_bins=40, scatter_kws={"color": "black",'s':50},
line_kws={"color": "black"}, ax = ax)
推荐阅读
- regex - How to add spaces to any given string and maintain N chars?
- javascript - 使用 window.prompt() 在 java 脚本中创建一个数组
- vb.net - 使 checkbox.Checked = True 其对应的 PictureBox 被点击
- r - R plotly mesh3 通过原点的坐标线
- java - How to create multiWidget Form
- python - HaarCascade 训练:合并 .lst 文件
- flask-sqlalchemy - 如何对 flask-admin 数据库列执行操作以将结果存储在同一表的其他列中
- python - 使用自签名证书将 pip install 安装到私有 pypi 服务器时如何忽略引发的安全警告
- laravel - 如何配置 Laravel Passport 的 '/oauth/token' 速率限制?
- django - Markdown 模板无法正确呈现 HTML