Last active
February 2, 2020 08:34
-
-
Save nkpro2000sr/369614844bf714083b1b8d981ba87787 to your computer and use it in GitHub Desktop.
this is to plot multiline graph using given plot.csv file (a line for each labels)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
# input data is "plot.csv" file | |
## with first column as lable and (second, third) as (x, y) | |
"""sample | |
labels | x_label | y_label | |
-------------------------- | |
label_1| 1 | 2 | |
label_1| 2 | 3 | |
label_1| 3 | 4 | |
label_2| 1 | 1 | |
label_2| 2 | 2 | |
label_2| 3 | 3 | |
""" | |
" in ms excel use `'Save As' > 'Other Formats' > 'file_name.csv' " | |
data = [] | |
with open("plot.csv",'r') as csvfile : | |
csvreader = csv.reader(csvfile) | |
for row in csvreader: | |
data.append(row) | |
xlabel, ylabel = data[0][1:] | |
data = data[1:] | |
from collections import defaultdict | |
datadict = defaultdict(list) | |
for label,x,y in data : | |
datadict[label].append((float(x),float(y))) | |
del data, label, x, y, csvreader, csv # memory managment | |
import matplotlib.pyplot as plt | |
colors = ['b','g','r','c','m','y', # 16 colors available | |
'tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan'] | |
for i,line in enumerate(datadict.keys()): | |
plt.plot([row[0] for row in datadict[line]], [row[1] for row in datadict[line]], color= colors[i%len(colors)], label= line) | |
plt.xlabel(xlabel) | |
plt.ylabel(ylabel) | |
plt.legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment