我想知道如何将以下图放在同一图中:

import matplotlib.pyplot as plt
from sympy.plotting.plot import plot_parametric
from sympy import *
from sympy.abc import x,y,z
p1 = plt.arrow(0,0,0.5,0.5,head_width = 0.05, head_length=0.05,length_includes_head=True)
p2 = plot_parametric(cos(x),sin(x),(x,0,2*pi))


知道可以通过以下方式访问Sympy的坐标轴和图形可能会有所帮助

fig = p2._backend.fig
ax = p2._backend.ax


任何帮助都非常感谢。

最佳答案

@ImportanceOfBeingErnest为此答案here做了基础工作,在其中他向相同的matplotlib轴添加了2个sympy图。

只是为了得到您想要的东西而做的很小的改动:

import matplotlib.pyplot as plt

from sympy.plotting.plot import plot_parametric
from sympy import *
from sympy.abc import x,y,z

def move_sympyplot_to_axes(p, ax):
    backend = p.backend(p)
    backend.ax = ax
    # Fix for > sympy v1.5
    backend._process_series(backend.parent._series, ax, backend.parent)
    backend.ax.spines['right'].set_color('none')
    backend.ax.spines['bottom'].set_position('zero')
    backend.ax.spines['top'].set_color('none')
    plt.close(backend.fig)

p2 = plot_parametric(cos(x), sin(x), (x, 0, 2*pi), show=False)

fig, (ax, ax2) = plt.subplots(ncols=2)

ax.arrow(0,0,0.5,0.5,head_width = 0.05, head_length=0.05,length_includes_head=True)
move_sympyplot_to_axes(p2, ax2)

plt.show()

关于python - 将Matplotlib和Sympy的图放在一起,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60325325/

10-16 00:56