Files
Cloud-Server/syncthing/sync/Coding Projects/Python/Plotting/GT-Power/Stacked_CAD_Template.py
2025-11-25 14:15:40 -05:00

185 lines
6.8 KiB
Python

import numpy as np
import matplotlib.pyplot as plt
'''
This file gives 2 stacked graphs, both with the options to have multiple data sets (Nominally measured and simulated for the top graph and temperature for the bottom)
It will automatically scale from -180 to 180 for a 2-Stroke
It needs .txt files copied from the data of GT-Power with the format of:
title
data CAD
data CAD
. .
. .
. .
Other needed info: Max y value for the y axis
**Will need to adjust colors, line type, etc.**
'''
def get_data(filename):
file = open(filename)
title = file.readline()
lines = file.readlines()
x = []
y = []
x_shift = []
y_shift = []
for line in lines:
line.strip()
data = line.split()
if float(data[0])<=180:
x.append(float(data[0]))
y.append(float(data[1]))
else:
x_shift.append(float(data[0])-360)
y_shift.append(float(data[1]))
return x_shift+x, y_shift+y
x_meas, y_meas = get_data('Python/Plotting/Base/Measured_P.txt') #Files go here
x_sim, y_sim = get_data("Python/Plotting/Base/Simulated_P.txt")
x_temp, y_temp = get_data("Python/Plotting/Base/temperature.txt")
################################# BASIC PLOT CODE #################################
# Basic Styling
plt.rcParams.update({
'font.family': 'Courier New', # monospace font
'font.size': 20, # Fonts
'axes.titlesize': 20, # |
'axes.labelsize': 20, # V
'xtick.labelsize': 15,
'ytick.labelsize': 15,
'legend.fontsize': 15,
'figure.titlesize': 20,
'figure.figsize': [10,10] # Figure Size
})
# Figure Setup
fig, ax = plt.subplots(2)
title = 'Cylinder Pressure Trace' # Title
ax[0].set_title(title, pad = 20) #pad controls distance to plot
### Figure 1 (top) ###
x_1_lab = '' # X Label
y_1_lab = 'Pressure [Bar]' # Y Label
ax[0].set_xlabel(x_1_lab)
ax[0].set_ylabel(y_1_lab)
ax[0].spines['top'].set_visible(False) # Controls non axis borders
ax[0].spines['right'].set_visible(False)
### Figure 2 (bottom) ###
x_2_lab = '' # X Label
y_2_lab = 'Temperature [K]' # Y Label
ax[1].set_xlabel(x_2_lab)
ax[1].set_ylabel(y_2_lab)
ax[1].spines['top'].set_visible(False) # Controls non axis borders
ax[1].spines['right'].set_visible(False)
### axis is the same for both graphs ###
### x displays on bottom graph only ###
x_min = -180 # Axis Limits and Ticks
x_max = 180
x_step_maj = 1 #steps not division
x_step_min = 1
ax[1].set_xlim(x_min,x_max) # X limits
ax[1].set_xticks([-180,-90,0,90,180],['BDC\n-180°','Intake/Compression','TDC\n','Exhaust/Expansion','BDC\n180°']) # X Major Ticks
ax[1].set_xticks([-180,-90,0,90,180], minor=True) # X Minor Ticks
ax[0].set_xlim(x_min,x_max) # X limits
ax[0].set_xticks([-180,-90,0,90,180],[]) # X Major Ticks
ax[0].set_xticks([-180,-90,0,90,180], minor=True) # X Minor Ticks
### Figure 1 (top) ###
y_1_min = 0
y_1_max = 36
y_1_step_maj = 5
y_1_step_min = 1
ax[0].set_ylim(y_1_min,y_1_max) # Y limits
ax[0].set_yticks(np.arange(y_1_min,y_1_max,y_1_step_maj),tickpad = 10) # Y Major Ticks
# ax.set_yticks(np.arange(y_min,y_max,y_step_min),minor=True) # Y Minor Ticks
ax[0].grid(True, which='major',alpha=0.5) # Turn On Major Grid
ax[0].grid(True, which='minor',alpha=0.2) # Turn on Minor Grid
# alpha controls transparency
### Figure 2 (bottom) ###
y_2_min = 500
y_2_max = 2150
y_2_step_maj = 200
y_2_step_min = 100
ax[1].set_ylim(y_2_min,y_2_max) # Y limits
ax[1].set_yticks(np.arange(y_2_min,y_2_max,y_2_step_maj),tickpad = 10) # Y Major Ticks
# ax[1].set_yticks(np.arange(y_2_min,y_2_max,y_2_step_min),minor=True) # Y Minor Ticks
ax[1].grid(True, which='major',alpha=0.5) # Turn On Major Grid
ax[1].grid(True, which='minor',alpha=0.2) # Turn on Minor Grid
# alpha controls transparency
###################### Single Line ######################
'''
# x = []
# y = []
ax.plot(x,y,color='black',linestyle='-',linewidth='1')
# Basic Line Styles: -, --, :, -.
# Basic Colors: red, blue, green, purple, cyan, magenta, black, brown, etc
# Can Specify Hex code for colors
# ax.scatter(x,y,color='black',marker='o',size=20)
# # Many Markers: circle-'o', square-'s', triangle-'^',star-'*', x-'x'
# plt.show()
'''
###################### Stacked Line ######################
x1 = [x_meas,x_sim,[0,0]] # List of Lists
y1 = [y_meas,y_sim,[0,36]] # List of Lists
dl1 = ['Measured','Simulated',''] # Data Labels (list)
lc1 = ['black','black','black'] # Line Color |
ls1 = ['--','-','-'] # Line Style |
lw1 = [2,2,1] # Line Width V
a1 = [1,1,0.8] # Transparency
for i in range(len(x1)):
ax[0].plot(x1[i],y1[i],label=dl1[i],color=lc1[i],linestyle=ls1[i],linewidth=lw1[i], alpha=a1[i])
ax[0].legend(loc='center', bbox_to_anchor=(0.8,0.8), ncol=1, frameon=True,edgecolor='white',framealpha=1, labelspacing=0.2, columnspacing=0.75,handlelength=0.9, handletextpad=0.3)
# anchor loc is based on the plot area, 0.5 is half the width, 1.01 is just above the top
# labelspacing is for vertical spacing, column is for horizontal, handel is for line length, textpad is for handl eto text
x2 = [x_temp,[0,0]] # List of Lists
y2 = [y_temp,[0,2150]] # List of Lists
dl2 = ['',''] # Data Labels (list)
lc2 = ['black','black'] # Line Color |
ls2 = ['-','-'] # Line Style |
lw2 = [2,1] # Line Width V
a2 = [1,0.8] # Transparency
for i in range(len(x2)):
ax[1].plot(x2[i],y2[i],label=dl2[i],color=lc2[i],linestyle=ls2[i],linewidth=lw2[i], alpha=a2[i])
# ax[1].fill_between(x_temp,y_temp,hatch='///', alpha=0)
# ax[1].legend(loc='center', bbox_to_anchor=(0.8,0.8), ncol=1, frameon=True,edgecolor='white',framealpha=1, labelspacing=0.2, columnspacing=0.75,handlelength=0.9, handletextpad=0.3)
# anchor loc is based on the plot area, 0.5 is half the width, 1.01 is just above the top
# labelspacing is for vertical spacing, column is for horizontal, handel is for line length, textpad is for handl eto text
plt.tight_layout()
plt.show()