Sympy 隐式绘图总是在 y=0 显示 x 轴
我正在尝试使用 sympy.implicit_plot
来绘制椭圆。我的椭圆中心不在原点,所以我设置了函数的参数 axis_center
,以便将其移动到我椭圆的正确位置。
举个例子,在我的图中,椭圆的中心在 (35, 35),而短轴长度是 10,我不需要显示 y 轴在 y=0 到 y=20 之间的部分,所以我设置了参数 y_var=(y, 20, 60)
,这样就只显示 y 轴在 [20, 60] 之间的部分。但我注意到,无论 y_var
的范围如何,x 轴总是停留在 y=0 的位置,它的作用只是去掉了 y 轴在 [0, 20] 之间的显示。
我希望 x 轴和 y 轴能在 y=20 的地方相交,就像 matplotlib 可以做到的那样。这有可能实现吗?
补充说明:我使用 sympy 是因为我需要绘制多个隐式函数以及散点数据。我在网上查找,发现 sympy 不能做散点图。所以,我先用 sympy 绘制曲线,然后再用这个函数转到 matplotlib 将 Matplotlib 和 Sympy 的图形结合起来
import numpy as np
import matplotlib.pyplot as plt
from sympy import plot_implicit, symbols
def get_rotation_matrix(deg):
rad = np.deg2rad(deg)
return np.array([[np.cos(rad), -np.sin(rad)],
[np.sin(rad), np.cos(rad)]])
def move_sympyplot_to_axes(p, ax):
backend = p.backend(p)
backend.ax = ax
backend._process_series(backend.parent._series, ax, backend.parent)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_position('zero')
ax.set_xlabel(ax.get_xlabel(), ha='right', x=1, labelpad=0)
ax.set_ylabel(ax.get_ylabel(), ha='right', va='top', y=1, labelpad=0, rotation=0)
plt.close(backend.fig)
a = 20
b = 10
u = get_rotation_matrix(60)
center= np.array((35, 35))
x, y = symbols('x y')
# ellipse function
f = ((x-center[0])/a)**2 + ((y-center[1])/b)**2 - 1
# plot
p = plot_implicit(f, (x, -20, 60), (y, 20, 60), aspect_ratio=(1,1), \
axis_center =center.tolist(), xlabel="x", ylabel="y", \
title="", show=False)
f, ax = plt.subplots()
move_sympyplot_to_axes(p, ax)
plt.show()
1 个回答
0
你的x轴一直停在零的位置,是因为在move_sympyplot_to_axes
这个函数里有这么一行代码:
ax.spines['bottom'].set_position('zero')
把那一行删掉,你的x轴就会出现在y轴的中间。如果你想让x轴从y=20开始,可以把那一行换成:
ax.spines['bottom'].set_position(('data', 20))