[PYTHON] Can't change aspect_ratio with sympy.plotting? About the matter

Can't change aspect_ratio with sympy.plotting? About the matter

~~ I'm sorry if I misunderstood. ~~ I lacked my knowledge. We received a comment from @SaitoTsutomu and solved.

Start jupyter notebook. First of all, preparation.

from sympy.plotting import plot, plot_implicit, plot_parametric
from sympy import symbols, sin, cos, pi
x, y, t = symbols('x y t')

plot

plot(x**2, (x, -2, 2))

Then, $ y = x ^ 2 $ in the range of $ -2 \ leqq x \ leqq 2 $ is drawn.

Unknown.png

It is also possible to display graphs of multiple functions at the same time. Draw $ y = x ^ 2 $ and $ y = x $ in the same coordinate space.

plot(x**2, x, (x, -2, 2))

Unknown2.png

plot_implicit

You can also draw figures in the form of $ f (x, y) = 0 $ (so-called implicit functions). Next is $ x ^ 2 + y ^ 2-1 = 0 $, that is, a circle with a radius of $ 1 $ centered on the origin.

plot_implicit(x**2+y**2-1)

Unknown3.png

I'm concerned about the aspect ratio, but it's a circle. It seems that plot_implicit cannot set two or more expressions and draw multiple curves. Does the implicit function mean that plotting is troublesome?

plot_parametric

This is my favorite. If $ y = f (x) , the expression is limited. Therefore, it is a depiction of the figure represented by the parameter display. For example $ x = \cos t,\ y = \sin t\quad (0\leqq t\leqq 2\pi) $$ Is also a circle with a radius of $ 1 $ centered on the origin, but to plot this:

plot_parametric(cos(t), sin(t), (t, 0, 2*pi))

Unknown-2.png

You can draw multiple curves at the same time.

plot_parametric((cos(t), sin(t)), (cos(t)+1, sin(t)+1), (t, 0, 2*pi))

Unknown-3.png

It is also possible to set the parameter range individually.

plot_parametric((cos(t), sin(t), (t, 0, 2*pi)), (cos(t)+1, sin(t)+1, (t, 0, pi)))

Unknown-5.png

By the way, I'm sure some of you have been interested in it for a long time, but you can see an ellipse even though you should be plotting a circle. According to the official documentation

aspect_ratio : tuple of two floats or {‘auto’}

There is, but nothing changes even if this is set ...

plot_parametric((cos(t), sin(t)), (cos(t)+1, sin(t)+1), (t, 0, 2*pi), axis=False, aspect_ratio=(1, 1))

Unknown-4.png

I can confirm that the ʻaxis option is applied, but ʻaspect_ratio does not respond. I searched on Google, but it was only an English site, so I looked through it for a while, but I couldn't get a clear answer.

In the first place, sympy plot seems to mediate matplotlib, so forcibly change the matplotlib settings? There was an answer like this, but I thought it would be better to illustrate it with matplotlib from the beginning if I wanted to do it.

** kwargs ...

It's my opinion of an amateur who is neither a programmer nor anything, but even if I insert a keyword that I did not expect to be ** kwargs, no particular error occurs. I think it's a pretty dangerous specification ... I thought about spelling mistakes in ʻaspect_ratio, but I didn't say yes or no, so I wasn't even sure if I was aware of the option ʻaspect_ratio.

Solution

The screen size is different from aspect_ratio.

from sympy.plotting import plot_parametric
from sympy import symbols, sin, cos, pi
t = symbols('t')

import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (8,8)

plot_parametric(cos(t), sin(t), (t, 0, 2*pi))

Unknown6.png

It was displayed safely at $ 1: 1 $. Thank you, @SaitoTsutomu.

Recommended Posts

Can't change aspect_ratio with sympy.plotting? About the matter
Think about the minimum change problem
I can't install the package with pip.
About the matter that localhost: 4040 cannot be accessed after running Spark with Docker
About the matter that was worried about sampling error
About the test
Notes about with
About the queue
Change the time zone with Docker in Oracle Database
Extract the maximum value with pandas and change that value