Your data does not have to be an ndarray
in order to plot it with matplotlib
. You can read in your data as a list and it will plot all the same as also mentioned by kwinkunks. How you read in your data matters which is a step you really need to worry about first!
To answer your question, if you really want to manipulate data and not just plot it then using a numpy
array is the way to go. The advantage of using numpy
arrays is that you can easily compute new variables and condition the data you have.
Take the following example. On the left you can plot the data as a list but you cannot manipulate the data and subset points. On the right side if your data is a numpy
array you can easily condition the data say take only x values greater than 4 and plot them as red.
import matplotlib.pyplot as plt
import numpy as np
#Declare some data as a list
x = [2,5,4,3,6,2,6,10,1,0,.5]
y = [7,2,8,1,4,5,6,5,4,5,2]
#Make that same data a numpy array
x_array = np.array([2,5,4,3,6,2,6,10,1,0,.5])
y_array = np.array([7,2,8,1,4,5,6,5,4,5,2])
#Declare a figure with 2 subplots
fig = plt.figure(figsize=(12,6))
ax1 = plt.subplot(121)
ax2 = plt.subplot(122)
#Plot only the list
ax1.scatter(x,y)
#Plot only the list again on the second subplot
ax2.scatter(x,y)
#Index the data based on condition and plot those points as red
ax2.scatter(x_array[x_array>3],y_array[x_array>3],c='red')
plt.show()