2017-04-13 239 views
8

因此,它似乎在Ubuntu的Windows(Windows子系統爲Linux)人們建議我們需要使用Agg後端,只保存圖像,而不是顯示情節。在Ubuntu中顯示matplotlib圖(Linux子系統的Windows子系統)

import matplotlib 
matplotlib.use('Agg') # no UI backend 

import matplotlib.pyplot as plt 
import numpy as np 

t = np.arange(0.0, 2.0, 0.01) 
s = 1 + np.sin(2*np.pi*t) 
plt.plot(t, s) 
plt.title('About as simple as it gets, folks') 

#plt.show() 
plt.savefig("matplotlib.png") #savefig, don't show 

我們怎樣才能把它放到plt.show()實際顯示圖像的位置?我目前的選擇是重寫plot.show(),而不是隻保存在/ mnt/c/Users/james/plots /下的plot-148123456.png窗口中,只需打開瀏覽器窗口即可查看圖像。

我想我可以託管該文件夾並使用瀏覽器。

我的目標是能夠像上面的代碼一樣運行簡單的示例,而不需要將代碼更改爲ftp圖像等等。我只希望劇情出現在窗口中。

有沒有人想出了一個體面的方式來做到這一點?

+0

在[這個問題](http://stackoverflow.com/questions/40566837/no-plot-window-in-matplotlib-in-linux-shell-windows-10)答案表示,這將是可能的爲此目的使用xming。你也可以考慮在SuperUser中提出一個類似的問題(不要過多地關注matplotlib)。 – ImportanceOfBeingErnest

回答

8

好吧,所以我得到它的工作如下,但一些步驟可能沒有必要。我有Ubuntu的窗口上,安裝了水蟒蟒3.6

  1. 谷歌 「的Xming」(X11服務器)和下載(從SourceForge)/安裝/運行
  2. sudo apt-get install x11-apps
  3. export DISPLAY=localhost:0.0(添加到~/.bashrc使永久)
  4. sudo apt-get install gnome-calculator(獲得GTK)
  5. 編輯matplotlibrc和改變後端:到後端:TkAgg

matplotlibrc/home/james/anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc

注意GTKAgg沒有工作,所以第4步可能是毫無意義的,不知道。 gnome-calulator起作用,所以GTK似乎工作,但它說需要pygtk但pip install pygtk沒有工作。

不管怎麼說,畢竟是,這個代碼在WSL Ubuntu的運行工作作爲是:

import matplotlib.pyplot as plt 
import numpy as np 

t = np.arange(0.0, 2.0, 0.01) 
s = 1 + np.sin(2*np.pi*t) 
plt.plot(t, s) 

plt.title('About as simple as it gets, folks') 
plt.show() 

```

結果: enter image description here

我有一種感覺,這是更好通過Jupyter筆記本或其他東西完成,但很高興在Linux上的子系統上的Ubuntu for Windows上具有基本的命令行python matplotlib功能

編輯:今天我運行了下面的命令,安裝Qt5作爲後端,並且它與Qt5Agg作爲後端(vs TkAgg)正常工作。如果你正在運行的一些事情上WSL其他這就需要QT5

sudo apt-get update && sudo apt-get install qtbase5-dev

而且這可能是有幫助的,找到你的matplotlibrc,並命令提示符下鍵入:

python import matplotlib print(matplotlib.matplotlib_fname()) quit()

+2

我只需要在windows端安裝xming,並在ubuntu端安裝python-tk sudo apt-get install python-tk,然後在設置DISPLAY之後很好。 – AlistairH

+0

@AlistairH你把DISPLAY設置爲了什麼? – ScheissSchiesser

+0

'DISPLAY = localhost:0.0',如上面的答案。 – AlistairH

1

要獲得matplotlib在猛砸在Ubuntu與GTKAgg工作在Windows,我:

  1. 安裝VcXsrv Windows下如上所述(但事情應該只是用的Xming相同)
  2. 設置的顯示[export DISPLAY=localhost:0.0(添加到〜/ .bashrc中,使永久)
  3. 執行sudo pip uninstall matplotlib
  4. 其次sudo apt install python-matplotlib
  5. 更新matplotlibrc讀取backend : GTKAgg(而不是backend : agg
  6. 我也跑了sudo apt-get install python-gtk2-dev,但這可能沒有必要。

Uninstalling the pip-installed matplotlib and reinstalling it via apt似乎是必要的,因爲pip不包括運行GTK所需的C擴展,但apt版本的確如此。

+1

python文件的頂部:import matplotlib; matplotlib.use( 'GTKAgg');不要忘記啓動VcXsrc。我跳過了第6步。謝謝! – Jason

+0

@Jason:我更新了我的'matplotlibrc'(步驟5),以便默認使用'GTKAgg',而不是顯式設置'matplotlib use'屬性,但我喜歡你的變體。另外,關於驗證VcXsvr正在運行的好處。很高興我能幫上忙! – marisano

+0

請注意'mobaXtrem'內置了xserver,可以用來代替安裝'VcXsrv'。請參閱https://nickjanetakis.com/blog/using-wsl-and-mobaxterm-to-create-a-linux-dev-environment-on-windows – oak