from tkinter import Tk
from tkinter import filedialog as fd
import numpy as np
import h5py
import matplotlib.pyplot as plt
import cmaps
from matplotlib import cm
from matplotlib.colors import ListedColormap
import os
plt.rcParams['font.sans-serif'] = ['SimHei'] # 显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 显示坐标轴的负号
# red = plt.cm.Reds_r
red = cmaps.MPL_gist_heat
new_red = red(np.linspace(0, 1, 256))
new_red[-3:, 3] = 0
newred = ListedColormap(new_red)
count = 0
files = os.listdir(r'D:test10-4b1Charge_slice_0002') #返回路径下的文件和文件名列表
for i in files:
fileName1 = r'D:test10-4b1/Charge_slice_0002/' + str(i)
fileName2 = r'D:test10-4b2/Charge_slice_0002/' + str(i)
fileName3 = r'D:test10-4/Charge_slice_0002/' + str(i)
f1 = h5py.File(fileName1, 'r')
f2 = h5py.File(fileName2, 'r')
f3 = h5py.File(fileName3, 'r')
dataname1 = list(f1.keys())
dataset1 = f1['/' + dataname1[1]]
dataname2 = list(f2.keys())
dataset2 = f2['/' + dataname2[1]]
dataname3 = list(f3.keys())
dataset3 = f3['/' + dataname3[1]]
data1 = dataset1[...].T
# data1 = np.flip(data1,1)
data2 = dataset2[...].T
# data2 = np.flip(data2,1)
data3 = dataset3[...].T
plt.rcParams['xtick.direction'] = 'in' # 设置坐标轴的刻度向 内
plt.rcParams['ytick.direction'] = 'in'
xaxis = f1['/AXIS/AXIS2']
yaxis = f1['/AXIS/AXIS1']
x = np.linspace(xaxis[0], xaxis[1], data1.shape[1])
y = np.linspace(yaxis[0], yaxis[1], data1.shape[0])
plt.figure()
myfig = plt.gcf()
plt.pcolormesh(x, y, data3, vmin=-10, vmax=0, cmap=cmaps.MPL_Blues_r)
cb2 = plt.colorbar()
cb2.set_label("等离子体电荷密度")
plt.pcolormesh(x, y, data1, vmin=-50, vmax=0, cmap=newred)
cb1 = plt.colorbar()
cb1.set_label('粒子束团电荷密度')
plt.pcolormesh(x, y, data2, vmin=-50, vmax=0, cmap=newred)
plt.axis([x.min(), x.max(), y.min(), y.max()])
plt.xlabel("$\xi[c/\omega_p]$ ")
plt.ylabel("$x[c/\omega_p]$ ")
count += 1
myfig.savefig(r'C:UserstangPicturesb6/' + str(count) + r".png", dpi=300)
plt.close()