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

So how do I use subplots?

08/26/2018


The code below is custom to my csv file (see below), so make sure you either use the same format or change the "get_file" function to the properly process your file. 
MOCK_DATA_2

The file above looks like this: 
x,y
64,13
57,14
16,30
92,29
65,97
11,33 

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

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!