2016-04-08 114 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