Search code examples
pythonmatplotlibplotzoomingfigure

Adding a Zoomed Part of the Figure in a Plot


I am solving a set of differential equations, and I somehow able to show an additional frame in my code, but still unable to obtain the figure. I somehow couldn't extract the my variable of the DE, $$\phi$$ into the zoomed figure. I am showing the code:

import matplotlib.pyplot as plt
from scipy.integrate import odeint
import numpy as np

m = 0.5
g = 0
time = np.linspace(-100, 100, 2000)


def system(phi, t):        #differential equation function
    phi_1 = phi[0]
    phi_2 = phi[1]
    dphi1_dt = phi_2
    H = np.sqrt((8 * np.pi / 3) * (0.5 * (phi_2**2)) + (0.5 * (phi_1**2) * m**2) + (g * (phi_1**3)))
    dphi2_dt = (-3 * H * phi_2) - (phi_1 * m**2) - (3 * g * phi_1**2)
    dphi_dt = [dphi1_dt, dphi2_dt]
    return dphi_dt


init = [[-2, -2], [-1, -2], [0, -2], [1, -2], [2, -2], [-2, -2],
        [2, 2], [1, 2], [0, 2], [1, 2], [2, 2], [-2, 2], [-1, 2],
        [0, 2], [-3, -2], [-4, -2], [3, -2], [4, -2], [-3, 2],
        [-4, 2], [3, 2], [4, 2]]        #selected initial conditions
phi_2 = np.linspace(-0.25, 0.25, 100)        #Issue here
phi_1 = np.linspace(-0.25, 0.25, 100)        #Issue here
for i in init:
    phi = odeint(system, i, time)
    plt.plot(phi[:, 0], phi[:, 1])

sub_axes = plt.axes([.6, .6, .25, .25])        #here is the problem
sub_axes.plot(phi_1, phi_2, c='k')        #here is the problem
plt.xlabel("$\phi$", fontsize=12)
plt.ylabel("$d \phi/dt$", fontsize=12)
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.show()

If someone could help me out.

I have seen the example answers on the forum, and spent a time playing with the example codes. My code has differential equations, and I need to zoom their portion, I don't understand to plot my DE variables in the zoomed figure.


Solution

  • enter image description here

    import matplotlib.pyplot as plt
    from scipy.integrate import odeint
    import numpy as np
    
    m = 0.5
    g = 0
    time = np.linspace(-100, 100, 2000)
    fig, ax = plt.subplots(1, 1, sharex=True, figsize=(10, 10))
    ax_in = ax.inset_axes([0.7, 0.7, 0.3, 0.3])
    
    def system(phi, t):        #differential equation function
        phi_1 = phi[0]
        phi_2 = phi[1]
        dphi1_dt = phi_2
        H = np.sqrt((8 * np.pi / 3) * (0.5 * (phi_2**2)) + (0.5 * (phi_1**2) * m**2) + (g * (phi_1**3)))
        dphi2_dt = (-3 * H * phi_2) - (phi_1 * m**2) - (3 * g * phi_1**2)
        dphi_dt = [dphi1_dt, dphi2_dt]
        return dphi_dt
    
    
    init = [[-2, -2], [-1, -2], [0, -2], [1, -2], [2, -2], [-2, -2],
            [2, 2], [1, 2], [0, 2], [1, 2], [2, 2], [-2, 2], [-1, 2], [0, 2], [-3, -2], [-4, -2], [3, -2], [4, -2], [-3, 2],
            [-4, 2], [3, 2], [4, 2]]        #selected initial conditions
    # phi_2 = np.linspace(-0.25, 0.25, 100)        #Issue here
    # phi_1 = np.linspace(-0.25, 0.25, 100)        #Issue here
    for i in init:
        phi = odeint(system, i, time)
        ax.plot(phi[:, 0], phi[:, 1])
        ax_in.plot(phi[:, 0], phi[:, 1])
    
    # sub_axes = plt.axes([.6, .6, .25, .25])        #here is the problem
    # sub_axes.plot(phi_1, phi_2, c='k')        #here is the problem
    
    
    ax_in.set_xlim(-0.1, 0.1)
    ax_in.set_ylim(-0.1, 0.1)
    # ax_in.xaxis.set_ticks(np.arange(-0.1, 0.11, 0.1))
    ax.set_xlabel("$\phi$", fontsize=12)
    ax.set_ylabel("$d \phi/dt$", fontsize=12)
    plt.show()
    

    Change the coordinate zooming plot here

    # [0.7, 0.7, 0.3, 0.3] => [x, y, w, h]
    ax_in = ax.inset_axes([0.7, 0.7, 0.3, 0.3])
    

    Reference - inset_axes

    If you want to change something in the main plot, change at ax

    but in the zooming plot, change at ax_in

    For example, if you want to change x ticks

    ax_in.xaxis.set_ticks(np.arange(-0.1, 0.11, 0.1))
    

    enter image description here

    Reference