Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I ran in one more problem - I have multiple files with the following format:

Freq A B

1000 1.2 0.0014

100 1.2 0.00013

10 1.2 0.0012

all files are in the same folder; up to now I am able to read all files, do the calculations I want, and then save one large file with all the needed data (see below for code)

before I save the data I want to:

Break the data into the number of the original files, with the new data, and use the same name as the input name (N in the code is the number of lines per file, I use that to be able to identify how many files I have from the total number of lines)

and, to plot all different data in one plot (assume that I have 3 files like the above, I want a plot with with 3 lines, all on the same plot) - I cannot seem to be able to do that since all my tries give me just one single line

* UPDATE - I can read the data, and putting everithing in the first loop, then I can go over all data - now I want to automate the saving function, but the code (see below a simplified version) saves only one data set (maybe overwrites the text files?) * UPDATE 2 - this now works - needed to add the save function in the 2nd loop

import os
import numpy as np

datadirectory = '/media/data'
os.chdir( datadirectory)

listing = os.listdir(datadirectory) 
my_array=np.zeros(shape=(0,3))

for infile in listing: 
   dataset = open(infile).readlines()
   data = np.genfromtxt(dataset, usecols=(0,1,2))
   my_array = np.vstack((my_array, data))
   lta= my_array

SOME PROCESSING HERE - lta now has 5 columns

   Results=np.column_stack((lta[:,0], lta[:,1], lta[:,2], lta[:,3], lta[:,4]))
   for i in listing:
       date = i
       np.savetxt((os.path.join(resultpath, date)) + '.txt', Results, fmt='%s', delimiter='\t') 
   my_array = np.vstack((my_array, Results))

Plot data - IN my_array array I have all data, and every N rows (given number) I have a different dataset - Iwant to plot all data in the same graph, and every N rows I want to change symbol color

import matplotlib.pylab as plt

plt.figure(figsize=(10,5))
#graph_axes = plt.subplot(N,2,1)
graph = plt.semilogx(my_array[:,0], my_array[:,2])
plt.ylim(0, 25)
plt.xlim(0.1, 1000)
plt.show()
plt.savefig(os.path.join(resultpath, 'image.png'))

any help / guidance is appreciated!

share|improve this question
 
Can you give the smallest possible program where you try to plot two functions but end up with one line? –  Guy Sirton Feb 2 at 4:54
 
I didn't mean plot 2 functions; I meant separating the one function, from the number of files I read, and plot them with separate color - if I use matplolib.pylab and plot (freq, phase) then everything is plotted as one line (points) - but I need to differentiate (e.g. every n lines to change the color) -does this make sense? I will try to make it clearer with a piece of code in the following few days –  Dimitris Feb 2 at 21:11
 
It's been a little while since I used matplotlib. If you actually posted some code that does the plot and doesn't work it would probably trigger my memory and I'd be able to help :-) –  Guy Sirton Feb 3 at 2:48

1 Answer

up vote 0 down vote accepted

It'd be better if you could provide a sample of the contents in my_array, but for what I understand, this code could help you achieve what you need. First, here's the code I wrote to fake the data.

import numpy as np
from random import random

# generate fake data
num_files = 5
N = 20
my_array = []
for f in range(num_files):  # simulate multiple files
    for n in range(N):  # simulate multiple samples per file
        # fake data
        my_array.append([10 ** n / N, random() + f, 10 + f + random()])
my_array = np.asarray(my_array)

Hopefully, at this point the contents of my_array are similar to what you have (I only simulated 3 columns instead of the 5 you have after processing the data, but that shouldn't affect the plotting code that follows:

import matplotlib.pyplot as plt

# now plot it
for i in range(0, num_files * N, N):
    # plot column-0 vs column-1 in one subplot
    plt.subplot(2, 1, 1)
    plt.semilogx(my_array[i:i + N, 0], my_array[i:i + N, 1])
    plt.hold(True)
    # plot column-0 vs column-2 in another subplot
    plt.subplot(2, 1, 2)
    plt.semilogx(my_array[i:i + N, 0], my_array[i:i + N, 2])
    plt.hold(True)
plt.show()

The trick is to use plt.hold(True), which will preserve whatever was plotted before, and will assign a new color to the next data to be plotted. This solution will also work if N is not constant (but you'll have to modify how you loop through the data).

Hope that helps!

share|improve this answer
 
thank you! that works great –  Dimitris Feb 5 at 14:53
 
I'm glad it worked. If it answered your question, would you mind marking it as the answer ;-) –  jorgeh Feb 5 at 20:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.