import numpy as np import matplotlib.pyplot as plt ''' This file gives the trace for 2 data sets (Nominally measured and simulated) but can be modified to do 1 or more if needed. 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 line 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") y_max_data = 36 ################################# 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,5] # Figure Size }) # Figure Setup fig, ax = plt.subplots() title = 'Cylinder Pressure Trace' # Title xlab = '' # X Label ylab = 'Pressure [Bar]' # Y Label ax.set_xlabel(xlab) ax.set_ylabel(ylab) ax.set_title(title, pad = 20) #pad controls distance to plot ax.spines['top'].set_visible(False) # Controls non axis borders ax.spines['right'].set_visible(False) x_min = -180 # Axis Limits and Ticks x_max = 180 x_step_maj = 1 #steps not division x_step_min = 1 y_min = 0 y_max = y_max_data y_step_maj = 5 y_step_min = 1 ax.set_xlim(x_min,x_max) # X limits ax.set_xticks([-180,-90,0,90,180],['BDC\n-180°','Intake/Compression','TDC\n0°','Exhaust/Expansion','BDC\n180°']) # X Major Ticks ax.set_xticks([-180,-90,0,90,180], minor=True) # X Minor Ticks ax.set_ylim(y_min,y_max) # Y limits ax.set_yticks(np.arange(y_min,y_max,y_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.grid(True, which='major',alpha=0.5) # 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_sim,x_meas,[0,0]] # List of Lists y = [y_sim,y_meas,[0,36]] # List of Lists #### Lists must be equal length ### dl = ['Simulated','Measured',''] # Data Labels (list) lc = ['black','black','black'] # Line Color | ls = ['-','--','-'] # Line Style | lw = [2,2,1] # Line Width V a = [1,1,0.8] # Transparency 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='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()