After collecting your data as part of a V&V effort, it helps to be able to visualize it to help determine a PASS or a FAIL. This post will walk you through a couple setups.
The Data Files
For the purposes of this example, you will find a couple of CSV files on my GitHub that are simple trig functions to give us something to plot. Sine, Cosine, Sinc, etc.
The Basic Plot
You will find the code below on my Python Examples project on GitHub.
Walking through the script below:
- Initialize our arrays
- Retrieve example data from data file and append to our null arrays
- Format our plot
- Apply our data
- Show our plot
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import csv import matplotlib.pyplot as plt import numpy as np # Initialize arrays x_axis = [] y_axis = [] # Retrieve our data from external CSV file with open('sineData.csv') as csvfile: readCSV = csv.reader(csvfile, delimiter=',') # populate arrays for row in readCSV: x_axis.append(float(row[0])) y_axis.append(float(row[1])) # format our plot plt.xlabel('Sample') plt.ylabel('Voltage') plt.title('Example Plot') # apply data to plot plt.plot(x_axis,y_axis) # show plot plt.show() |
This produces the plot below.
Multiple Lines on One Plot
Taking the above example a step farther, it’s often useful to plot multiple lines on a single plot.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import csv import matplotlib.pyplot as plt import numpy as np # Initialize arrays x_axis = [] y_axis_sin = [] y_axis_cos = [] y_axis_sinc = [] # Retrieve our data from external CSV file with open('multiPlotData.csv') as csvfile: readCSV = csv.reader(csvfile, delimiter=',') # populate arrays for row in readCSV: x_axis.append(float(row[0])) y_axis_sin.append(float(row[1])) y_axis_cos.append(float(row[2])) y_axis_sinc.append(float(row[3])) # format our plot plt.xlabel('Sample') plt.ylabel('Voltage') plt.title('Example Plot') # apply data to plot plt.plot(x_axis,y_axis_sin, label='SIN') plt.plot(x_axis,y_axis_cos, label='COS') plt.plot(x_axis,y_axis_sinc, label='SINC') plt.legend() # show plot plt.show() |
This produces the plot below.
Working with Subplots
Finally, for more complicated data sets you can split the graphs onto multiple subplots using the subplot definition.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import csv import matplotlib.pyplot as plt import numpy as np # Initialize arrays x_axis = [] y_axis_sin = [] y_axis_cos = [] y_axis_sinc = [] # Retrieve our data from external CSV file with open('multiPlotData.csv') as csvfile: readCSV = csv.reader(csvfile, delimiter=',') # populate arrays for row in readCSV: x_axis.append(float(row[0])) y_axis_sin.append(float(row[1])) y_axis_cos.append(float(row[2])) y_axis_sinc.append(float(row[3])) plt.subplot(3,1,1) plt.xlabel('Sample') plt.ylabel('Voltage') plt.title('Sine Function') plt.plot(x_axis,y_axis_sin, label='SIN') plt.subplot(3,1,2) plt.xlabel('Sample') plt.ylabel('Voltage') plt.title('Cosine Function') plt.plot(x_axis,y_axis_cos, label='COS') plt.subplot(3,1,3) plt.xlabel('Sample') plt.ylabel('Voltage') plt.title('Sinc Function') plt.plot(x_axis,y_axis_sinc, label='SINC') plt.tight_layout() # show plot plt.show() |
This produces the plot below.