Jupyter Notebook + Matplotlib;在plot.show()上冻结

时间:2017-01-07 19:08:16

标签: python matplotlib plot pygtk

目的

  • 使用Matplotlib绘制德克萨斯州的状态

CODE

import pandas as pd
import numpy as np
matplotlib.use('QT4Agg')
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import Normalize
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap


map = Basemap(projection='merc', lat_0 = 57, lon_0 = -135,
    resolution = 'h', area_thresh = 0.1,
    llcrnrlon=-106.65, llcrnrlat=25.83,
    urcrnrlon=-93.50, urcrnrlat=36.50)

map.drawcoastlines()
map.drawstates()
map.drawcountries()
map.fillcontinents(color = 'white')
map.drawmapboundary()


plt.show(block = False)

输出

enter image description here

DEBUG

  • 显然changing backend helps,但是matplotlib.use('QT4Agg')会产生以下错误"Gtk* backend requires pygtk to be installed"

  • 在线查看解决方案,请注意您必须安装PyGTK,但是,在安装时,我收到以下错误:Building PyGTK using distutils is only supported on windows.(在输入pip install PyGTK后)

  • 此外,尽管致电matplotlib.use('QT4Agg'),但收到错误matplotlib.use() has no effect because the backend has already been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot, or matplotlib.backends is imported for the first time.

问题

  1. 无法正确安装必要的软件包以防止matplotlib在连续循环中运行。我在这里错过了什么吗?有解决方法吗?
  2. 为什么,尽管在导入matplotlib之前调用matplotlib('QT4Agg'),我收到上面列出的错误(项目符号#3)?

1 个答案:

答案 0 :(得分:0)

这是在Jupyter笔记本中还是那种东西?通常你不应该这样做

matplotlib.use('QT4Agg')
import matplotlib
按顺序

,因为matplotlib不在命名空间中。更改顺序,然后重新启动Jupyter内核或将所有内容转储到 .py 文件中。

在Jupyter,你应该可以打电话

%matplotlib qt4

在导入 matplotlib 之前设置后端而不是调用 use()