import numpy as np import matplotlib.pyplot as plt ''' This file gives the PV Diagram for 2 traces (Nominally measured and simulated) but can be modified to do 1 or more if needed. It needs .txt files copied from the data of GT-Power with the format of: (no headers) pressure volume fraction pressure volume fraction . . . . . . Other needed info: Max y value for the y axis **Will need to adjust how far out min/max lines go, as well as colors, line type, etc.** ''' 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, round(min(x),3), round(max(y),2) x_meas, y_meas, x_meas_min, y_meas_max = get_data('Python/Plotting/Base/Measured.txt') #Files here x_sim, y_sim, x_sim_min, y_sim_max = get_data("Python/Plotting/Base/Simulated.txt") x_min_data = min(x_meas_min, x_meas_min) #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([x_min_data,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([y_meas_max,y_sim_max]) # 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],[x_min_data,x_min_data],[0,.125],[0,.125]] # List of Lists y = [y_meas,y_sim,[0,1.1],[0,12.5],[y_meas_max,y_meas_max],[y_sim_max,y_sim_max]] # 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()