_backend
属性获取 SymPy 使用的 Matplotlib 轴和图形的后端包装器,然后像其他 Matplotlib 对象一样修改属性。查看此示例: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()
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')
ax.contour(x, y, eval(str(a_symbolic_expression)), [0]);
。 - pankplt.ion()
并在结尾添加plt.show(block=True)
,第一种方法对我有效。(Python 3.6.2,matplotlib 1.1.1) - nekketsuuuax = hp._backend.ax[0]
来获取轴列表中的第一个轴。 - JohanCSympy的绘图函数有一个参数aspect_ratio=
,可以是元组(1,1)
,表示等比例。
它们还支持markers=
和annotations=
关键字。这些是字典列表,用于matplotlib的plot
和annotate
函数。
from sympy import symbols, Eq, plot_implicit
x, y = symbols("x y")
plot_implicit(Eq(x ** 2 + y ** 2, 4), (x, -3, 3), (y, -3, 3), aspect_ratio=(1, 1),
markers=[{'args': [0, 0], 'color': "blue", 'marker': "o", 'ms': 5},
{'args': [[-3, 3], [-3, 3]], 'color': "blue", 'ls': '-', 'lw': 2}],
annotations=[{'xy': (0, 0), 'text': " 0", 'ha': 'left', 'va': 'top', 'color': 'blue'},
{'xy': (2, 2), 'text': "x=y ", 'ha': 'right', 'va': 'bottom', 'color': 'blue'}])