A simple CSV importer/exporter with plotting capabilities with matplotlib
Project description
csv_manager
This is a python module that features three simple classes for reading (class Reader
), plotting (class Plotter
, using Matplotlib with Latex rendering enabled) for the plots) and managing (class Database
) CSV files and also a method for writing CSVs.
By using this module, the user avoids the cumbersome repetition of coding a csv reader using the csv
python library then making the data in a readable structure for plt.plot(...)
.
Simple single graph example
from csv_manager import Plotter, DataFile
# Plotter inherits the class Reader
plotter = Plotter()
# Number of rows and columns for plt.sublots
datafile = DataFile('data_file_1.csv')
datafile.get_column_names('dataset1')
# returns the list of column names of the file `data_file_1.csv`
######################################################
plotter.plot(datafile, 'time', 'position', label='position $x(t)$', color='red', linestyle=':')
# Starting from " label='save' [...]" the arguments are the **kwargs in https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html
plotter.plot(datafile, 'time', 'speed', label='speed $v(t)$', color='blue', linestyle='--')
plotter.plot(datafile, 'time', 'position + sqrt(2/10 * speed)', 0, 1, label='dummy curve from expression')
# Mathematical expressions involving column names can be used
plotter.plot_data([0, 1, 2], [0, 1, 2], label='dummy data')
plotter.set(xlabel='Time ', ylabel='Position')
# the options that can be set are the **kwargs in https://matplotlib.org/api/axes_api.html
plotter.show()
The file data_file_1.csv
can be the followin (Note that the default column separator is a single space):
time position speed
0 0 0
1 5 3
2 20 3.6
3 45 2
4 80 5
5 125 8
6 180 9
7 245 10
8 320 9.2
9 405 8
10 500 7
Using Database
If you have a folder with lots of CSV files and find it too cumbersome to find the correct ones to plot or read. The class Databse
is made for you!
Requirements:
- Have all your CSV files in a folder (still works if they are in a subfolder of that folder)
- One of the following (or both):
- Follow a specific naming scheme on your CSV files:
filename|var1=val1|var2=val2|...|varN=valN.csv
where|
is a separator that can be different (any string of characters). - Have
sim_setting_name
andsim_setting_value
columns in your datafile (names can be changed), that contains the variables that define the simulation settings, e.g.:time speed sim_setting_name sim_setting_value 0 1 wind_speed 2.4 1 2 temp 25 2 4 3 5 ...
- Follow a specific naming scheme on your CSV files:
Filtering
Then, what you can do is to create a Database
instance with the folder path, and then you can use its method filter_datafiles
:
def filter_datafiles(self, file_name_base: str, filter_dict : dict) -> List[DataFile]:
where:
file_name_base
is a string that the file should contain in its filename (the text before the start of the variable definitions)filter_dict
is a dictionary that contains(key, val)
pairs, both strings, that correspond tovarN=valN
in the csv files you are looking for.
And this method will return all the files that match your filters. The retrieve a single datafile interactively, you can use the file_selection_prompt
method from Database
.
Dependencies:
- Python Matplotlib
- Python py_expression_eval, can be installed with
pip install py_expression_eval
- Latex distribution installed in your computer, can be deactivated in csv_plotter.py by changing the following line
plt.rcParams['text.usetex'] = True
toplt.rcParams['text.usetex'] = False
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file csv_manager-0.11.9.tar.gz
.
File metadata
- Download URL: csv_manager-0.11.9.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
42214010725b045ebfd2e1a72efb888a14dbe980fcd9a4c59f18618e4aebc58c
|
|
MD5 |
f3560994e4d29c7cdd547bc13ec6c57c
|
|
BLAKE2b-256 |
1c5a7c3868e48eafbe57ce8a1702b9f02b7cb97e6c31c065b942b4eac3c34f1b
|
File details
Details for the file csv_manager-0.11.9-py3-none-any.whl
.
File metadata
- Download URL: csv_manager-0.11.9-py3-none-any.whl
- Upload date:
- Size: 13.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
18a0f3f23744f95e2fb87ab3ed9455dd357e7e4d44fc7d1b0e470795f7abd80c
|
|
MD5 |
3fbc9846885419c40435a1d37d5f92fb
|
|
BLAKE2b-256 |
41fc6d87e5fced0b2b5d50466c91ba6c24bc378fb051a9cf5a2da17a7c546848
|