2

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.

Vahid
  • 179
  • 1
  • 9

2 Answers2

2

@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()
jwalton
  • 5,286
  • 1
  • 18
  • 36
  • In fact, I wanted to place the arrow inside the circle. By the way, your answer learnt me how to do it by a minor modification. Thanks very much! For future readers, I will post my answer in the below, however, your answer is the accepted one. – Vahid Feb 20 '20 at 19:25
1

@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()
Vahid
  • 179
  • 1
  • 9