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

118 lines
4.1 KiB
Python

import numpy as np
import matplotlib.pyplot as plt
def get_data(filename):
file = open(filename)
title = file.readline()
lines = file.readlines()
x = []
y = []
for line in lines:
line.strip()
data = line.split()
x.append(float(data[0]))
y.append(float(data[1]))
return x, y
x_meas, y_meas = get_data('Plotting\Base\Measured.txt')
x_sim, y_sim = get_data("Plotting\Base\Simulated.txt")
#print(min(x_meas),min(x_sim))
################################# BASIC PLOT CODE #################################
# Basic Styling
plt.rcParams.update({
'font.family': 'Courier New', # monospace font
'font.size': 20, # Fonts
'axes.titlesize': 30, # |
'axes.labelsize': 20, # V
'xtick.labelsize': 15,
'ytick.labelsize': 15,
'legend.fontsize': 20,
'figure.titlesize': 20,
'figure.figsize': [10,10] # Figure Size
})
# Figure Setup
fig, ax = plt.subplots()
title = 'PV Diagram' # Title
xlab = '% of Volume' # X Label
ylab = 'Cyinder Pressure [Bar]' # Y Label
ax.set_xlabel(xlab)
ax.set_ylabel(ylab, labelpad=0)
ax.set_title(title, pad = 0) #pad controls distance to plot
ax.spines['top'].set_visible(False) # Controls non axis borders
ax.spines['right'].set_visible(False)
# ax.set_xscale('log')
# ax.set_yscale('log')
x_min = 0 # Axis Limits and Ticks
x_max = 1.1
x_step_maj = 1 #steps not division
x_step_min = 1
y_min = 0
y_max = 25
y_step_maj = 25
y_step_min = 1
ax.set_xlim(x_min,x_max) # X limits
ax.set_xticks([0.1047,1]) # X Major Ticks
# ax.set_xticks(np.arange(x_min,x_max,x_step_min), minor=True) # X Minor Ticks
ax.set_ylim(y_min,y_max) # Y limits
ax.set_yticks([24.04,24.68]) # Y Major Ticks
# ax.set_yticks(np.arange(y_min,y_max,y_step_min),minor=True) # Y Minor Ticks
# ax.grid(True, which='major',alpha=1, lw=1, ls='--', color='black') # Turn On Major Grid
# ax.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()
###################### Multi Line ######################
x = [x_meas,x_sim,[1,1],[.107,.107],[0,.125],[0,.125]] # List of Lists
y = [y_meas,y_sim,[0,1.1],[0,12.5],[24.04,24.04],[24.68,24.68]] # List of Lists
#### Lists must be equal length ###
dl = ['Measured', 'Simulated','','','',''] # Data Labels (list)
lc = ['black', 'black','black','black','black','black'] # Line Color |
ls = ['-','--','--','--', '--','--'] # Line Style |
lw = [1.5,2,1,1,1,1] # Line Width V
a = [1,1,1,1,1,1]
s = [] # Marker Size, 20 is default
m = [] # Marker Type, 'o' is default
# Use color-hex.com for color pallets
# Common ones: Shades of Teal, Ocean Breezes By,Ppt Cv
for i in range(len(x)):
ax.plot(x[i],y[i],label=dl[i],color=lc[i],linestyle=ls[i],linewidth=lw[i],alpha=a[i])
#ax.scatter(x[i],y[i],label=dl[i],color=lc[i],marker=m[i],size=s[i])
ax.legend(loc='upper right', bbox_to_anchor=(0.85, 0.85), ncol=1, frameon=False, labelspacing=0.2, columnspacing=0.75,
handlelength=1, 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()