在绘图中添加图形的缩放部分

问题描述 投票:0回答:1

我正在求解一组微分方程,我以某种方式能够在我的代码中显示一个额外的框架,但仍然无法获得图形。我不知何故无法将 DE 的 my 变量 $$\phi$$ 提取到放大的图形中。我正在显示代码:

import matplotlib.pyplot as plt
import scipy as sp
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()

如果有人能帮助我。

我在论坛上看到了示例答案,并花了一些时间来玩示例代码。我的代码有微分方程,我需要放大它们的部分,我不明白在放大的图中绘制我的 DE 变量。

python matplotlib plot zooming figure
1个回答
0
投票

我不确定这是不是你想要的。但我希望它有所帮助。

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

在这里更改坐标缩放图

# [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])

参考 - inset_axes

如果你想改变主要剧情中的某些内容,请在

ax

进行更改

但是在缩放图中,在

ax_in

处变化

例如,如果你想改变 x ticks

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

参考

© www.soinside.com 2019 - 2024. All rights reserved.