【问题标题】:Jupyter Notebook + Matplotlib; Freeze on plot.show()Jupyter 笔记本 + Matplotlib;冻结 plot.show()
【发布时间】:2017-05-22 09:03:51
【问题描述】:

目标

  • 使用 Matplotlib 绘制德克萨斯州

代码

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)

输出

调试

  • 显然是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)?

【问题讨论】:

标签: python matplotlib plot pygtk


【解决方案1】:

这是在 Jupyter Notebook 还是类似的东西中?通常你应该做不到

matplotlib.use('QT4Agg')
import matplotlib

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

在 Jupyter 中,你应该可以调用

%matplotlib qt4

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

【讨论】:

猜你喜欢
  • 2022-11-15
  • 2017-05-15
  • 1970-01-01
  • 2021-03-22
  • 2017-08-17
  • 2016-05-26
  • 2021-05-10
  • 2017-02-03
  • 1970-01-01
相关资源
最近更新 更多