pythonparametric-equations

How to plot multiple parametric plots in a single figure?


I need to plot a family of parametric curves in a single figure for each alpha values as mentioned in the code

import numpy as np
from sympy import *  
from sympy.plotting import plot_parametric
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D


a=45
t = symbols('t')
for alpha in np.arange(0.5,3,.1):
    M=a*sqrt(cos(2*t)+sqrt(pow(alpha,4)+pow(sin(2*t),2)))
    x = M*cos(t)
    y = M*sin(t)
    plot_parametric(x, y, (t, 0, 2*pi))

The code returns a sequence of 2D plots for each alpha value. Instead, I want to plot the whole set of plots in one figure, something like this image attached

enter image description here

Any solution?


Solution

  • enter image description here

    from numpy import arange, cos, linspace, pi, sin, sqrt
    from matplotlib.pyplot import colorbar, Normalize, show, subplots
    from matplotlib.cm import ScalarMappable, viridis
    
    a=45
    t= linspace(0, 2*pi, 2001)
    
    norm = Normalize(vmin=0.5, vmax=3)
    cmap = viridis
    sm = ScalarMappable(cmap=cmap, norm=norm)
    
    fig, (ax_xy, ax_tM) = subplots(1, 2, figsize=(10, 4), constrained_layout=1)
    
    for alpha in arange(0.5,3,.1):
        color = cmap(norm(alpha))
        M=a*sqrt(cos(2*t)+sqrt(pow(alpha,4)+pow(sin(2*t),2)))
        x = M*cos(t)
        y = M*sin(t)
        ax_tM.plot(t, M, color=color)
        ax_xy.plot(x, y, color=color)
    colorbar(sm, aspect=40)
    show()