This snippet provides a standard format to read .txt files, then manipulate and convert the essential data into scientific plots.
import numpy as np
import scipy.constants as C
import matplotlib.pyplot as plt
import seaborn as sns
# Constants
A = ...
B = ...
...
# Function to read and parse the data file
def read_and_parse_data(filename):
with open(filename, 'r') as file:
lines = file.readlines()
# Assuming the first row after the headers contains x-data
x1_data = [float(x) for x in lines[3].split('\t')[0:]] # '3' should be replaced by actual line number
x2_data = ... # More x-data if data is multidimensional
...
# Collect all y-data, assuming they start from the third line
y_data = []
for line in lines[5:]: # '5' should be replaced by actual line number
# Splitting each line on tab and converting each piece to float
# z_data_points = [float(y) for i, y in enumerate(line.split('\t')) if i % 2 == 0]
y_data_points = [float(y) for y in line.split('\t')[0:]]
y_data.append(y_data_points)
return x1_data, x2_data, ..., y_data
# Function to reform the read data
def massage_data(x1, x2, ..., y):
x1_new_data = []
for i in range(len(x1)):
x1_new_data.append(2*x1[i]) # replace the manipulation by actual massage way
x2_new_data = []
...
y_new_data = []
for i in range(len(y)):
ydata = []
for yy in y[i]:
ydata.append((yy)*1e6) # replace the manipulation by actual massage way
y_new_data.append(ydata)
return x1_new_data, x2_new_data, ..., y_new_data
# Example usage
filename1 = r'D:\···\···.txt' # Change this to the path of your file
filename2 = ... # More files
x1_data, x2_data, ..., y_data = read_and_parse_data(filename1)
x1_new_data, x2_new_data, ..., y_new_data = massage_date(x1_data, x2_data, ..., y_data)
# Line plot function
plt.figure(figsize=(10, 8))
plt.plot(x1_new_data, y_new_data)
plt.title('phase/°')
plt.xticks([], [], size=18,weight='bold')
plt.yticks([], [], size=18,weight='bold')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
# Map plot function
# Create a meshgrid for plotting
x, y = np.meshgrid(x1_data, x2_data)
# Shift the heatmap
# For each row in y, we shift x left by the index of the row
# for i in range(y.shape[0]):
# x[i, :] += 1.5*i
# Plotting the heatmap
plt.figure(figsize=(10, 8))
plt.pcolormesh(x, y, y_data, cmap='jet_r', shading='auto') # 'auto' shading interpolates the values
plt.colorbar() # adds a color bar to indicate the scale
plt.title('phase/°')
plt.xticks([], [], size=18,weight='bold')
plt.yticks([], [], size=18,weight='bold')
plt.xlabel('x')
plt.ylabel('y')
plt.show()

留下评论