Dimitris
Dimitris

Reputation: 435

plot multiple data series from numpy array

I had a very ambitious project (for my novice level) to use on numpy array, where I load a series of data, and make different plots based on my needs - I have uploaded a slim version of my data file input_data and wanted to make plots based on: F (where I would like to choose the desired F before looping), and each series will have the data from E column (e.g. A12 one data series, A23 another data series in the plot, etc) and on the X axis I would like to use the corresponding values in D.

so to summarize for a chosen value on column F I want to have 4 different data series (as the number of variables on column E) and the data should be reference (x-axis) on the value of column D (which is date)

I stumbled in the first step (although spend too much time) where I wanted to plot all data with F column identifier as one plot. Here is what I have up to now:

import os 
import numpy as np
N = 8 #different values on column F
M = 4 #different values on column E
dataset = open('array_data.txt').readlines()[1:]
data = np.genfromtxt(dataset)
my_array = data
day = len(my_array)/M/N # number of measurement sets - variation on column D
for i in range(0, len(my_array), N):
    plt.xlim(0, )
    plt.ylim(-1, 2)
    plt.plot(my_array[i, 0], my_array[i, 2], 'o')
    plt.hold(True)
plt.show()

this does nothing.... and I still have a long way to go..

Upvotes: 2

Views: 3526

Answers (1)

Stephen
Stephen

Reputation: 2424

With pandas you can do:

import pandas as pd
dataset = pd.read_table("toplot.txt", sep="\t")
#make D index (automatically puts it on the x axis)
dataset.set_index("D", inplace=True)
#plotting R vs. D
dataset.R.plot()
#plotting F vs. D
dataset.F.plot()

dataset is a DataFrame object and DataFrame.plot is just a wrapper around the matplotlib function to plot the series.

I'm not clear on how you are wanting to plot it, but it sound like you'll need to select some values of a column. This would be:

# get where F == 1000
maskF = dataset.F == 1000
# get the values where F == 1000
rows = dataset[maskF]
# get the values where A12 is in column E
rows = rows[rows.E == "A12"]
#remove the we don't want to see
del rows["E"]
del rows["F"]
#Plot the result
rows.plot(xlim=(0,None), ylim=(-1,2))

Upvotes: 2

Related Questions