2016-04-08 115 views
5

如果我繪製在這個片段中圓圈在sympy陰謀,我怎樣才能得到一個具有固定長寬比的情節?

from sympy import * 
x, y = symbols('x y')   
p1 = plot_implicit(Eq(x**2 +y**2, 1),aspect_ratio=(1.,1.)) 

我會得到這樣一個

enter image description here

一個數字窗口現在寬高比不是我所期待的,因爲我看到一個橢圓形而不是一個圓圈。此外,如果我改變了窗口的縱橫比(拖動窗口的右下角),我也得到了圖的縱橫比的變化......下圖是我拖拽角落後得到的圖像爲了看到一個圓:

enter image description here

我希望得到像你在Matlab中得到,當你設置axis equal的一個情節,看到http://it.mathworks.com/help/matlab/creating_plots/aspect-ratio-for-2-d-axes.html當你繪製一個橢圓

enter image description here

我錯過了什麼?

我正在使用Jupyter,並且筆記本服務器的版本是4.1.0並且正在運行:Python 2.7.11 | Anaconda 2.5.0(64位)| (默認,2015年12月6日,18:08:32)[GCC 4.4.7 20120313(Red Hat 4.4.7-1)]

+0

我試過'window2010'和'蟒蛇2.7.x'和' sympy 1.0',和你的問題一樣。我想知道'python 2.7'或'sympy 1.0'中是否有'bug'。我沒有'python 3.x',但是需要在那個版本上嘗試 –

回答

2

我不確定Sympy的穩定API是否涵蓋了這一點,但您可以提取matplotlib的身影和軸實例,並使用標準matplotlib調用來改變你的圖的外觀:

import matplotlib.pyplot as plt 
import sympy as sy 

x, y = sy.symbols('x y') 
p1 = sy.plot_implicit(sy.Eq(x**2 +y**2, 4)) 
fg, ax = p1._backend.fig, p1._backend.ax # get matplotib's figure and ax 

# Use matplotlib to change appearance: 
ax.axis('tight') # list of float or {‘on’, ‘off’, ‘equal’, ‘tight’, ‘scaled’, ‘normal’, ‘auto’, ‘image’, ‘square’} 
ax.set_aspect("equal") # 'auto', 'equal' or a positive integer is allowed 
ax.grid(True) 
fg.canvas.draw() 


plt.show() # enter matplotlib's event loop (not needed in Jupyter) 

這給: Tight Axis with equal aspect ratio

+0

你可以寫一下你的Python環境的版本嗎?我測試了你的代碼,並且得到了與你所附的不同的情節。謝謝。 –

+0

@AlessandroJacopson我正在使用Debian/Sid與matplotlib 1.5,ipython 2.4,Sympy 0.7.6.1和python 2.7/3.5。當使用pylab(ipython shell或--pylab中的'%pylab'作爲參數)時,我重新獲得了期望的結果,但是在不使用它時沒有。對於可能的原因,我有點無知。 – Dietrich

+0

在Sympy 1.0中不起作用... 函數show()在plot.py中調用self.plt.show()代替self.fig.show()。 由於快速解決方法/測試取代plt給無花果給予預期的結果。 – kimstik