matplotlib show() method does not open window

六眼飞鱼酱① 提交于 2019-12-10 13:12:52

问题


I'm using a mac and when I do the following with matplotlib:

import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import pylab as P

...
plt.plot(x,y)  
plt.show() <-- nothing happens
plt.savefig('figure.png') <-- works fine

So, plt.show does not open a window or anything while plt.savefig works fine.

What could be the problem?


回答1:


Pyplot will only pop up a figure window if

matplotlib.rcParams['interactive'] == True

This is the case if you:

  • have previous called plt.ion() in your script, or
  • equivalently, called matplotlib.interactive(True), or
  • started an ipython session with the --pylab option at the command line.

When interactive mode is off, you generally have to call plt.show() explicitly to make the figure window pop up. This is because we often want to call plot multiple times to draw various things before displaying the figure (which is a blocking call).


Edit (after the question was modified):

One reason for plt.show() not popping up a figure window is that you haven't activated an interactive backend. Check the output of plt.get_backend() - if it returns 'agg', for example, you have a non-interactive backend.

If this is your problem, you may add lines like

import matplotlib
matplotlib.use('MacOSX')

At the start of your script to specify the backend. This needs to be placed before any other matplotlib related imports.

To make such a change permanent, you can specify a different backend as default by modifying your matplotlib rcfile. The location of this file is found by calling matplotlib.matplotlib_fname().



来源:https://stackoverflow.com/questions/21835847/matplotlib-show-method-does-not-open-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!