plot.py 2.08 KB
'''
Take a file and plot its data onto a 2d or 3d axis depending on the data. 
'''

import os
import numpy as np
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import argparse
import json

# Defining argparse
parser = argparse.ArgumentParser(prog='Plotter', description='Plot a file of 2d ou 3d dimension')
parser.add_argument('filepath', type=str,
                    help='the path of the file you want to plot')
parser.add_argument('-o-', '--output', type=str,
                    default='plot.pdf',
                    help='the path of the ploted file')
parser.add_argument('-t', '--toy', action='store_true',
                    help='test the script on a toy example. Do not test all the file content')

args = parser.parse_args()

# Editing global variable
FILE_PATH=args.filepath
OUTFILE_PATH = args.output
TOY_VERSION = args.toy

# Defining vectors with default number of column
vectors = np.empty((0, 64), np.float32)
metas = np.empty((0, 4), np.float32)

# READ DATA
with open(os.path.join(FILE_PATH), "r") as f:
        for i, line in enumerate(f):
                if TOY_VERSION == True and i > 100:
                        break
                spl_line = line.split(" ")
                if(len(vectors) == 0):
                        vectors = np.empty((0, len(spl_line[1:])), np.float32)
                metas = np.append(
                        metas,
                        np.asarray([spl_line[0].split(",")]),
                        axis=0)

                vectors = np.append(
                        vectors,
                        np.asarray([spl_line[1:]], dtype=np.float32),
                        axis=0)

vectors_T = np.transpose(vectors)


# Plot the file
plt.plot(vectors, 'ro')
fig, ax = plt.subplots()

if(vectors_T.shape[0] == 2):
	ax.scatter(vectors_T[0], vectors_T[1]) #c=close, s=volume, alpha=0.5)
else:
	ax.scatter(vectors_T[0], vectors_T[1], vectors_T[2])

ax.set_xlabel('Axe 1', fontsize=15)
ax.set_ylabel('Axe 2', fontsize=15)

if(vectors_T.shape[0] == 3):
	ax.set_zlabel('Axe 3', fontsize15=15)

ax.set_title('Volume and percent change')
plt.savefig(OUTFILE_PATH)