0

From this answer, I discovered how to reuse a plot.

This is the first plot

plot_depression, ax = plt.subplots() 
ax=sns.lineplot(x="time", 
            y="value", 
            hue = "Group",
            ci=90, err_style='bars',
            data=df_long)
ax.set(xlabel = "Time", ylabel = "Result")
ax.set_title('Trajectory of depression during the pandemic', size=20)

This is the second plot

plot_anxiety, ax = plt.subplots() 
ax=sns.lineplot(x="time", 
            y="value", 
            hue = "Group",
            ci=90, err_style='bars',
            data=df_long2)
ax.set(xlabel = "Time", ylabel = "Result")
ax.set_title('Trajectory of anxiety during the pandemic', size=20)

In any chunk, I can reuse these plots by calling plot_depression or plot_anxiety. However, when I try to combine these two plots using this post, the result is two empty plots

fig, ax = plt.subplots(1, 2)
plot_anxiety
plot_depression

enter image description here

Any help is appreciated. If the full df is needed, I can edit this question.

Luis
  • 1,388
  • 10
  • 30
  • 2
    In your code, you are creating 3 figures. The [question you linked to](https://stackoverflow.com/questions/41537457/how-to-reuse-plot-in-next-jupyter-cell) only works for 1 figure. The correct approach is to either create one figure with two subplots, or define functions that create the plots and that you can call twice. – JohanC Feb 03 '22 at 23:11

1 Answers1

4

the idea is to have 1 figure with 2 subplots. so that call to plt.subplots() should only be done once, and your plots need to reference the output of that.

this is the kind of thing you need to do:

fig, ax = plt.subplots(1,2)  # only do this 1x 

sns.lineplot(x="time", 
             y="value", 
             hue = "Group",
             ci=90, err_style='bars',
             data=df_long, ax=ax[0])  # use this as 1st plot in `fig`
ax[0].set(xlabel = "Time", ylabel = "Result")
ax[0].set_title('Trajectory of depression during the pandemic', size=20)

sns.lineplot(x="time", 
             y="value", 
             hue = "Group",
             ci=90, err_style='bars',
             data=df_long2, ax=ax[1])  # use this as 2nd plot in `fig`
ax[1].set(xlabel = "Time", ylabel = "Result")
ax[1].set_title('Trajectory of anxiety during the pandemic', size=20)

plt.show()
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
  • Thank you!! However, I need to create the first plot in a chunk and the second plot in another chunk. After that, I'll have to "merge" or "combine" these two different plots in another chunk. is that possible? Thank you again! – Luis Feb 03 '22 at 23:01
  • you can reference those plots using `ax[0]` and `ax[1]` anywhere else you need them. hopefully that helps. – mechanical_meat Feb 03 '22 at 23:07
  • Thank you. It helped. I'm almost there to adapt your code to multiple chunks. However, the plots became empty. [image](https://ibb.co/4jGTspn) This issue is probably due to some error in my coding. – Luis Feb 03 '22 at 23:43
  • 1
    yes put this line `fig, ax = plt.subplots(1,2)` at the top before the Seaborn stuff. – mechanical_meat Feb 04 '22 at 00:58