简体   繁体   English

将 Matplotlib 和 Sympy 的图放在一起

[英]Putting together plots of Matplotlib and Sympy

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知道可以通过以下方式访问 Sympy 的轴和图可能会有所帮助

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

Any help is really appreciated.任何帮助都非常感谢。

@ImportanceOfBeingErnest did the ground work for this answer here , in which he added 2 sympy plots to the same matplotlib axes. @ImportanceOfBeingErnest 在这里为这个答案做了基础工作,他在同一个 matplotlib 轴上添加了 2 个 sympy 图。

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()

@Ralph helped me to arrive at the below answer @Ralph 帮助我得出以下答案

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM