Home > Net >  How to prevent jupyter from superimposing different plots on the same cell?
How to prevent jupyter from superimposing different plots on the same cell?

Time:10-13

I'm trying to plot two different graphs in different jupyter cells. They are given by:

import matplotlib.pyplot as plt
%matplotlib notebook

# Plotting background functions

nr_int = len(p)
plt.title("Background functions")
plt.grid()
plt.plot(r, m, label = "m")
plt.plot(r, bare, label = "naked")
plt.plot(r[:nr_int], p, label = "p")
plt.plot(r[:nr_int], rho, label = "rho")
plt.xlim(0, 2* radius)
plt.legend()

and

# Plotting the initial conditions

plt.title("Initial Conditions")
plt.grid()
plt.plot(r, F, label = "F")
plt.plot(r, S, label = "S")
plt.plot(r, H, label = "H")
plt.plot(r, Hamiltevol[0], label = "Hamiltonian constraint")
plt.xlim(3.87, 4.8)
plt.legend()

When I rotate the first cell, the graph is generated correctly. However, when I run the next cell, the output is <matplotlib.legend.Legend at 0x26d13362d60> and the plot appears in the output of the top cell, that is, the graphs are superimposed. How can I avoid this?

CodePudding user response:

Whenever you need a new plot, run plt.figure() before your plot commands, for example before plt.title.

  • Related