[Matplotlib-users] AxesGrid: X axis dates and other axis questions.
(sorry if this is a duplicate post) Jae, Thank you for your help. I found the problem. It was caused by using pyplot.title(). It is working better now. I next have to figure out how to do the following within AxesGrid: 1. How to convert the x axis labels from an integer value representing epoch seconds to a nicely formatted date. I think this has something to do with matplotlib.dates.DateFormatter. I hope that this will remove the 1.25325e9 from the plot. 2. How to minimize or eliminate the white bands on the right and bottom of each axes caused by the axis scale exceeding the data values. 3. How to eliminate (or hide) the first major tic label on the y axis (always 0) so it doesn't overlap with the last tick from the previous y axis. It seems like there may be a different way to approach this than with subplot() Regards, -Ryan * Here's a complete example:* from matplotlib import pyplot from mpl_toolkits.axes_grid import AxesGrid from numpy import arange, linspace, meshgrid, random, transpose # Generate some data x_dim = linspace(125325,125325 + 60*60*24,47) # This is epoch seconds y_dim = arange(0,-2.7,-0.1) z_dim = {} z_dim['chl'] = random.rand(len(x_dim),len(y_dim)) + linspace(5,26,len(y_dim)) z_dim['do'] = random.rand(len(x_dim),len(y_dim)) + linspace(5,10,len(y_dim)) z_dim['turb'] = random.rand(len(x_dim),len(y_dim)) + linspace(4.5,12.5,len(y_dim)) x_grid,y_grid = meshgrid(x_dim,y_dim) x_grid = transpose(x_grid) y_grid = transpose(y_grid) # Start the plotting routines DAP_figure = pyplot.figure(1,(8,8)) #pyplot.title('Title goes here') # *THIS IS THE LINE THAT CAUSES THE EARLIER PROBLEM* pyplot.figtext(0.05,.5,Depth (m),rotation='vertical',verticalalignment='center') # Create a grid of axes with the AxesGrid helper class my_grid = AxesGrid(DAP_figure, 111, # Only one grid in DAP_figure nrows_ncols = (3,1), axes_pad = 0.0, #pad between axes in inches aspect=False, #By default (False), widths and heigths of axes in the grid are scaled independently. If True, they are scaled according to their data limits add_all=True, # Add axes to figures if True (default True) share_all=False, # xaxis yaxis of all axes are shared if True (default False) label_mode = L, # location of tick labels thaw will be displayed. 1 (only the lower left axes), L (left most and bottom most axes), or all cbar_location=right, # right or top cbar_mode=each, # None,single, or each cbar_size=2%, cbar_pad=1%, ) for i,parameter in enumerate(z_dim): ax = my_grid[i].pcolor(x_grid,y_grid,z_dim[parameter]) my_grid[i].set_ylabel(parameter) # Puts a y label on every graph. Eventually we want this labeled only once. my_grid.cbar_axes[i].colorbar(ax) my_grid.cbar_axes[i].axis[right].toggle(ticklabels=True,label=True) my_grid.cbar_axes[i].set_ylabel(units) my_grid[i].axis[bottom].major_ticklabels.set_rotation(45) # pyplot.show() [image: p5R5J.png] On Tue, Dec 8, 2009 at 7:39 PM, Jae-Joon Lee lee.j.j...@gmail.com wrote: Did you test the code in my previous post? If you want to get some help, you need to take your time to create a simple and complete example (which reproduces the problem) that others can easily test. Since I believe the problem is due to the existence of an extra axes, your example don't need to show any images. Please post a simple script that draws a blank AxesGrid and shows extra ticklabels as your current code does. Regards, -JJ -- Return on Information: Google Enterprise Search pays you back Get the facts. http://p.sf.net/sfu/google-dev2dev ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] AxesGrid problem.
Sorry for the delay. I don't know if I ever included my software versions: Python IDLE 2.6.2 matplotlib 0.99.0 numpy 1.4.0rc1 (I was using 1.3.0) Here is more complete code. This is the only place I use matplotlib for anything so I don't think any earlier code should affect the plot. I've included the values of the input variables below and I could include all the code which gets the data and manipulates it if this would help. def plotGrid(x_dim,y_dim,z_dim,long_name,units,contours=16): This will create a frame for all the sub plots. There will be one row (subplot) per parameter. There will be one column. All plots will share their x scale (time) Each row will have its own y scale and legend from matplotlib import pyplot from mpl_toolkits.axes_grid import AxesGrid from numpy import meshgrid, transpose nrows = len(z_dim) # Number of rows print('there are',nrows,'rows') # Confirm that the number of rows is as expected. fig_h_size = 20. # figure width in inches fig_v_size = 8. # figure height in inches dev_mult = 3 # How many standard deviations to mask out. x_grid,y_grid = meshgrid(x_dim,y_dim) x_grid = transpose(x_grid) y_grid = transpose(y_grid) # Start the plotting routines DAP_figure = pyplot.figure(1,(fig_h_size,fig_v_size)) pyplot.title('Title goes here') pyplot.figtext(0.05,.5,Depth (m),rotation='vertical',verticalalignment='center') # Create a grid of axes with the AxesGrid helper class my_grid = AxesGrid(DAP_figure, 111, # Only one grid in DAP_figure nrows_ncols = (nrows,1), axes_pad = 0.0, # pad between axes in inches aspect=False, # By default (False), widths and heigths of axes in the grid are scaled independently. If True, they are scaled according to their data limits add_all=True, # Add axes to figures if True (default True) share_all=True, # xaxis yaxis of all axes are shared if True (default False) label_mode = L, # location of tick labels thaw will be displayed. 1 (only the lower left axes), L (left most and bottom most axes), or all cbar_location=right, # right or top cbar_mode=each, # None,single, or each cbar_size=2%, cbar_pad=1%, ) for i,parameter in enumerate(z_dim): z_dim[parameter] = maskDAP(z_dim[parameter],parameter,dev_mult) #Need to mask each grid ax = my_grid[i].pcolor(x_grid,y_grid,z_dim[parameter]) print('from',x_grid[0][0],'to',x_grid[-1][0]) my_grid[i].set_ylabel(long_name[parameter]) # Puts a y label on every graph. Eventually we want this labeled only once. my_grid.cbar_axes[i].colorbar(ax) my_grid.cbar_axes[i].axis[right].toggle(ticklabels=True,label=True) my_grid.cbar_axes[i].set_ylabel(units[parameter]) # Now show it pyplot.draw() pyplot.show() return x_grid, y_grid, my_grid #Useful only for debugging. There is no code after this. Here are some typical values for the inpit variables if it helps, x_dim, time in epoch seconds, is: array([125325, 1253251800, 1253253600, 1253255400, 1253257200, 1253259000, 1253260800, 1253262600, 1253264400, 1253266200, 1253268000, 1253269800, 1253271600, 1253273400, 1253275200, 1253277000, 1253278800, 1253280600, 1253282400, 1253284200, 1253286000, 1253287800, 1253289600, 1253291400, 1253293200, 1253295000, 1253296800, 1253298600, 1253300400, 1253302200, 1253304000, 1253305800, 1253307600, 1253309400, 1253311200, 1253313000, 1253314800, 1253316600, 1253318400, 1253320200, 1253322000, 1253323800, 1253325600, 1253327400, 1253329200, 1253331000, 1253332800]) y_dim, water depths in meters, is: array([ 0. , -0.1, -0.2, -0.3, -0.4, -0.5, -0.6, -0.7, -0.8, -0.9, -1. , -1.1, -1.2, -1.3, -1.4, -1.5, -1.6, -1.7, -1.8, -1.9, -2. , -2.1, -2.2, -2.3, -2.4, -2.5, -2.6, -2.7]) in the example plot below z_dim is a dictionary with three arrays, 'do','chl','turb'. as an example, z_dim['chl'] (chlorophyl) is a 2D array of the form: masked_array(data = [[-- 14.842718 14.842718 ..., 13.123892 -- --] [-- 15.0 15.0 ..., -- -- --] [-- 13.1241378212 13.1241378212 ..., -- -- --] ..., [-- 12.081481385 12.081481385 ..., 10.3037038589 -- --] [-- 11.0882356451 11.0882356451 ..., 9.95714437393 -- --] [-- 13.4448273754 13.4448273754 ..., -- -- --]], mask = [[ True False False ..., False True True] [ True False False ..., True True True] [ True False False ..., True True True] ..., [ True False False ..., False True True] [ True False False ..., False True True] [ True False False ..., True True True]], fill_value = 1e+20) Here's the plot as it stands now: [image: fgpXr.png] Thank you again for your time. On Fri, Dec 4, 2009 at 4:07 PM, Jae-Joon Lee
Re: [Matplotlib-users] AxesGrid problem.
Than you for your assistance with AxesGrid. Concerning the documentation, on this page: http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/users/overview.htmit says: Name Default Description aspect True aspect of axes then a few lines below: *aspect*By default (False), widths and heigths of axes in the grid are scaled independently. If True, they are scaled according to their data limits (similar to aspect parameter in mpl). *Here is a more complete example of my code: *In the following code, x_grid and y_grid are are arrays created by meshgrid and represent time and water depth respectively. z_dim is a dictionary of one or more arrays of sensor readings corresponding to the depths and times in x_grid and y_grid. from matplotlib import pyplot from mpl_toolkits.axes_grid import AxesGrid nrows = len(z_dim) # Number of rows DAP_figure = pyplot.figure(1,(20,8)) pyplot.figtext(0.05,.5,Depth (m),rotation='vertical',verticalalignment='center') # Create a grid of axes with the AxesGrid helper class my_grid = AxesGrid(DAP_figure, 111, # Only one grid in this figure nrows_ncols = (nrows,1), # one or more rows, but only one column axes_pad = 0.0, #pad between axes in inches aspect=False, # If True, all plots are superimposed upon one another. add_all=True, # not sure why this would ever be False share_all=True, # I think this means that all axes have the same x y scales label_mode = L, # labels for depth on left and time on bottom cbar_location=right, cbar_mode=each, # each axes has a different scale cbar_size=2%, cbar_pad=1%, ) for i,parameter in enumerate(z_dim): z_dim[parameter] = maskDAP(z_dim[parameter],parameter,dev_mult) #Need to mask NaNs and outliers for each grid ax = my_grid[i].pcolor(x_grid,y_grid,z_dim[parameter]) my_grid[i].set_ylabel(long_name[parameter]) # Puts a y label on every graph. my_grid.cbar_axes[i].colorbar(ax) my_grid.cbar_axes[i].axis[right].toggle(ticklabels=True,label=True) my_grid.cbar_axes[i].set_ylabel(units[parameter]) #Puts the units on the right side of the colorbar pyplot.draw() pyplot.show() I do need a separate colorbar for each plot as they are results of different sensors all taken at the same time and depth scales. Here is what I have now: [image: wutSM.png] Which, aside from the extra scale labels on the x and y axis is getting close. Thank You for your help, -Ryan *matplotlib version:* On Thu, Dec 3, 2009 at 4:36 PM, Jae-Joon Lee lee.j.j...@gmail.com wrote: On Thu, Dec 3, 2009 at 3:40 PM, Ryan Neve ryan.n...@gmail.com wrote: I tried all sorts of things, but finally, by setting aspect=False I got it to work. In the documentation, the table says this defaults to True and the explanation of aspect below says it defaults to False. Although I don't entirely understand what is going on, I think this threw me off. So then I had this: Can you be more specific about which documentation says the default aspect is False? This may need to be fixed. Note that AxesGrid is designed for displaying images with aspect=True. Otherwise, you may better stick to the subplot.. [image: 84Kna.png] ... which looks much better, except that there are two sets of x and y axis labels? This seems to have something to do with the colorbar. I've got: To me, there is another axes underneath the AxesGrid. It is hard to tell without a complete code. label_mode = L, cbar_location=right, cbar_mode=each, cbar_size=2%, cbar_pad=0.5% Now I'm trying to get scales and labels on my colorbars. I tried: for i,parameter in enumerate(z_dim): ax = my_grid[i].pcolor(x_grid,y_grid,z_dim[parameter]) # This is the pcolor plot my_grid[i].set_ylabel('Depth') # Correctly puts a y label on every plot. cb = my_grid.cbar_axes[i].colorbar(ax) # Puts in a colorbar for this axes?s cb.set_ylabel(parameter) #It would be nice if this was on the far right next to the colorbar. I don't see it anywhere. Perhaps underneath something? The label of the colorbar is set to invisible by default (this is a bug). So, try something like my_grid.cbar_axes[i].set_ylabel(parameter) my_grid.cbar_axes[i].axis[right].toggle(ticklabels=True, label=True) [image: DPkWz.png] It looks like perhaps the colorbar axes is inside the ax axes rather than besides it? In the demo_grid_with_each_cbarhttp://matplotlib.sourceforge.net/examples/axes_grid/demo_axes_grid.htmlexample, how would you put a scale and label on the colorbar like in this plot:? [image: 58dFK.png] I can put a y_label on each contour plot, but since they all have depth, I'd like to label this only once. Is there a way to label the entire
Re: [Matplotlib-users] AxesGrid problem.
Thank You, I think I have a better understanding. In my figure, there are six axes, three for the plots: grid[i] and three for their colorbars: grid.cbar_axes[i]. I changed my code as you suggested and got something like: [image: UKM0g.png] I tried all sorts of things, but finally, by setting aspect=False I got it to work. In the documentation, the table says this defaults to True and the explanation of aspect below says it defaults to False. Although I don't entirely understand what is going on, I think this threw me off. So then I had this: [image: 84Kna.png] ... which looks much better, except that there are two sets of x and y axis labels? This seems to have something to do with the colorbar. I've got: label_mode = L, cbar_location=right, cbar_mode=each, cbar_size=2%, cbar_pad=0.5% Now I'm trying to get scales and labels on my colorbars. I tried: for i,parameter in enumerate(z_dim): ax = my_grid[i].pcolor(x_grid,y_grid,z_dim[parameter]) # This is the pcolor plot my_grid[i].set_ylabel('Depth') # Correctly puts a y label on every plot. cb = my_grid.cbar_axes[i].colorbar(ax) # Puts in a colorbar for this axes?s cb.set_ylabel(parameter) #It would be nice if this was on the far right next to the colorbar. I don't see it anywhere. Perhaps underneath something? [image: DPkWz.png] It looks like perhaps the colorbar axes is inside the ax axes rather than besides it? In the demo_grid_with_each_cbarhttp://matplotlib.sourceforge.net/examples/axes_grid/demo_axes_grid.htmlexample, how would you put a scale and label on the colorbar like in this plot:? [image: 58dFK.png] I can put a y_label on each contour plot, but since they all have depth, I'd like to label this only once. Is there a way to label the entire AxesGrid (or is that subplot?)? Thank you very much for your help, -Ryan On Wed, Dec 2, 2009 at 10:21 PM, Jae-Joon Lee lee.j.j...@gmail.com wrote: This happens because, when the AxesGrid is created, gca() is set to the last axes, which is the last colobar axes. If you use axes_grid toolkit, you'd better not use pyplot command that works on axes. Instead, use axes method directly. For example, instead of pyplot.pcolor(..) , use ax.pcolor(..). Regards, -JJ On Wed, Dec 2, 2009 at 2:18 PM, Ryan Neve ryan.n...@gmail.com wrote: Hello, I'm trying to use AxesGrid but I'm running into a problem: I can plot a single pcolor plot: [image: 58dFK.png] But when I try to use AxesGrid, my pcolor plot is ending up where I expect my colorbar to be. [image: mEbTA.png] I want to have up to 6 of these plots stacked vertically, sharing a common time axis and y (depth) scale. I'll try to simplify my code to show what I'm doing: # I have arrays x_grid and y_grid for time and water depth. # z_dim is a dictionary of arrays (one for each plot) # In the plot above it has two arrays. from matplotlib import pyplot nrows = len(z_dim) # Number of rows is the number of arrays My_figure = pyplot.figure(1,(8,8)) my_grid = AxesGrid(My_figure, 111, #Is this always 111? nrows_ncols = (nrows,1), # Always one column axes_pad = 0.1, add_all=True, share_all=True, # They all share the same time and depth scales label_mode = L, cbar_location=right, cbar_mode=each, cbar_size=7%, cbar_pad=2%, ) for row_no,parameter in enumerate(z_dim): ax = my_grid[row_no] ax = pyplot.pcolor(x_grid,y_grid,z_dim[parameter]) pyplot.draw() pyplot.show() I eventually want to end up with something like this matlab output (which I didn't generate): [image: jiIaK.png] but without the duplication of x scales. I'm new to pyplot and even after reading the documentation much of this is baffling. -Ryan -- Join us December 9, 2009 for the Red Hat Virtual Experience, a free event focused on virtualization and cloud computing. Attend in-depth sessions from your desk. Your couch. Anywhere. http://p.sf.net/sfu/redhat-sfdev2dev ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users -- Join us December 9, 2009 for the Red Hat Virtual Experience, a free event focused on virtualization and cloud computing. Attend in-depth sessions from your desk. Your couch. Anywhere. http://p.sf.net/sfu/redhat-sfdev2dev___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
[Matplotlib-users] AxesGrid problem.
Hello, I'm trying to use AxesGrid but I'm running into a problem: I can plot a single pcolor plot: [image: 58dFK.png] But when I try to use AxesGrid, my pcolor plot is ending up where I expect my colorbar to be. [image: mEbTA.png] I want to have up to 6 of these plots stacked vertically, sharing a common time axis and y (depth) scale. I'll try to simplify my code to show what I'm doing: # I have arrays x_grid and y_grid for time and water depth. # z_dim is a dictionary of arrays (one for each plot) # In the plot above it has two arrays. from matplotlib import pyplot nrows = len(z_dim) # Number of rows is the number of arrays My_figure = pyplot.figure(1,(8,8)) my_grid = AxesGrid(My_figure, 111, #Is this always 111? nrows_ncols = (nrows,1), # Always one column axes_pad = 0.1, add_all=True, share_all=True, # They all share the same time and depth scales label_mode = L, cbar_location=right, cbar_mode=each, cbar_size=7%, cbar_pad=2%, ) for row_no,parameter in enumerate(z_dim): ax = my_grid[row_no] ax = pyplot.pcolor(x_grid,y_grid,z_dim[parameter]) pyplot.draw() pyplot.show() I eventually want to end up with something like this matlab output (which I didn't generate): [image: jiIaK.png] but without the duplication of x scales. I'm new to pyplot and even after reading the documentation much of this is baffling. -Ryan -- Join us December 9, 2009 for the Red Hat Virtual Experience, a free event focused on virtualization and cloud computing. Attend in-depth sessions from your desk. Your couch. Anywhere. http://p.sf.net/sfu/redhat-sfdev2dev___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] Unwanted lines between contourf() contour levels
Thank you for the suggestion, but I couldn't see a difference with antialiased either True or False. The lines between contour levels remain. I tried a different colormap (spectral) but it had the same effect. I tried more color levels (256) but the effect got worse. I can't find any example pictures online of matplotlib's contourf() producing a smooth plot, I know matlab's does it: http://www.mbari.org/bog/Projects/CentralCal/summary/images/m1_nuts_ts_contour.jpg -Ryan On Wed, Nov 11, 2009 at 5:08 PM, Eric Firing efir...@hawaii.edu wrote: Ryan Neve wrote: Hello, In my filled contour plot: http://imgur.com/vXoCL.png There are faint lines between the contour levels. I think they are yellow since they disappear in the yellow parts of the graph and are most obvious in the red areas. Is there any way to get rid of these lines? The number of contour levels is arbitrary, and I don't need them emphasized with a moire pattern. Try experimenting with the antialiased kwarg in your call to contourf. It is a boolean; see if a value of True or False gives a better result. Eric Thank you, -Ryan -- Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users -- Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] Unwanted lines between contourf() contour levels
Eric, Here's a pcolor plot of the same data: contour_plot = pyplot.pcolor(x_grid,y_grid,z_grid_masked) http://imgur.com/iL4k7.png For contourf I'm using: contour_plot = pyplot.contourf(x_grid,y_grid,z_grid_masked,contour_levels,origin='upper',\ extent=extent,cmap=pyplot.cm.jet) ... where there are 256 evenly spaced contour_levels. Note that we have many more points on the Y (depth) axis than the X (time). Each Y axis column originally had about 50 irregularly spaced data points, but I used scipy.interpolate.interp1d to make my grid even. I then increased the density substantially to smooth the data. I don't know if this matters. I'm not familiar with pcolorfast pcolormesh, but I'll look in to that tomorrow. Many Thanks, -Ryan On Thu, Nov 12, 2009 at 1:11 PM, Eric Firing efir...@hawaii.edu wrote: Ryan Neve wrote: Thank you for the suggestion, but I couldn't see a difference with antialiased either True or False. The lines between contour levels remain. I tried a different colormap (spectral) but it had the same effect. I tried more color levels (256) but the effect got worse. I can't find any example pictures online of matplotlib's contourf() producing a smooth plot, I know matlab's does it: http://www.mbari.org/bog/Projects/CentralCal/summary/images/m1_nuts_ts_contour.jpg That looks to me like a pcolor plot, not a contourf plot, regardless of what the file name says. And, maybe it is my eyes, but it looks to me like there are artifacts in the colorbar. In any case, if you are plotting a very densely sampled data set, you may want to use the Axes.pcolorfast method or the pcolormesh function or method instead of contourf. Contouring, filled or not, is suitable for data in which you want to bring out a moderate number of regions, not for data with highly complex structure and texture, or if you want essentially a smooth color progression. Eric -Ryan On Wed, Nov 11, 2009 at 5:08 PM, Eric Firing efir...@hawaii.edu mailto: efir...@hawaii.edu wrote: Ryan Neve wrote: Hello, In my filled contour plot: http://imgur.com/vXoCL.png There are faint lines between the contour levels. I think they are yellow since they disappear in the yellow parts of the graph and are most obvious in the red areas. Is there any way to get rid of these lines? The number of contour levels is arbitrary, and I don't need them emphasized with a moire pattern. -- Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
[Matplotlib-users] Unwanted lines between contourf() contour levels
Hello, In my filled contour plot: http://imgur.com/vXoCL.png There are faint lines between the contour levels. I think they are yellow since they disappear in the yellow parts of the graph and are most obvious in the red areas. Is there any way to get rid of these lines? The number of contour levels is arbitrary, and I don't need them emphasized with a moire pattern. Thank you, -Ryan -- Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
[Matplotlib-users] Filling in missing samples by interpolating.
Hello, I've got many 1d arrays of data which contain occasional NaNs where there weren't any samples at that depth bin. Something like this... array([np.nan,1,2,3,np.nan,5,6,7,8,np.nan,np.nan,11,12,np.nan,np.nan,np.nan]) But much bigger, and I have hundreds of them. Most NaN's are isolated between two valid values, but they still make my contour plots look terrible. Rather than just mask them, I want to interpolate so my plot doesn't have holes in it where it need not. I want to change any NaN which is preceded and followed by a value to the average of those two values. If it only has one valid neighbor, I want to change it to the values of it's neighbor. Here's a simplified version of my code: from copy import copy import numpy as np sample_array = np.array(([np.nan,1,2,3,np.nan,5,6,7,8,np.nan,np.nan,11,12,np.nan,np.nan,np.nan])) #Make a copy so we aren't working on the original cast = copy(sample_array) #Now iterate over the copy for j,sample in enumerate(cast): # If this sample is a NaN, let's try to interpolate if np.isnan(sample): #Get the neighboring values, but make sure we don't index out of bounds prev_val = cast[max(j-1,0)] next_val = cast[min(j+1,cast.size-1)] print Trying to fix,prev_val,-,sample,-,next_val # First try an average of the neighbors inter_val = 0.5 * (prev_val + next_val) if np.isnan(inter_val): #There must have been an neighboring Nan, so just use the only valid neighbor inter_val = np.nanmax([prev_val,next_val]) if np.isnan(inter_val): printNo changes made else: printFixed to,prev_val,-,inter_val,-,next_val #Now fix the value in the original array sample_array[j] = inter_val After this is run, we have: sample_array = array([1,1,2,3,4,5,6,7,8,8,11,11,12,12,np.nan,np.nan]) This works, but is very slow for something that will be on the back end of a web page. Perhaps something that uses masked arrays and some of the numpy.ma methods? I keep thinking there must be some much more clever way of doing this. -Ryan -- Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
[Matplotlib-users] How to contour plot my water quality data?
Hello, I hope someone can give me a tip to get this working. I have some data that I have manipulated in to the following format: x_dim is a 1D array of sample times (in minutes) array([ 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360, 390, 420, 450, 480, 510, 540, 570, 600, 630, 660, 690, 720, 750, 780, 810, 840, 870, 900, 930, 960, 990, 1020, 1050, 1080, 1110, 1140, 1170, 1200, 1230, 1260, 1290, 1320, 1350, 1380, 1410]) x_dim is often, but not always regularly spaced and will in practice be much much larger. y_dim is a 1D array of sample depths array([ 0., -10., -20., -30., -40., -50., -60., -70., -80., -90., -100., -110., -120., -130., -140., -150., -160., -170., -180., -190., -200., -210.]) y_dim is always regularly spaced and won't get much bigger than this z_dim is a dictionary of 2D arrays of data values where: z_dim['salin'][1,:] is an array of salinity data taken at the second sampling (time 30) with one value for every depth in y_dim: z_dim['salin'][1,:] = array([ NaN, 10.1434, 10.1444, 10.179 , 10.2236, 10.2623, NaN, 10.2104, 10.2104, 10.1981, 10.1585, 10.1287, 10.1047, 10.0997, 10.0701, 10.0651, 10.0519, 10.0355, 10.01666705, 9.9918, 9.9754, NaN]) I put in the numpy.nan where I have no data. I tried to run this through griddata to make sure the times are regular with something like this: import matplotlib, numpy xi = arange(0,x_dim[-1] + 30,30) zi = mlab.griddata(x_dim,y_dim,z_dim['salin'],xi,y_dim) # but it complains that inputs x,y,z must all be 1D arrays of the same length # I can't find any example of griddata that use arrays rather than functions for z. # in my exampley_dim IS regular, so I should be able to skip on to plotting. x_grid,y_grid = meshgrid(x_dim,y_dim) z_grid = transpose(z_dim['salin']) # x_grid, y_grid, and z_grid now have the same shape # Now mask out all the NaNs ma.fix_invalid(z_grid) # I have previously figured out level_min and level_max for this dataset. contour_levels = list(linspace(floor(level_min),ceil(level_max),10)) figure = pyplot.figure() contour_plot = pyplot.contourf(x_grid,y_grid,z_grid,contour_levels) cbar = pyplot.colorbar() pyplot.show() #and that doesn't look right at all. Any tips are greatly appreciated. -- Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users