问题
I would like to know how to put the following plots together in a same figure:
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))
It might be helpful to know that it is possible to access to the axes and figure of plots of Sympy by
fig = p2._backend.fig
ax = p2._backend.ax
Any help is really appreciated.
回答1:
@ImportanceOfBeingErnest did the ground work for this answer here, in which he added 2 sympy plots to the same matplotlib axes.
It's only a small alteration to this to get what you desire:
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()
回答2:
@Ralph helped me to arrive at the below answer
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 = plt.subplots(ncols=1)
ax.set_aspect('equal')
ax.arrow(0,0,0.7,0.7,head_width = 0.05, head_length=0.05,length_includes_head=True)
move_sympyplot_to_axes(p2, ax)
plt.show()
来源:https://stackoverflow.com/questions/60325325/putting-together-plots-of-matplotlib-and-sympy