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?

4
  • How are you running your code in Spyder? By sending to the console one line at a time? Commented May 9, 2021 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. Commented May 10, 2021 at 17:39
  • You can also use cells to run several lines of code at once, as described here. Commented May 10, 2021 at 23:06
  • 1
    @CarlosCordoba Did not know about that. Will have to give it a more detailed look. Thanks. Commented May 11, 2021 at 10:31

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.