0

I am running python (3.8) via spyder (4.1.4) on a Windows laptop. I need to plot multiple series on a single graph, then do the usual things like adjusting axis limits, positioning the legend, etc. Spyder will not let me do all of this in one single plot.

For instance, the following produces two different plots:

plt.plot(seriesa,'o')
plt.plot(seriesb,'o')

and so does this:

fig = plt.figure()
ax = fig.add_subplot()
ax.plot(seriesa,'o')
ax.plot(seriesb,'o')

I can get both plots in one graph by doing the entire thing in one command:

fig = plt.figure() ; ax = fig.add_subplot() ; ax.plot(seriesa,'o') ;  ax.plot(seriesb,'o')

(which seems like a hack to me, but I'll do whatever works). But then I need to adjust the y axis limits, and the command

ax.set_ylim((0,2000))

has no effect on the plot. And the command

plt.ylim((0,2000))

opens up an entirely new plot.

I tried inline plotting too (unchecking the "Mute inline plotting" menu item), with no improvement.

How do I get the control I need with my plots?

bob.sacamento
  • 6,283
  • 10
  • 56
  • 115
  • How are you running your code in Spyder? By sending to the console one line at a time? – Carlos Cordoba May 09 '21 at 21:00
  • @CarlosCordoba Found an answer, which I have posted below. But I was trying to send one command at a time to the console. I did get some more success copying and pasting several lines at once. But that did not work well enough, and would not be an option all the time anyway. Like i said, found a solution. But I would be grateful for any further insights you might have. – bob.sacamento May 10 '21 at 17:39
  • You can also use cells to run several lines of code at once, as described [here](http://docs.spyder-ide.org/current/panes/editor.html#defining-code-cells). – Carlos Cordoba May 10 '21 at 23:06
  • 1
    @CarlosCordoba Did not know about that. Will have to give it a more detailed look. Thanks. – bob.sacamento May 11 '21 at 10:31

1 Answers1

1

In case anyone is interested, the solution is, before doing any plotting, issue the IPython "magic command":

In [1]: %matplotlib auto

This puts spyder in a state where plots are by default placed in independent windows, as when python is run in a regular shell. Could not find this in spyder documentation. Found it in a similar stack overflow question here.

Carlos Cordoba
  • 33,273
  • 10
  • 95
  • 124
bob.sacamento
  • 6,283
  • 10
  • 56
  • 115