Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

python - How to customize and add extra elements to Sympy plots?

I have a few questions about what how to work with graphics, using Sympy.

My code:

from sympy import *
x, y = symbols("x y")
plot_implicit(Eq(x**2 + y**2, 4), (x, -3, 3), (y, -3, 3))

1) The graph is obtained stretched along the x axis.

enter image description here

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.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

import matplotlib.pyplot as plt
import numpy as np
%matplotlib notebook
from sympy import *

x, y = symbols("x y")
hp = plot_implicit(Eq(x**2 + y**2, 4), (x, -3, 3), (y, -3, 3))
fig = hp._backend.fig
ax = hp._backend.ax
xx = yy = np.linspace(-3,3)
ax.plot(xx,yy) # y = x
ax.plot([0],[0],'o') # Point (0,0)
ax.set_aspect('equal','datalim')
fig.canvas.draw()

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:

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(1,1)
xx,yy = np.linspace(-3,3), np.linspace(-3,3)
x,y = np.meshgrid(xx,yy)
ax.contour(x, y, (x**2+y**2-4), [0]);
ax.plot([0],[0],"o")
ax.plot(xx,yy)
ax.set_aspect('equal','datalim')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...