【问题标题】:2D plot using imshow - Nonlinear Schrödinger equation in 2+1 dimensions使用 imshow 的 2D 绘图 - 2+1 维非线性薛定谔方程
【发布时间】:2020-03-16 22:44:57
【问题描述】:

我一直在研究一个涉及 2+1 维非线性薛定谔方程的物理问题。我想为模拟的最后一步绘制取决于 x 和 y 的密度。本次模拟对应的数据可以在this h5 file找到。

我已经为情节编写了以下脚本

import matplotlib.pyplot as plt
import numpy as np
import h5py as h5

data = h5.File('groundstate_interacting_2D_n50u2.h5', 'r') 
dens = data['1']['norm_dens'] 
norma = data['2']['norm'] 
en_pot = data['3']['energyp'] 
en_kin = data['4']['energyk'] 
en_int = data['5']['energyint'] 
pot_chem = data['6']['potencialchem'] 
latticex = data['7']['x'] 
latticey = data['7']['y'] 
time    = data['7']['t']  
phireal = data['7']['phireal'] 
phiimag = data['7']['phiimag'] 

#computes the square of the module of the wave-function
distr = np.power(phireal[:,:,:],2) + np.power(phiimag[:,:,:],2) 

X, Y = np.meshgrid(latticex, latticey)

fig, ax = plt.subplots()
mdr = ax.imshow(distr[400,:,:], interpolation='gaussian',cmap='plasma')
ax.set_ylabel('Y',fontsize='large', fontweight='bold')
ax.set_xlabel('X',fontsize='large', fontweight='bold')
ax.grid(False)
fig.colorbar(mdr)

plt.show()

在我的问题中,x 和 y 都定义在包含 32 个点的格子 (latticex and latticey) 的范围 (-4.0, 4.0) 中。我想知道是否可以在我的图中显示相同的范围。我尝试了一些方法,但都没有奏效。

【问题讨论】:

    标签: python matplotlib 2d physics imshow


    【解决方案1】:

    由于您没有使用imshownorm 参数,您可以使用vminvmax 来指定颜色图的上限和下限。一个例子:

    import matplotlib.pyplot as plt
    import numpy as np
    
    a = np.outer(np.linspace(-10, 10, 100), np.linspace(-10, 10, 100))
    a = (a / a.max() - 0.5) * 5.0
    
    fig, ax = plt.subplots(1, figsize=(10,10))
    m = ax.imshow(a, cmap='plasma', vmin=-4.0, vmax=4.0)
    plt.show()
    

    没有vminvmax

    使用vminvmax

    这将反映在颜色条中,尽管为了简单起见我忽略了它们。

    【讨论】:

    • 感谢您的回复,@WilliamMiller。我尝试了您的示例,但仍有一些我想了解的内容,这就是我如何更改绘图中 x 和 y 中显示的范围。在我的情节中,它会自动将 x 和 y 从 0 设置为 30。在你的情节中,我在这里运行它,它已显示 x 和 y 从 0 到 100。就我而言,我希望 x 和 y 从 -4 到4. 这个可以吗?
    • @HeitorGalacian 当然,唯一的问题是您是否要修改数据以将其实际转换为 (-4,4) 空间,或者仅更改绘图上显示的限制就足够了?
    • 是的@William。我想要修改数据以便将其转换为(-4,4)空间并制作绘图。
    • @HeitorGalacian 最简单的方法是标准化distr的层,然后移动0.5并缩放8,类似于我为@987654337所做的@ 多于。 norm_distr = (distr[400,:,:] / (distr[400,0,:].max() - distr[400,0,:].min()) - 0.5) * 8 之类的东西 - 但请注意,这不是一个完整的空间变换,而是基于 distr 的第二个轴进行归一化;不保证矩形层。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    相关资源
    最近更新 更多