在同意情节中,如何获得具有固定宽高比的情节?

时间:2022-08-27 10:45:50

If I plot a circle with this snippet

如果我用这个片段绘制一个圆圈

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

I will get a figure window like this one

我会得到一个像这样的数字窗口

在同意情节中,如何获得具有固定宽高比的情节?

Now the aspect ratio is not what I was expecting because I see an ellipse instead of a circle. Moreover, if I change the aspect ratio of the window (dragging the bottom-right corner of the window) I get also a change in the aspect ratio of the plot... The following image is what I get after dragging the corner in order to see a circle:

现在宽高比不是我所期待的,因为我看到的是椭圆而不是圆形。此外,如果我改变窗口的纵横比(拖动窗口的右下角),我也得到了绘图的纵横比的变化...下面的图像是我按顺序拖动角落后得到的图像看一个圆圈:

在同意情节中,如何获得具有固定宽高比的情节?

I would like to get a plot like the one you get in Matlab when you set axis equal, see http://it.mathworks.com/help/matlab/creating_plots/aspect-ratio-for-2-d-axes.html when you plot an ellipse

我想得到一个类似你在Matlab中得到的图表,当你设置轴相等时,请参阅http://it.mathworks.com/help/matlab/creating_plots/aspect-ratio-for-2-d-axes.html当你绘制一个椭圆

在同意情节中,如何获得具有固定宽高比的情节?

What am I missing?

我错过了什么?

I am using Jupyter and the version of the notebook server is 4.1.0 and is running on: Python 2.7.11 |Anaconda 2.5.0 (64-bit)| (default, Dec 6 2015, 18:08:32) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]

我正在使用Jupyter,笔记本服务器的版本是4.1.0并且正在运行:Python 2.7.11 | Anaconda 2.5.0(64位)| (默认,2015年12月6日,18:08:32)[GCC 4.4.7 20120313(红帽4.4.7-1)]

2 个解决方案

#1


2  

I'm not sure if this is covered in Sympy's stable API, but you can extract matplotlib's figure and axis instance and the use standard matplotlib calls to change the appearance of your plot:

我不确定这是否包含在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)

This gives: 在同意情节中,如何获得具有固定宽高比的情节?

这给出了:

#2


0  

Within the help for plot_implicit, the x_var and y_var-arguments are mentioned. Using them allows you to manually set limits to the x and y axis. If you scale those limits appropriately, you can achieve an even aspect ratio.

在plot_implicit的帮助中,提到了x_var和y_var参数。使用它们可以手动设置x和y轴的限制。如果适当地缩放这些限制,则可以实现均匀的宽高比。

from sympy import *

x, y = symbols('x y')

scal = 3840/2400 # corresponds to your screen resolution
a = 1.05

p1 = plot_implicit(Eq(x**2+y**2,1),title='with xlim and ylim\n',\
                   xlim=(-1,1), ylim=(-1,1),aspect_ratio='equal')

p2 = plot_implicit(Eq(x**2+y**2,1),title='with x_var and y_var\n',\
                   x_var=(x,-a*scal,a*scal), y_var=(y,-a,a))

(My Sympy-version: 1.1.1)

(我的Sympy版本:1.1.1)

#1


2  

I'm not sure if this is covered in Sympy's stable API, but you can extract matplotlib's figure and axis instance and the use standard matplotlib calls to change the appearance of your plot:

我不确定这是否包含在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)

This gives: 在同意情节中,如何获得具有固定宽高比的情节?

这给出了:

#2


0  

Within the help for plot_implicit, the x_var and y_var-arguments are mentioned. Using them allows you to manually set limits to the x and y axis. If you scale those limits appropriately, you can achieve an even aspect ratio.

在plot_implicit的帮助中,提到了x_var和y_var参数。使用它们可以手动设置x和y轴的限制。如果适当地缩放这些限制,则可以实现均匀的宽高比。

from sympy import *

x, y = symbols('x y')

scal = 3840/2400 # corresponds to your screen resolution
a = 1.05

p1 = plot_implicit(Eq(x**2+y**2,1),title='with xlim and ylim\n',\
                   xlim=(-1,1), ylim=(-1,1),aspect_ratio='equal')

p2 = plot_implicit(Eq(x**2+y**2,1),title='with x_var and y_var\n',\
                   x_var=(x,-a*scal,a*scal), y_var=(y,-a,a))

(My Sympy-version: 1.1.1)

(我的Sympy版本:1.1.1)