使用Matplotlib的2D映射图的问题

时间:2019-04-03 06:08:10

标签: python matplotlib

我正在绘制能量和密度分布的2D图像。映射中始终会出现一些轻微的未对齐情况,其中在绘图期间,第一个“列”似乎会移至最后一列。

我具有数据测试文件的附加链接。 AuthenticationCallback

这是情节:Data files

有什么可以防止这种情况的吗?

绘图中的部分代码如下:

import numpy as np
import matplotlib.pyplot as plt
import pylab as pyl
import scipy.stats as ss
import matplotlib.ticker as ticker
import matplotlib.transforms as tr

#%matplotlib inline

pi = 3.1415
n = 5e24 # density plasma
m = 9.109e-31
eps = 8.85e-12
e = 1.6021725e-19
c = 3e8
wp=np.sqrt(n*e*e/(m*eps))
kp = np.sqrt(n*e*e/(m*eps))/c #plasma wavenumber

case=400 
## decide on the target range of analysis for multiples
start= 20500
end  = 21500
gap = 1000


## Multiples plots
def target_range (start, end, gap):
    while start<= end:
        yield start
        start += gap

for step in target_range(start, end, gap):

    fdata =np.genfromtxt('./beam_{}'.format(step)).reshape(-1,6)

    ## dimension, dt, and superpaticle
    xBoxsize = 50e-6 #window size
    yBoxsize = 80e-6 #window size

    xbind = 10
    ybind = 1

    dx = 4e-8 #cell size
    dy = 4e-7 #cell size
    dz = 1e-6 #assume to be same as dy

    dt = 1.3209965456e-16
    sptcl = 1.6e10 
    xsub = 0e-6 
    xmax = dt*step*c  
    xmin = xmax - xBoxsize 

    ysub = 1e-7
    ymin = ysub #to make our view window
    ymax = yBoxsize - ysub

    xbins =  int((xmax - xmin)/(dx*xbind))
    ybins =  int((ymax - ymin)/(dy*ybind))
    #zbins =  int((zmax - zmin)/dz) #option for 3D

    # To make or define "data_arr" as a matrix with 2D array size 'xbins x ybins'
    data_arr = np.zeros((2,xbins,ybins), dtype=np.float)


    for line in fdata:
        x = int((line[0]-xmin)/(dx*xbind))
        y = int((line[1]-ymin)/(dy*ybind))
        #z = int((line[2]-zmin)/dz)

        if x >= xbins: x = xbins - 1
        if y >= ybins: y = ybins - 1
        #if z >= zbins: z = zbins - 1
data_arr[0, x, y] = data_arr[0,x, y] + 1  #cummulative adding up the number of particles

        energy_total = np.sqrt(1+ line[2]*line[2]/(c*c)+line[3]*line[3]/(c*c))/0.511

        data_arr[1, x, y] += energy_total 
        #array 1 tells us the energy while array 0 tells us the particles


## make average energy , total energy/particle number 
    np.errstate(divide='ignore',invalid='ignore')
    en_arr = np.true_divide(data_arr[1],data_arr[0]) # total energy/number of particles
    en_arr[en_arr == np.inf] = 0
    en_arr = np.nan_to_num(en_arr)
    en_arr = en_arr.T   

    ## This part is real density of the distribution    
    data_arr[0]= data_arr[0] * sptcl/dx/dy   #in m-3
    d = data_arr[0].T

    ## Plot and save density and energy distribution figures

    den_dist=plt.figure(1)

    plt.imshow(d,origin='lower', aspect = 'auto',cmap =plt.get_cmap('gnuplot'),extent =(xmin/1e-3,xmax/1e-3,ymin/1e-6,ymax/1e-6))
    plt.title('Density_dist [m-3]_{}'.format(step))
    plt.xlabel('distance[mm]')
    plt.ylabel('y [um]')
    plt.colorbar()
    plt.show()
    den_dist.savefig("./Qen_distribution_{}.png".format(step),format ='png')

    #note:cmap: rainbow, hot,jet,gnuplot,plasma


    energy_dist=plt.figure(2)

    plt.imshow(en_arr, origin ='lower',aspect = 'auto', cmap =plt.get_cmap('jet'),extent =(xmin/1e-3,xmax/1e-3,ymin/1e-6,ymax/1e-6))
    plt.title ('Energy_dist [MeV]_{} '.format(step))
    plt.xlabel('distance[mm]')
    plt.ylabel('y [um]')
    plt.colorbar()
    plt.show()
    energy_dist.savefig("./Qenergy_distribution_{}.png".format(step),format ='png')

0 个答案:

没有答案