Coding

Python: Matplotlib – How to use csv and subplots for beginners

So how do I use subplots?

08/26/2018



from matplotlib.pyplot import  *
import numpy as np, os
from typing import List
#get file 
def get_file(filename: str):
	#determines validity of path 
	#returns file content 
	filepath = os.path.abspath(filename)
	if os.path.exists(filepath):
		fh = open(filepath)
		header = fh.readline() #excludes first row of the csv file e.g. x,y
		data = {"x":[],"y":[]}
		for row in fh.readlines():
			if len(row) <= 0: break print(len(row)) row = row.rstrip() x,y = row.split(",") x,y = float(x), float(y) data["x"].append(x) data["y"].append(y) fh.close() return data return False #plot the data -> subplots ( arguments in function)
def get_plot(data: List):
	#first create a figure --> a space where your subplots will be drawn into 
	fig = figure()
	x,y  = data["x"], data["y"]
	#create the first subplot x,y 
	subplot(221)
	plot(x, y)
	subplot(222)
	bar(x, y)
	subplot(223)
	hist(x, np.arange(1, len(x)))
	show()
	return

def task(filename):
	data = get_file(filename)
	if data:
		return get_plot(data)
	return False 

task("./MOCK_DATA_2.csv")

When I run the file, the following figure is generated:

#I know, it does not scream “pretty data visualization” but the code works I guesss.. See ya!

error: Content is protected !!