-
Notifications
You must be signed in to change notification settings - Fork 0
/
PLdraw.py
executable file
·205 lines (186 loc) · 6.13 KB
/
PLdraw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# python package for PLdraw
import scienceplots, os, fnmatch
import pandas as pd
import matplotlib.pyplot as plt
from warnings import warn
from matplotlib.axes import Axes
from matplotlib.figure import Figure
global h # eV energy
global c
h = 6.63e-34 / 1.6e-19 # eV energy
c = 3e8
def find(pattern, path):
result = []
for fileName in os.listdir(path):
if fnmatch.fnmatch(fileName, pattern):
result.append(os.path.join(path, fileName))
return result
def read_data(file) -> pd.DataFrame:
data = pd.read_csv(file, sep="\s+", header=None, names=["wavelength", "count"])
# normalize
data["normalized"] = (data["count"] - data["count"].min()) / (
data["count"].max() - data["count"].min()
)
data["energy"] = h * c / (data["wavelength"] * 1e-9)
return data
def single_plot(ax: Axes, file: str, energyDiagram: bool, rcParDict: dict = {}) -> Axes:
if bool(rcParDict):
plt.rcParams.update(rcParDict)
data = read_data(file)
ax.set_ylabel("Normalized intensity(arb.u.)", fontsize=12)
ax.tick_params(width=1.0, labelsize=12)
plt.setp(ax.spines.values(), linewidth=1.0)
if energyDiagram:
ax.invert_xaxis()
ax.plot(data["energy"], data["normalized"])
ax.set_xlabel("Energy(eV)", fontsize=12)
else:
ax.plot(data["wavelength"], data["normalized"])
ax.set_xlabel("Wavelength (nm)", fontsize=12)
ax.grid()
return ax
def single_origin(
ax: Axes, file: str, energyDiagram: bool, rcParDict: dict = {}
) -> Axes:
if bool(rcParDict):
plt.rcParams.update(rcParDict)
data = read_data(file)
ax.set_ylabel("Intensity(arb.u.)", fontsize=12)
ax.tick_params(width=1.0, labelsize=12)
plt.setp(ax.spines.values(), linewidth=1.0)
if energyDiagram:
ax.invert_xaxis()
ax.plot(data["energy"], data["count"])
ax.set_xlabel("Energy(eV)", fontsize=12)
else:
ax.plot(data["wavelength"], data["count"])
ax.set_xlabel("Wavelength (nm)", fontsize=12)
ax.grid()
return ax
# multi lines
def multi_plot(
ax: Axes,
files: list,
energyDiagram: bool = False,
legend: list = [],
shift: list = [],
rcParDict: dict = {},
) -> Axes:
if bool(rcParDict):
plt.rcParams.update(rcParDict)
if not legend:
legend = [file[-8:-4] for file in files]
if not shift:
shift = [0] * len(files)
ax.set_ylabel("Normalized intensity(arb.u.)", fontsize=12)
ax.tick_params(width=1.0, labelsize=12)
plt.setp(ax.spines.values(), linewidth=1.0)
if energyDiagram:
ax.invert_xaxis()
for idx, file in enumerate(files):
data = read_data(file)
if energyDiagram:
ax.plot(data["energy"] + shift[idx], data["normalized"], label=legend[idx])
ax.set_xlabel("Energy(eV)", fontsize=12)
else:
ax.plot(
data["wavelength"] + shift[idx], data["normalized"], label=legend[idx]
)
ax.set_xlabel("Wavelength (nm)", fontsize=12)
ax.legend()
ax.grid()
return ax
def overall_normalize(
ax: Axes,
files: list,
energyDiagram: bool = False,
legend: list = [],
shift: list = [],
rcParDict: dict = {},
) -> Axes:
warn("Only reasonable with same exposure condition")
if bool(rcParDict):
plt.rcParams.update(rcParDict)
if not legend:
legend = [file[-8:-4] for file in files]
if not shift:
shift = [0] * len(files)
# ax.set_xlim(0, 3000)
# ax.set_ylim(0, 5)
ax.set_ylabel("Normalized intensity(arb.u.)", fontsize=12)
ax.tick_params(width=1.0, labelsize=12)
plt.setp(ax.spines.values(), linewidth=1.0)
if energyDiagram:
ax.invert_xaxis()
data_list = []
all_max = []
all_min = []
for file in files:
data = read_data(file)
all_max.append(data["count"].max())
all_min.append(data["count"].min())
data_list.append(data)
for idx, data in enumerate(data_list):
data["normalized"] = (data["count"] - min(all_min)) / (
max(all_max) - min(all_min)
)
if energyDiagram:
ax.plot(data["energy"] + shift[idx], data["normalized"], label=legend[idx])
ax.set_xlabel("Energy(eV)", fontsize=12)
else:
ax.plot(
data["wavelength"] + shift[idx], data["normalized"], label=legend[idx]
)
ax.set_xlabel("Wavelength (nm)", fontsize=12)
ax.legend()
ax.grid()
return ax
def multi_origin(
ax: Axes,
files: list,
energyDiagram: bool = False,
legend: list = [],
shift: list = [],
rcParDict: dict = {},
) -> Axes:
warn("Only reasonable with same exposure condition")
if bool(rcParDict):
plt.rcParams.update(rcParDict)
if not legend:
legend = [file[-8:-4] for file in files]
if not shift:
shift = [0] * len(files)
# ax.set_xlim(0, 3000)
# ax.set_ylim(0, 5)
ax.set_ylabel("Intensity(arb.u.)", fontsize=12)
ax.tick_params(width=1.0, labelsize=12)
plt.setp(ax.spines.values(), linewidth=1.0)
if energyDiagram:
ax.invert_xaxis()
for idx, file in enumerate(files):
data = read_data(file)
if energyDiagram:
ax.plot(data["energy"] + shift[idx], data["count"], label=legend[idx])
ax.set_xlabel("Energy(eV)", fontsize=12)
else:
ax.plot(data["wavelength"] + shift[idx], data["count"], label=legend[idx])
ax.set_xlabel("Wavelength (nm)", fontsize=12)
ax.legend()
ax.grid()
return ax
## test area
if __name__ == "__main__":
postfix = input("give me the postfix")
energyDiagram = input("energy plot? (y/n): ").lower().strip() == "y"
files = find("*" + postfix + ".txt", path="./raman shift compare/")
legend = []
print("number of files:" + str(len(files)))
if len(files) == 0:
raise Exception("no match file found")
rcParams = {}
rcParams["lines.linewidth"] = 3
rcParams["figure.figsize"] = (5, 5)
with plt.style.context(["science", "nature"]):
fig, ax = plt.subplots()
ax = multi_plot(ax, files, energyDiagram, rcParDict=rcParams)
plt.show()