Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline figures in Spyder using multiple separate plt.plot

I found the Figure viewer in Spyder 4 very convenient for comparing data of the same kind, say, if the way of analysis is clear and it's just about comparing data. Still, for trying around, it's really uncomfortable, that Spyder is always opening a new figure for every line of plt.plot.

What I have now is the following:

import numpy as np
def Gauss(beta0, x):
    return beta0[0] * np.exp(-(x - beta0[1])**2 / (2 * beta0[2]**2))+ beta0[3]

plt.figure() 
plt.plot(Gauss([0.25,50,1.5,0],np.arange(100)))
plt.plot(Gauss([0.25,50,3,0],np.arange(100)))
plt.plot(Gauss([0.25,50,5,0],np.arange(100))) 
plt.show()

Running this gives me a nice plot with 3 lines of data 3 lines in 1 figure

In case, I might not now exactly, what data to plot beforehand, so I want to plot one line, look at the data, then "update" the figure with another line, and so on, the code lines would be separated:

plt.figure()
Out[11]: <Figure size 1800x1200 with 0 Axes><Figure size 1800x1200 with 0 Axes>

plt.plot(Gauss([0.25,50,1.5,0],np.arange(100)))
Out[12]: [<matplotlib.lines.Line2D at 0x2218c6c06d8>]

plt.plot(Gauss([0.25,50,3,0],np.arange(100)))
Out[13]: [<matplotlib.lines.Line2D at 0x2218c3891d0>]

plt.plot(Gauss([0.25,50,5,0],np.arange(100)))
Out[14]: [<matplotlib.lines.Line2D at 0x2218ba1a438>]

plt.show()

Then I get 3 separate figures with 1 line each, although I didn't explicitly open a new figure or close the last one.

3 Figures with one line each

Is there a way of preventing Spyder to open a new figure for every line of plt.plot, to give an output like in Fig 1?

Thank you, lepakk

like image 279
Lepakk Avatar asked Apr 25 '26 15:04

Lepakk


1 Answers

I think this is just a matter of how you run the code. If you run line by line what you describe will happen. If you highlight all the lines then run, you get everything in the same figure.

like image 131
user18433873 Avatar answered Apr 28 '26 06:04

user18433873



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!