2

I'm having so much trouble and I'd like if I could get a hand. I'm trying to read through a csv file and extract the columns and plot whatever columns are listed in column_index, which is actually an input provided to the user and can be changed.

Here is a link of my pastebin of the .csv file, and this is my attempt:

with open('./P14_data.csv', 'rb') as csvfile:
        data = csv.reader(csvfile, delimiter=',')

        #retrieves rows of data and saves it as a list of list
        x = [row for row in data]

        #forces list as array to type cast as int
        int_x = np.array(x, int)

        column_index = [1,2,3]
        column_values = np.empty(0)

        for col in column_index:
        #loops through array
            for array in range(len(int_x)):
        #gets correct column number
                column_values = np.append(column_values,np.array(int_x[array][col-1]))
            plt.plot(column_values)

However, this only graphs one line for all 3 columns when I want 3 different lines for the columns:

enter image description here

1
  • show the result of print(int_x), print(column_values) and print(column_values) just before plt.plot() Commented May 4, 2017 at 4:00

1 Answer 1

1

Reset column_values before the inner loop. Otherwise, values are keep appended to the same list.

column_index = [1,2,3]
for col in column_index:
    column_values = np.empty(0)  # <--- reset for each line chart.
    for array in range(len(int_x)):
        column_values = np.append(column_values, np.array(int_x[array][col-1]))
    plt.plot(column_values)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.