I have a few questions about what how to work with graphics, using Sympy.
My code:
JavaScript
x
4
1
from sympy import *
2
x, y = symbols("x y")
3
plot_implicit(Eq(x**2 + y**2, 4), (x, -3, 3), (y, -3, 3))
4
1) The graph is obtained stretched along the x axis.
How to make so that the curve looked like a circle?
2) How to add other elements to the chart. For example, the point O(0, 0) and the line y = x.
Advertisement
Answer
According to the docstring of plot.py, you can get the backend wrapper of the Matplotlib axes and figure that SymPy uses, through _backend
attribute, and then modify properties as any other Matplotlib objects. Check this example:
JavaScript
1
15
15
1
import matplotlib.pyplot as plt
2
import numpy as np
3
%matplotlib notebook
4
from sympy import *
5
6
x, y = symbols("x y")
7
hp = plot_implicit(Eq(x**2 + y**2, 4), (x, -3, 3), (y, -3, 3))
8
fig = hp._backend.fig
9
ax = hp._backend.ax
10
xx = yy = np.linspace(-3,3)
11
ax.plot(xx,yy) # y = x
12
ax.plot([0],[0],'o') # Point (0,0)
13
ax.set_aspect('equal','datalim')
14
fig.canvas.draw()
15
The Sympy Plot objects have append and extend methods that allows add a Plot object to other, but this don’t work (at least for me and using Jupyter).
Another option is use only Matplotlib:
JavaScript
1
11
11
1
import matplotlib.pyplot as plt
2
import numpy as np
3
4
fig, ax = plt.subplots(1,1)
5
xx,yy = np.linspace(-3,3), np.linspace(-3,3)
6
x,y = np.meshgrid(xx,yy)
7
ax.contour(x, y, (x**2+y**2-4), [0]);
8
ax.plot([0],[0],"o")
9
ax.plot(xx,yy)
10
ax.set_aspect('equal','datalim')
11