簡體   English   中英

Jupyter Notebook + Matplotlib; 在plot.show()上凍結

[英]Jupyter Notebook + Matplotlib; Freeze on plot.show()

目的

  • 使用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)

OUTPUT

在此處輸入圖片說明

調試

  • 顯然改變后端有幫助 ,但是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. 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)?

這是在Jupyter Notebook中還是類似的東西? 通常你不應該做

matplotlib.use('QT4Agg')
import matplotlib

按照這個順序,因為matplotlib不會在名稱空間中。 更改順序,然后重新啟動Jupyter內核或將所有這些都轉儲到.py文件中。

在Jupyter中,您應該可以致電

%matplotlib qt4

在導入matplotlib設置后端之前,而不是調用use()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM