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

File Content


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

CODE

from matplotlib.pyplot import *
import numpy as np
import 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")

Result

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!

%d bloggers like this: