111

I upgraded from Python(x,y) 2.7.2.3 to 2.7.6.0 in Windows 7 (and was happy to see that I can finally type function_name? and see the docstring in the Object Inspector again) but now the plotting doesn't work as it used to.

Previously (Spyder 2.1.9, IPython 0.10.2, matplotlib 1.2.1), when I plotted this script, for instance, it would plot the subplots side-by-side in an interactive window:

enter image description here

Now (Spyder 2.2.5, IPython 1.2.0, Matplotlib 1.3.1) when I try to plot things, it does the subplots as tiny inline PNGs, which is a change in IPython:

tiny inline PNGs

So I went into options and found this:

graphics options

which seems to say that I can get the old interactive plots back, with the 4 subplots displayed side-by-side, but when I switch to "Automatic", and try to plot something, it does nothing. No plots at all.

If I switch this drop-down to Qt, or uncheck "Activate support", it only plots the first subplot, or part of it, and then stops:

enter image description here

How do I get the old behavior of 4 side-by-side subplots in a single figure that I can interact with?

endolith
  • 25,479
  • 34
  • 128
  • 192
  • I think this should go as a bug report to pythonxy. My naive guess is that they are using pyplot to handle all there plotting and getting bitten in the butt for it (which seems wrong, because they have their own qt widget). – tacaswell May 12 '14 at 13:39
  • 1
    @tcaswell Who is "they" and what would they use instead of pyplot? – endolith May 12 '14 at 13:58
  • They is the people who maintain pythonxy and they should be using the object oriented interface rather than the pyolot state machine interface – tacaswell May 12 '14 at 14:00
  • @tcaswell I'm using the pyplot state machine interface in my programs, but why would that work differently with one version of IPython than another? – endolith May 12 '14 at 14:03
  • I do not use pythonxy, so I have no idea what bits and pieces got updated under you and without seeing your code, can't even guess. – tacaswell May 12 '14 at 16:52
  • 1
    Can you reproduce this problem outside of pythonxy (just using straight ipython + qtconsole)? There is not enough information here to sort out what is going wrong. – tacaswell May 12 '14 at 17:10
  • 1
    I would guess it's a bug in Spyder (Spyder is the IDE, pythonxy is the distribution). – Thomas K May 12 '14 at 19:38
  • 1
    @tcaswell: Aha. It seems my script is producing matplotlib errors which are then sent somewhere other than the IPython console. They show up if I use the Qt console instead, and now that I look for it, they show up in Spyder's regular Console tab. If I make a more simple script, it works, whether state machine or object-oriented. I assumed I had to change some IPython settings because of the new inline plots. – endolith May 12 '14 at 23:25

6 Answers6

167

Change the backend to automatic:

Tools > preferences > IPython console > Graphics > Graphics backend > Backend: Automatic

Then close and open Spyder.

Alex Lamson
  • 479
  • 5
  • 14
Victor Arellano
  • 1,671
  • 1
  • 10
  • 4
  • 28
    The closing and opening is indeed necessary. Just wondering why this isn't the default value. – Mathias711 Feb 03 '16 at 11:57
  • 5
    Also, for anybody still not seeing their plots ( like I wasn't ), the plot window may be silently appearing_behind_ your console window ( at least it does in Spyder on Windows 10 ) so you may have to hunt around for your window. – Joshua Glazer Apr 18 '16 at 19:54
  • 3
    For those with trouble finding this, it's located at `python > preferences > IPython console > Graphics > Graphics backend > Backend: Automatic` – cameronroytaylor May 08 '17 at 19:44
  • 8
    Instead of restarting Spyder you can also just restart the kernel (either via clicking on the settings wheel in the console and then "Restart kernel" or via a keyboard shortcut (on Mac Cmd+. ) – Michael Mar 26 '18 at 01:06
  • 1
    I have no idea why, but changing to Automatic, restarting Spyder, then changing to back to Inline caused the inline plots to start displaying whenever I issue an input command in a loop. This is very helpful! – Josiah Yoder Jul 19 '18 at 14:46
  • @Michael just restarting the kernel didn't work for me (Linux), I had to restart Spyder. – jrh Dec 06 '18 at 16:14
61

You can quickly control this by typing built-in magic commands in Spyder's IPython console, which I find faster than picking these from the preferences menu. Changes take immediate effect, without needing to restart Spyder or the kernel.

To switch to "automatic" (i.e. interactive) plots, type:

%matplotlib auto

then if you want to switch back to "inline", type this:

%matplotlib inline

(Note: these commands don't work in non-IPython consoles)

See more background on this topic: Purpose of "%matplotlib inline"

Mike T
  • 41,085
  • 18
  • 152
  • 203
  • In Python 3.6, I obtain ``matplotlib inline File "", line 1 %matplotlib inline ^ SyntaxError: invalid syntax`` – Karlo Nov 08 '17 at 17:21
  • 2
    @Karlo that error is expected from a conventional "python" interpreter. This syntax is only valid with ipython or Spyder. – Mike T Nov 08 '17 at 20:15
  • OK, I was using it in Spyder but in the Python console. Indeed, it works in the IPython console. – Karlo Nov 09 '17 at 15:11
  • 3
    Is there a way to set it using a script? Namely to chose where is the output of the graph within a script? Thank You. – Royi Nov 11 '17 at 10:57
  • 1
    This appears not to work in Spyder, even though it's the way to do it in a normal IPython window. Using the menu preferences worked for me; not sure why this doesn't. – Turtles Are Cute Nov 14 '17 at 21:45
  • 1
    @Royi the only way to include IPython syntax in a file is to rename it with a `*.ipy` extension; see [this Q/A for details](https://stackoverflow.com/q/21541319) – Mike T Sep 05 '19 at 20:44
12

After applying : Tools > preferences > Graphics > Backend > Automatic Just restart the kernel enter image description here

And you will get Interactive Plot.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Archit Pandey
  • 167
  • 1
  • 7
5

As said in the comments, the problem lies in your script. Actually, there are 2 problems:

  • There is a matplotlib error, I guess that you're passing an argument as None somewhere. Maybe due to the defaultdict ?
  • You call show() after each subplot. show() should be called once at the end of your script. The alternative is to use interactive mode, look for ion in matplotlib's documentation.
J. Martinot-Lagarde
  • 3,280
  • 2
  • 15
  • 17
2

This is actually pretty easy to fix and doesn't take any coding:

1.Click on the Plots tab above the console. 2.Then at the top right corner of the plots screen click on the options button. 3.Lastly uncheck the "Mute inline plotting" button

Now re-run your script and your graphs should show up in the console.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Chris
  • 41
  • 1
  • 4
-3

For most mathematical coding, I use this website and their services as they offer examples for every subject and their support is super helpful:
https://labdeck.com/application-examples-screenshots/

If your want you graph to change by a variable amount then the code you want to use is

   import matplotlib.pyplot as plt
   import time
   vec1=[1, 2, 3, 4, 5]
   vec2py=[10, 12, 9, 11, 13]
   plt.show()
   axes = plt.gca()
   axes.set_xlim(0, 6)
   axes.set_ylim(5, 50)
   plt.xlabel('x - axis')
   plt.ylabel('y - axis')
   plt.title('Example 1')
   plt.grid()
   line,= axes.plot(vec1,vec2py,color='red',lw=1)
   for x in range(0,10):
      vec2py = [x + 2 for x in vec2py]
      line.set_ydata(vec2py)
      plt.draw()
      plt.pause(1e-17)
      time.sleep(0.5)
   plt.show()

You will have to change x for how many iterations of the graph you want and how long you want it to run for and also the +2 in the vec2py line for what variable amount you want to change it by. Naturally the code is a template and you can make any aesthetical changes. This code file is found under displaying dynamic graphs which is under python programming in the link above.

If you want to display a constant rely of information from a source, I'm not to sure how to do that but the website mention before does have an example, however it isn't in python but in a simplified form of C++.If you do want to see it then the link is https://labdeck.com/examples/dsp-ecg-processing/ecg-9-leads-graphs.pdf?01a96f&01a96f and its under ECG 9 Leads graphs in ECG on the link at the start. The graph can be shown as in a document or independently from the document.

PS this is for people who have the same question but not necessarily the same scenario as I think this will help more.