## [Matplotlib-users] Filtering plots

 [Matplotlib-users] Filtering plots From: Armando Serrano Lombillo - 2007-07-18 08:48:33 Attachments: Message as HTML ```Hello, I have a question. Let's say I have the following data: [1,3,6,1,2,0,0,0,0,1,4,7,9,4,2,4,6,0,0,0,0,0,0,0,0,0,0,0,0,1,3,5,6,7,8] which I want to plot, but I want to omit the zeros, so I would like to do something like: plot(range(1,6), [1,3,6,1,2], 'b') plot(range(10,18), [1,4,7,9,4,2,4,6], 'b') plot(range(30,36), [1,3,5,6,7,8], 'b') savefig('filtered.eps') Is there an elegant way of doing this? Armando. ```

 [Matplotlib-users] Filtering plots From: Armando Serrano Lombillo - 2007-07-18 08:48:33 Attachments: Message as HTML ```Hello, I have a question. Let's say I have the following data: [1,3,6,1,2,0,0,0,0,1,4,7,9,4,2,4,6,0,0,0,0,0,0,0,0,0,0,0,0,1,3,5,6,7,8] which I want to plot, but I want to omit the zeros, so I would like to do something like: plot(range(1,6), [1,3,6,1,2], 'b') plot(range(10,18), [1,4,7,9,4,2,4,6], 'b') plot(range(30,36), [1,3,5,6,7,8], 'b') savefig('filtered.eps') Is there an elegant way of doing this? Armando. ```
 Re: [Matplotlib-users] Filtering plots From: Angel Lopez - 2007-07-18 09:18:43 ```Hola Alejandro, sure, I think this can do the job: write a function to calc your X values: def x(data): X=[] c=1 for i in data: if i>0: X.append(c) c+=1 return X then your data without zeros in a pythonic way: >>> my_y_data=[i for i in data if i>0] with data=[1,3,6,1,2,0,0,0,0,1,4,7,9,4,2,4,6,0,0,0,0,0,0,0,0,0,0,0,0,1,3,5,6,7,8] then you can plot: >>> plot(x(data), my_y_data) Hope I helped !! Suerte. Hasta otra. On 7/18/07, Armando Serrano Lombillo wrote: > Hello, I have a question. > > Let's say I have the following data: > [1,3,6,1,2,0,0,0,0,1,4,7,9,4,2,4,6,0,0,0,0,0,0,0,0,0,0,0,0,1,3,5,6,7,8] > which I want to plot, but I want to omit the zeros, so I would like to do > something like: > plot(range(1,6), [1,3,6,1,2], 'b') > plot(range(10,18), [1,4,7,9,4,2,4,6], 'b') > plot(range(30,36), [1,3,5,6,7,8], 'b') > savefig('filtered.eps') > > Is there an elegant way of doing this? > > Armando. > > ------------------------------------------------------------------------- > This SF.net email is sponsored by DB2 Express > Download DB2 Express C - the FREE version of DB2 express and take > control of your XML. No limits. Just data. Click to get it now. > http://sourceforge.net/powerbar/db2/ > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > ```
 Re: [Matplotlib-users] Filtering plots From: Angel Lopez - 2007-07-18 09:31:45 ```Even in only one line: plot([i+1 for i in range(len(data)) if data[i]>0], [i for i in data if i>0]) ```
 Re: [Matplotlib-users] Filtering plots From: Antonino Ingargiola - 2007-07-18 09:37:46 ```You can do it this way: data = array(data) x = arange(len(data)) plot(x[data!=0], data[data!=0]) Regards, ~ Antonio ```
 Re: [Matplotlib-users] Filtering plots From: Armando Serrano Lombillo - 2007-07-18 10:01:17 Attachments: Message as HTML ```Hello thanks everybody for you fast replies, but unfortunately there's a problem with all the solutions you propose: they plot a line between each of the data sets. It is not the same to do: plot([1,2],[1,1]) plot([7,8],[1,1]) than to do plot([1,2,7,8],[1,1,1,1]) The first example plots two disjoint segments, while the second plots three joint segments. I did code a workaround to plot the data as I wanted but it's something like 20 lines of ugly code and I really think there must be an easier way. By the way, Angel, my name is Armando not Alejandro. ;) On 7/18/07, Armando Serrano Lombillo wrote: > > Hello, I have a question. > > Let's say I have the following data: > [1,3,6,1,2,0,0,0,0,1,4,7,9,4,2,4,6,0,0,0,0,0,0,0,0,0,0,0,0,1,3,5,6,7,8] > which I want to plot, but I want to omit the zeros, so I would like to do > something like: > plot(range(1,6), [1,3,6,1,2], 'b') > plot(range(10,18), [1,4,7,9,4,2,4,6], 'b') > plot(range(30,36), [1,3,5,6,7,8], 'b') > savefig('filtered.eps') > > Is there an elegant way of doing this? > > Armando. > ```
 Re: [Matplotlib-users] Filtering plots From: Angus McMorland - 2007-07-18 10:17:35 ```Hi Armando, On 18/07/07, Armando Serrano Lombillo wrote: > Hello, I have a question. > > Let's say I have the following data: > [1,3,6,1,2,0,0,0,0,1,4,7,9,4,2,4,6,0,0,0,0,0,0,0,0,0,0,0,0,1,3,5,6,7,8] > which I want to plot, but I want to omit the zeros, so I would like to do > something like: > plot(range(1,6), [1,3,6,1,2], 'b') > plot(range(10,18), [1,4,7,9,4,2,4,6], 'b') > plot(range(30,36), [1,3,5,6,7,8], 'b') > savefig('filtered.eps') > > Is there an elegant way of doing this? Do masked arrays achieve what you want? from numpy.core import ma data = n.array([1,3,6,1,2,0,0,0,0,1,4,7,9,4,2,4,6,0,0,0,0,0,0,0,0,0,0,0,0,1,3,5,6,7,8]) masked_data = ma.masked_array(data, mask=(dat==0)) plot(masked_data) Angus. -- AJC McMorland, PhD Student Physiology, University of Auckland ```
 Re: [Matplotlib-users] Filtering plots From: Armando Serrano Lombillo - 2007-07-18 10:56:20 Attachments: Message as HTML ```Yes! matplotlib is beautiful. Thanks everybody for your help. On 7/18/07, Angus McMorland wrote: > > Hi Armando, > > On 18/07/07, Armando Serrano Lombillo wrote: > > Hello, I have a question. > > > > Let's say I have the following data: > > [1,3,6,1,2,0,0,0,0,1,4,7,9,4,2,4,6,0,0,0,0,0,0,0,0,0,0,0,0,1,3,5,6,7,8] > > which I want to plot, but I want to omit the zeros, so I would like to > do > > something like: > > plot(range(1,6), [1,3,6,1,2], 'b') > > plot(range(10,18), [1,4,7,9,4,2,4,6], 'b') > > plot(range(30,36), [1,3,5,6,7,8], 'b') > > savefig('filtered.eps') > > > > Is there an elegant way of doing this? > > Do masked arrays achieve what you want? > > from numpy.core import ma > data = n.array > ([1,3,6,1,2,0,0,0,0,1,4,7,9,4,2,4,6,0,0,0,0,0,0,0,0,0,0,0,0,1,3,5,6,7,8]) > masked_data = ma.masked_array(data, mask=(dat==0)) > plot(masked_data) > > Angus. > -- > AJC McMorland, PhD Student > Physiology, University of Auckland > > ------------------------------------------------------------------------- > This SF.net email is sponsored by DB2 Express > Download DB2 Express C - the FREE version of DB2 express and take > control of your XML. No limits. Just data. Click to get it now. > http://sourceforge.net/powerbar/db2/ > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > ```
 Re: [Matplotlib-users] Filtering plots From: Darren Dale - 2007-07-18 11:11:19 ```On Wednesday 18 July 2007 6:56:18 am Armando Serrano Lombillo wrote: > Yes! > matplotlib is beautiful. Thanks everybody for your help. Here is another way, with numpy arrays: data[data==0]=nan plot(data) > On 7/18/07, Angus McMorland wrote: > > Hi Armando, > > > > On 18/07/07, Armando Serrano Lombillo wrote: > > > Hello, I have a question. > > > > > > Let's say I have the following data: > > > [1,3,6,1,2,0,0,0,0,1,4,7,9,4,2,4,6,0,0,0,0,0,0,0,0,0,0,0,0,1,3,5,6,7,8] > > > which I want to plot, but I want to omit the zeros, so I would like to > > > > do > > > > > something like: > > > plot(range(1,6), [1,3,6,1,2], 'b') > > > plot(range(10,18), [1,4,7,9,4,2,4,6], 'b') > > > plot(range(30,36), [1,3,5,6,7,8], 'b') > > > savefig('filtered.eps') > > > > > > Is there an elegant way of doing this? > > > > Do masked arrays achieve what you want? > > > > from numpy.core import ma > > data = n.array > > ([1,3,6,1,2,0,0,0,0,1,4,7,9,4,2,4,6,0,0,0,0,0,0,0,0,0,0,0,0,1,3,5,6,7,8]) > > masked_data = ma.masked_array(data, mask=(dat==0)) > > plot(masked_data) ```
 Re: [Matplotlib-users] Filtering plots From: Pierre GM - 2007-07-18 15:57:56 ```On Wednesday 18 July 2007 07:11:04 Darren Dale wrote: > Here is another way, with numpy arrays: > > data[data==0]=nan My 2c: That won't work if data is int_, as nan will be treated as 0. I'm fairly partial to masked arrays... ```
 Re: [Matplotlib-users] Filtering plots From: Eric Firing - 2007-07-18 16:41:12 ```Darren Dale wrote: > On Wednesday 18 July 2007 6:56:18 am Armando Serrano Lombillo wrote: >> Yes! >> matplotlib is beautiful. Thanks everybody for your help. > > Here is another way, with numpy arrays: > > data[data==0]=nan > > plot(data) Support for nan in input arrays in mpl is not uniform, though, so the masked array alternative is preferred. Eric ```