Plotting data from a file

When I was a university (sheep) in the first year, we often had to build graphs in laboratory work in Physics. Moreover, they had to be drawn by hand on millimetric paper. Back then, there was simply not enough programming skills to draw these graphs on a PC. Now it can be easily done on LapTop(e). And there are a whole bunch of ways to build a graph on a PC. This can be done in Google Spreadsheets, MatLab, MathCAD, GNUOctave, MS Excel. Let’s try to do it in Python.

Formulation of the problem:

There is a LiLog.csv file. Here are some of his lines:

  14,   0.833,  22.25, 22:43:09, 9/7/2023, 1517544445
  15,   0.833,  22.25, 22:43:29, 9/7/2023, 1517544465
  16,   0.000,  22.25, 22:43:49, 9/7/2023, 1517544485
  17,   0.833,  22.25, 22:44:09, 9/7/2023, 1517544505

It is necessary to build a 2D graph, where the X-axis is the n-th column, and the Y-axis is the k-th column from the text file.

What do you need from the software?

No.

Program

Purpose

1

Python.exe

Python programming language interpreter

2

matplotlib

Module for visualization

3

csv

parser *.CSV files

4

NotePad++.exe

Text editor for writing a Python script and editing a file with source data for a chart

Solution

This script takes a *.csv file and builds a graph for the 4th and 2nd columns.

import matplotlib.pyplot as plt
import csv

X = []
Y = []

with open('LiLog.csv', 'r') as datafile:
    plotting = csv.reader(datafile, delimiter=";")
    
    for ROWS in plotting:
        X.append(float(ROWS[5]))
        Y.append(float(ROWS[1]))

plt.plot(X, Y)
plt.title('Line Graph using CSV')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

What happens in this script. The program creates 2 lists: X Y. List X puts the numbers from the 4th column into list Y puts the data from the second column. Then the program draws a graph of these values ​​on the canvas in a separate window.

This is how the chart turned out

Graph of natural light against time

Graph of natural light against time

You can also add another chart, explanations (legend) to the chart.

import matplotlib.pyplot as plt
import csv

X = []
Y = []

with open('LiLog.csv', 'r') as datafile:
    plotting = csv.reader(datafile, delimiter=";")
    
    for ROWS in plotting:
        X.append(float(ROWS[5]))
        #X.append('{} {}'.format(ROWS[3], ROWS[4]))
        Y.append(float(ROWS[1]))

print ('X {} Nums'.format(len(X)))
print ('Y {} Nums'.format(len(Y)))

threshold=63.0
T=[threshold]*len(Y)

print ('Type X {} '.format(type(X)))
print ('Type Y {} '.format(type(Y)))
print ('Type T {} '.format(type(T)))

plt.plot(X, Y)
plt.plot(X, T)
plt.title('Illumination change')
plt.xlabel('Time,[s]')
plt.ylabel('Light level, [Lx]')
plt.grid()
plt.xticks(rotation=-90)
plt.legend(['illumination', 'threshold {} Lx'.format(threshold)])
plt.show()

Here, for example, for clarity, a threshold value line for this measurement at the level of 63 Lux has been added.

Graph of natural light against time

Graph of natural light against time

The plot in matplotlib is not just static. It can be enlarged in the place of interest and substitute the chart fields in the menu, which is located in the lower left corner of the window.

dawn time 1:24:41

dawn time 1:24:41

Benefits of Plotting in Python

1–It’s free. Unlike MatLab, you can plot MathCAD in Python absolutely free of charge.

2–There is analytics. You can enlarge the graph, save the graph in *.png file format, set the scale, adjust the field width, expand the scale, and overlay the grid.

3–According to the Python script, you can generate an *.exe file in case you want to hide the plotting algorithm.

4–No artifacts and temporary files. No *.o *.ld is needed, just like if you decide to write a plotter in C++. There is only a *.py file with the source and nothing else is needed.

5–Everything is done purely by code. You don’t even need a mouse to draw a graph.

6–Thanks to Python, scripts can be executed on any operating system: Windows, Linux, etc.

Drawbacks of Plotting in Python

1–It’s not entirely clear how to display real-time graph readings. For example, when numbers come from the street from a serial COM port or TCP socket.

Conclusion

The Python interpreter in conjunction with Matplotlib is an excellent option for visualizing experimental data from a *.csv text file.

Acronym

Decryption

csv

Comma Separated Values

PC

personal computer

Links

https://translated.turbopages.org/proxy_u/en-ru.ru.769a89e5-64b314a2-b8a40da2-74722d776562/https/www.geeksforgeeks.org/how-to-plot-data-from-a-text-file- using-matplotlib/

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *