-1

I want to make a loop to create a plot for each corresponding column in 2 different csv files such that column 1 in csv A and column 1 in csv B are plotted together with the same timestamp (pulled from another csv). I do not think I will have trouble when I modify my code to create the loop, but I have to get matplotlib to work for the first column before trying to construct a loop.

I have already tried checking to make sure that the correct data is being passed into the function and that is in the correct order. For example, I printed the zipped array as a list (t_array, b_array) and checked my csv files to verify that the data was in the correct order. I have also tried modifying the axes, ticks, and zoom to no avail. I have tried checking the helper functions which I lifted from my other projects and they all work as expected.

def double_plot():
before = read_file(r_before)
after = read_file(r_after)
time = read_file(timestamp)
if len(before) == len(after):
    b_array = np.asarray(before[1])
    a_array = np.asarray(after[1])
    t_array = np.asarray(time[1])
    plt.plot(t_array, b_array)
    plt.plot(t_array, a_array)
    plt.show()
else:
    print(len(before))
    print(len(after))
    print("dimension failure")

read_file() is a helper function that reads csv files and saves the columns to dictionaries with the first column key indexed by key "1" and so on down the columns. I know I should probably change it to index with 0 first, but this is a problem for later...

Images showing what I want the code to do and what it is doing

What I would like What my code is actually doing

Thank you for your time. This is my first time posting so I apologize if something I did was incorrect. I did attempt to find the answer before posting.

Edits: data sample; read_file()

screenshot of excel

def read_file(read_file):
data = {}
with open(read_file, 'r') as f:
    reader = csv.reader(f)
    for row in reader:
        col_num = 0
        for col in row:
            col_num += 1
            if col_num in data:
                data[col_num].append(col)
            else:
                ls = col
                ls = [ls]
                data[col_num] = ls
return data

edit again: ^ its much better to use pandas but I am leaving this here because its funny after seeing it done with dataframes

angrymantis
  • 352
  • 1
  • 9
  • Hi and welcome to SO. Your plotting seems to be fine, maybe your preprocessing / helper function has a bug? Could you provide a data-sample? – ilja Jul 19 '19 at 22:02
  • @ilja Thank you for your comment. Here is sample data. Ordinarily each column comes from a seperate file and the # notes are not present. Thanks for your help. The data sample is added to the bottom of the post. – angrymantis Jul 19 '19 at 22:12
  • as far as I can see from the screenshot the data seems be fine, but you never know without seeing the whole file ;-). Maybe you could upload it somewhere line OneDrive and share a link to the files and/or add your `read_file()` function to the post? – ilja Jul 19 '19 at 22:21
  • I am sure that there is nothing intrinsically wrong with the files themselves as I have used them for many other things. I am totally stumped. I posted the read_file() function. Thanks again! – angrymantis Jul 19 '19 at 22:56
  • So, it turns out that for some reason the arrays contained strings rather than floats. I used something like '' new_list = list(np.float_(list(array)) '' to fix that. I figured that out from these two: https://stackoverflow.com/questions/1614236/in-python-how-do-i-convert-all-of-the-items-in-a-list-to-floatshttps://stackoverflow.com/questions/48062499/matplotlib-y-axis-values-are-not-ordered – angrymantis Jul 20 '19 at 05:05
  • "So, it turns out that for some reason the arrays contained strings rather than floats." CSV files are plain text. Python's standard-library CSV reader does not try to interpret the input, it returns only strings. If you really want a CSV reader that tries to infer your data types, that capability is available in a third-party package called Pandas. – John Ladasky Jul 20 '19 at 05:16

1 Answers1

0

The arrays I was using with the plot function contained strings rather than floats.

These links explain the problem along with multiple ways to fix it: Matplotlib y axis values are not ordered In Python, how do I convert all of the items in a list to floats?

angrymantis
  • 352
  • 1
  • 9