2

I plotted data from CSV file and I converted the same CSV file into NumPy array and plotted the same data. However, I get different graphs, which is confusing and can someone help me if I have made a mistake or I am missing to understand something.

Here is what I have coded and the respective images of the graph.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


csv_file1          = pd.read_csv('filename.csv')
columns            = csv_file1.columns
column_name1       = columns[7]
column_name2       = columns[8] 
column_name3       = columns[13]
csv_file1.plot(x=column_name3,y=column_name1,label='width')
csv_file1.plot(x=column_name3,y=column_name2,label = 'normalised width')

data_array = csv_file1.to_numpy()
plt.figure()
plt.plot(data_array[13],data_array[7])

Plot using data from the csv file:

Plot using data from the csv file

Plot using data from the csv file converted to numpy array:

Plot using data from the csv file converted to numpy array

2 Answers 2

0

Basically, they are the same data but when you plot, you choosed wrong index. The CSV is not matter here, you read the CSV file into a DataFrame then using the builtin plot method of pandas.DataFrame.

Here is example:

df = pd.DataFrame({"A": [1, 2], "B": [3.0, 4.5]})

When you plot, using df.plot(x='A', y='B'), x will be [1,2] and y will be [3.0, 4.5].

Then when convert DataFrame to numpy data using to_numpy() method, the data will become:

  numpy_data =  array([[1. , 3. ], [2. , 4.5]])

So if you using numpy_data[0] as x index, it will be [1., 3.] which is different to [1,2] in above.

To plot the same figure as pandas, you need to choose x, y in numpy as:

plot(numpy_data[:,0], numpy_data[:,1].

Conclusion: the plot depends on how you choose the x and y values.

1
  • Ahh, I see the error I have made. Thank you for your help Commented Feb 5, 2020 at 10:33
0

You are probably confusing rows and columns in the numpy array. The convention is array[row_index,column_index], and I think you want to plot column 13 and 7. Try with:

plt.plot(data_array[:, 13],data_array[:, 7])

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.