Skip to content Skip to sidebar Skip to footer

Using Sorted File To Plot X-axis With Corresponding Y-values From The Original File

Sample Data on GitHub I have a csv file which has 2 columns. The first column is in the format of: name001.a.a and the second column is 4 digit number (ex: 0001). I have another f

Solution 1:

NOTE: The sorting is probably not the most efficient method, but something to start with

Load the CSV file with csv.reader() and iterate it into a list

Load the sorted XML file into another list as well (Note: you can probably use csv.reader() again and set the delimiter to tab to keep it simple)

The syntax for loading a CSV file is as follows:

import csv
csv_file = []
withopen('file.csv', 'r') as f:
    csvreader = csv.reader(f)
    for line in csvreader:
        csv_file.append(line)

See the csv.reader() docs for more info and using delimiters. Just to be safe, remember to change the variable name of the file and reader when opening different files.

However, for your hostnum.csv, csv won't work, so you can write a parser by hand. I've done it for you:

csv_file = []
withopen('/Users/dash/Documents/hostnum.csv', 'r') as host:
    for line in host.readlines():
        line = line.replace('"', '')
        line = line.strip('\n')
        rank, value = line.split("    ")
        csv_file.append(value)

Sort the list by each element's position in the xml list:

us_csv_file.sort(key=lambda x: csv_file.index(x[0]))

This works by using a lambda (anonymous function) to take the string in the CSV file and look up its row number in the sorted XML file. The lambda returns a number, which sort then uses to set the new position the element in the list.

See the python wiki for a basic tutorial on sorting.

For plotting, usematplotlib.pyplot and set the xticks with matplotlib.pyplot.xticks()

Ex:

from matplotlib import pyplot as plt
import numpy as np

plt.plot([int(item[1]) for item in us_csv_file], 'o-')
plt.xticks(np.arange(len(csv_file)), [item for item in csv_file])

plt.show()

The end result

Hope this helps!

EDIT: use csv_file in the lambda

EDIT2: Here's the full code:

from matplotlib import pyplot as plt
import numpy as np
import csv

csv_file = []
withopen('hostnum.csv', 'r') as host:
    for line in host.readlines():
        line = line.replace('"', '')
        line = line.strip('\n')
        rank, value = line.split("    ")
        csv_file.append(value)

us_csv_file = []
withopen('us_csv_file.csv', 'r') as f:
    csvreader = csv.reader(f)
    for line in csvreader:
        us_csv_file.append(line)

us_csv_file.sort(key=lambda x: csv_file.index(x[0]))

plt.plot([int(item[1]) for item in us_csv_file], 'o-')
plt.xticks(np.arange(len(csv_file)), [item for item in csv_file])

plt.show()


EDIT (Again) After thinking about it, I think the best way would be to create a dict for each node with all the values stored in it.

from matplotlib import pyplot as plt
import numpy as np
from textwrap import wrap
import csv

#Opens the sorted hostnum.csv file and reads it; replaces the quotation marks.
csv_file = []
withopen('hostnum.csv', 'r') as host:
    for line in host.readlines():
        line = line.replace('"', '')
        line = line.strip('\n')
        rank, value = line.split("  ")
        csv_file.append(value)

#Opens the file and reads it
us_csv_file = []
withopen('fileFirst.csv', 'r') as f:
    csvreader = csv.reader(f)
    for line in csvreader:
        us_csv_file.append(line)

us_csv_file1 = []
withopen('fileSecond.csv', 'r') as f:
    csvreader = csv.reader(f)
    for line in csvreader:
        us_csv_file1.append(line)

us_csv_file2 = []
withopen('fileThird.csv', 'r') as f:
    csvreader = csv.reader(f)
    for line in csvreader:
        us_csv_file2.append(line)


runs = []

file_0 = {}
file_1 = {}
file_2 = {}

for result in us_csv_file:
    node_name = result[0]
    node_value = result[1]

    if file_0.get(node_name):   # If the node exists in the list
        file_0[node_name].append(node_value)
    else:
        file_0[node_name] = [node_value]

runs.append(file_0)

for result in us_csv_file1:
    node_name = result[0]
    node_value = result[1]

    if file_1.get(node_name):   # If the node exists in the list
        file_1[node_name].append(node_value)
    else:
        file_1[node_name] = [node_value]

runs.append(file_1)

for result in us_csv_file2:
    node_name = result[0]
    node_value = result[1]

    if file_2.get(node_name):   # If the node exists in the list
        file_2[node_name].append(node_value)
    else:
        file_2[node_name] = [node_value]

runs.append(file_2)


# all_plots = [[[], []],[[], []],[[], []]]

all_plots = [] # Make an array of 3 arrays, each with a pair of arrays inside# Each pair holds the x and y coordinates of the datapointsfor x inrange(3):
    all_plots.append([[],[]])


for run_number, run_group inenumerate(runs):

    for key, values in run_group.items():
        sorted_position = csv_file.index(key)
        for item in values:
            all_plots[run_number][0].append(sorted_position)
            all_plots[run_number][1].append(int(item))

#indicates the label names at the given spot
plt.legend(loc='upper right')

#Creates grid for x-y axises
plt.grid(True)

#Creates wrapped title for the graph
plt.title("\n".join(wrap("longlonglonglonglonglonglonglonglonglonglonglonglonglongTITLETITLETITLETITLETITLETITLE")),size = 9.5)

#x-y labels for the graph
plt.xlabel("Node Names", fontsize = 8)
plt.ylabel("Run Times", fontsize = 8)

#ticks - x and y axisses' data format.

plt.scatter(all_plots[0][0], all_plots[0][1], c='b', marker='+', label="First")
plt.scatter(all_plots[1][0], all_plots[1][1], c='g', marker=(5,2), label="Second")
plt.scatter(all_plots[2][0], all_plots[2][1], c='r', marker=(5,1), label="Third")


plt.xticks(range(len(csv_file))[::25], [item for item in csv_file][::25], rotation=90, size=8)


plt.yticks(np.arange(0,11000,1000), size=8)

#Saves a PNG file of the current graph to the folder and updates it every time
plt.savefig('./test.png', bbox_inches='tight')

# Not to cut-off bottom labels(manually) - enlarges bottom
plt.gcf().subplots_adjust(bottom=0.23)


plt.show()

Post a Comment for "Using Sorted File To Plot X-axis With Corresponding Y-values From The Original File"