簡體   English   中英

在Sympy繪圖中,如何獲得具有固定長寬比的繪圖?

[英]In sympy plotting, how can I get a plot with a fixed aspect ratio?

如果我用此代碼段繪制一個圓

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

我會得到一個像這樣的數字窗口

在此處輸入圖片說明

現在長寬比不是我所期望的,因為我看到的是橢圓而不是圓形。 此外,如果更改窗口的縱橫比(拖動窗口的右下角),則繪圖的縱橫比也將發生變化...下圖是按順序拖動角后得到的圖像看一個圓圈:

在此處輸入圖片說明

我想得到一種像將axis equal設為axis equal時在Matlab中得到的圖,請參閱http://it.mathworks.com/help/matlab/creating_plots/aspect-ratio-for-2-d-axes.html當繪制橢圓時

在此處輸入圖片說明

我想念什么?

我正在使用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)]

我不確定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)

這給出: 縱橫比相等的緊軸

現在在2019年9月,此代碼有效:

import matplotlib.pyplot as plt
import sympy

x, y = sympy.symbols('x y')

plt.ion() #interactive on 

p1 = sympy.plot_implicit(sympy.Eq(x**2 +y**2, 4), block = False)

fg, ax = p1._backend.fig, p1._backend.ax  # get matplotib's figure and axes

# 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)
plt.ioff() #interactive off
plt.show()

plot_implicit的幫助中, plot_implicitx_vary_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))

(我的Sympy版本:1.1.1)

暫無
暫無

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

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