Home > OS >  How can I add my seaborn plots created inside a function to a matplotlib subplot object that I creat
How can I add my seaborn plots created inside a function to a matplotlib subplot object that I creat

Time:09-26

I create the subplot object like this:

fig, ax = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(18, 10))

and then call my custom function that inside it create a seaborn scatter plot like this:

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')

Inside the custom function I set various aesthetic properties and the plot the graph like this:

graph=sns.regplot(data=df, x="actual", y="predicted",ci=95,x_bins=40, scatter_kws={"color": "black",'s':50},
                  line_kws={"color": "black"})

CodePudding user response:

I see that in the line where you create the sns.regplot you do not specify any axes for it.

I dont know what else you do in you custom function but you could pass the subplot axis to your function like this:

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')

and then inside your function you could assign these axis to the seaborn.regplot like this:

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)
  • Related