In a pretty graphic, how can I get a plot with a fixed aspect ratio?

If I build a circle with this fragment

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 window with a picture like this

enter image description here

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

enter image description here

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 plotting an ellipse

enter image description here

What am I missing?

I use Jupyter, and the laptop server version is 4.1.0 and runs on: Python 2.7.11 | Anaconda 2.5.0 (64-bit) | (default, December 6, 2015, 18:08:32) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]

+6
source share
3 answers

now in September 2019 this code works:

 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 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() 
+1
source

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

 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 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 event loop (not needed in Jupyter) 

This gives: Dense axis with equal aspect ratio

+2
source

The help for plot_implicit mentions x_var and y_var -arguments. Using them allows you to manually set limits for the x and y axes. If you scale these limits appropriately, you can achieve an even aspect ratio.

 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)

0
source

Source: https://habr.com/ru/post/1246728/


All Articles