[Matplotlib-users] Reliefplot (imshow with shading)
Dear all, I am using matplotlib with a great pleasure, and I enjoy its capabilities. I have recently attended a conference where the invited speaker showed great visualizations of arrays from both experiments and simulations. His plots were basically looking like those produced by imshow, that is a luminance array rendered as a colormap image, but with the additionnal use of a shading, which gives a really great feeling to the image. You can feel the height of each part of the image. I have tried to find what software could have produced such a plot, and found the ReliefPlot function of Mathematica, which has precisely this purpose : rendering a colormap image from an array with a shading to give the perception of relief. The documentation and its examples are self-explanatory : http://reference.wolfram.com/mathematica/ref/ReliefPlot.html (look in particular at the first neat example at the bottom of that page) The two live demonstrations illustrate this plot style quite well too : http://demonstrations.wolfram.com/ReliefShadedElevationMap/ http://demonstrations.wolfram.com/VoronoiImage/ So here are my questions : Is there a trick to generate an image with such a shading in matplotlib ? If not, do you know of a python tool that could help ? Where could I start if I want to code it myself in matplotlib ? Thanks for your help. Best regards, Timothée Lecomte -- Laboratoire Pierre Aigrain, École Normale Supérieure 24, rue Lhomond 75005 Paris -- Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are powering Web 2.0 with engaging, cross-platform capabilities. Quickly and easily build your RIAs with Flex Builder, the Eclipse(TM)based development software that enables intelligent coding and step-through debugging. Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
[Matplotlib-users] Multithreading problem with print_png and font object?
I am using matplotlib's object-oriented API to dynamically generate some graphs served by a web site. The web site is built with Django and I have generally followed the cookbook example I found here: http://www.scipy.org/Cookbook/Matplotlib/Django for serving matplotlib figures under Django. Specifically my code looks like this: from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.figure import Figure def generate_png(request, f, year, cid, pid, ic): # ...snipped code that generates the data to graph... fig = Figure() ax = fig.add_subplot(111) ax.set_title(fig_title) ax.set_xlabel(Score) ax.set_ylabel(Frequency) n, bins2, patches = ax.hist(vals, bins, facecolor='blue', edgecolor='blue') if x is not None: patches[x].set_facecolor('red') patches[x].set_edgecolor('red') fig.legend((patches[x],), ('%s (%d)' % (cname, cval),), 'lower left') canvas = FigureCanvas(fig) canvas.print_png(f) # ... snip remainder ... This works fine, except when I run it under a multi-threaded web server (Apache with mod_wsgi in daemon mode with multi-threaded processes) it sometimes (not always) fails with this traceback: File /home/kmt/django/Django-1.1-alpha-1/django/core/handlers/base.py, line 86, in get_response response = callback(request, *callback_args, **callback_kwargs) File /home/kmt/software/web/xword/acpt/views.py, line 321, in get_png response = generate_png(request, f, year, cid, pid, ic) File /home/kmt/software/web/xword/acpt/views.py, line 308, in generate_png canvas.print_png(f) File /usr/lib/python2.5/site-packages/matplotlib/backends/backend_agg.py, line 305, in print_png FigureCanvasAgg.draw(self) File /usr/lib/python2.5/site-packages/matplotlib/backends/backend_agg.py, line 261, in draw self.figure.draw(self.renderer) File /usr/lib/python2.5/site-packages/matplotlib/figure.py, line 765, in draw legend.draw(renderer) File /usr/lib/python2.5/site-packages/matplotlib/legend.py, line 215, in draw t.draw(renderer) File /usr/lib/python2.5/site-packages/matplotlib/text.py, line 329, in draw ismath=self.is_math_text(line)) File /usr/lib/python2.5/site-packages/matplotlib/backends/backend_agg.py, line 113, in draw_text self._renderer.draw_text_image(font.get_image(), int(x), int(y) + 1, angle, gc) RuntimeError: You must call .set_text() before .get_image() I'm not at all familiar with the internals (truly I'm barely familiar with the public APIs) of matplotlib but it appears from this exception that internally there's a 'font' object being shared between threads here, such that one thread can come in and change the font state resulting in a subsequent error in a different thread that was also in the middle of using that font object? If I protect that block of code above with a thread lock so that only one thread is allowed in at a time, the problem goes away. For reference I'm using the latest matplotlib available in the Ubuntu Intrepid (8.10) repositories, which looks to be 0.98.3. In a brief scan I didn't see anything relevant listed in the what's new page for 0.98.4 (and can't find a what's new in 0.98.5 on the matplotlib web site though that is what is listed as most recent?). Nor can I find anything that looks similar logged as a bug in the tracker. Is there something (besides bracketing all access to the matplotlib code with a thread mutex) that I should be doing to make my use of matplotlib thread safe or does it seem like there's a multi-threading bug in matplotlib here? Thanks for any pointers, Karen -- Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are powering Web 2.0 with engaging, cross-platform capabilities. Quickly and easily build your RIAs with Flex Builder, the Eclipse(TM)based development software that enables intelligent coding and step-through debugging. Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] Problem in robin - basemap?
James Boyle wrote: Jeff, This is something I have noticed recently. If I set lon_0 = 180. using the robin projection , the parallels from 0 to 180 are drawn thicker than those from 180 to 360. Perhaps the 0 to 180 are drawn twice - with some small offset - due some wraparound problem. Enclosed is some code illustrating the problem and the pngs. It is not a glaring difference, but I think real. It also shows up if you look carefully at the output from simpletest.py in your examples. basemap .99.1 matplotlib 0.98.3 OS X 10.4 --Jim m = basemap.Basemap(resolution='c',projection='robin',lon_0=180.) parallels = numpy.arange(-90.,90.,30.) m.drawparallels(parallels,labels=[1,0,0,0],color='r') pylab.title('lon_0=180') pylab.savefig('lon_0-180') pylab.clf() m = basemap.Basemap(resolution='c',projection='robin',lon_0=0.) parallels = numpy.arange(-90.,90.,30.) m.drawparallels(parallels,labels=[1,0,0,0],color='r') pylab.title('lon_0=0') pylab.savefig('lon_0-0') pylab.clf() Jim: I can't reproduce this, so I suspect it's been fixed since basemap 0.99.1. Can you upgrade and let me know whether that fixes it for you? -Jeff (P.S. CC'ing matplotlib-users) -- Jeffrey S. Whitaker Phone : (303)497-6313 Meteorologist FAX: (303)497-6449 NOAA/OAR/PSD R/PSD1Email : jeffrey.s.whita...@noaa.gov 325 BroadwayOffice : Skaggs Research Cntr 1D-113 Boulder, CO, USA 80303-3328 Web: http://tinyurl.com/5telg -- Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are powering Web 2.0 with engaging, cross-platform capabilities. Quickly and easily build your RIAs with Flex Builder, the Eclipse(TM)based development software that enables intelligent coding and step-through debugging. Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] Reliefplot (imshow with shading)
Timothée Lecomte wrote: Dear all, I am using matplotlib with a great pleasure, and I enjoy its capabilities. I have recently attended a conference where the invited speaker showed great visualizations of arrays from both experiments and simulations. His plots were basically looking like those produced by imshow, that is a luminance array rendered as a colormap image, but with the additionnal use of a shading, which gives a really great feeling to the image. You can feel the height of each part of the image. I have tried to find what software could have produced such a plot, and found the ReliefPlot function of Mathematica, which has precisely this purpose : rendering a colormap image from an array with a shading to give the perception of relief. The documentation and its examples are self-explanatory : http://reference.wolfram.com/mathematica/ref/ReliefPlot.html (look in particular at the first neat example at the bottom of that page) The two live demonstrations illustrate this plot style quite well too : http://demonstrations.wolfram.com/ReliefShadedElevationMap/ http://demonstrations.wolfram.com/VoronoiImage/ So here are my questions : Is there a trick to generate an image with such a shading in matplotlib ? If not, do you know of a python tool that could help ? Where could I start if I want to code it myself in matplotlib ? Thanks for your help. Best regards, Timothée Lecomte Timothée: There is nothing built-in, but it would be a nice thing to have. Here's a proof-of-concept hack that follows the approach used in the Generic Mapping Tools (explained here http://www.seismo.ethz.ch/gmt/doc/html/tutorial/node70.html), with some code borrowed from http://www.langarson.com.au/blog/?p=14. It's very rough, but if it looks promising to you I can try to polish it. -Jeff -- Jeffrey S. Whitaker Phone : (303)497-6313 Meteorologist FAX: (303)497-6449 NOAA/OAR/PSD R/PSD1Email : jeffrey.s.whita...@noaa.gov 325 BroadwayOffice : Skaggs Research Cntr 1D-113 Boulder, CO, USA 80303-3328 Web: http://tinyurl.com/5telg import numpy as np import matplotlib.pyplot as plt def rgb_to_hsv_arr(arr): fast rgb_to_hsv using numpy array # adapted from Arnar Flatberg http://www.mail-archive.com/numpy-discuss...@scipy.org/msg06147.html it now handles # NaN properly and mimics colorsys.rgb_to_hsv output arr = arr/255. out = np.empty_like(arr) arr_max = arr.max(-1) delta = arr.ptp(-1) s = delta / arr_max s[delta==0] = 0 # red is max idx = (arr[:,:,0] == arr_max) out[idx, 0] = (arr[idx, 1] - arr[idx, 2]) / delta[idx] # green is max idx = (arr[:,:,1] == arr_max) out[idx, 0] = 2. + (arr[idx, 2] - arr[idx, 0] ) / delta[idx] # blue is max idx = (arr[:,:,2] == arr_max) out[idx, 0] = 4. + (arr[idx, 0] - arr[idx, 1] ) / delta[idx] out[:,:,0] = (out[:,:,0]/6.0) % 1.0 out[:,:,1] = s out[:,:,2] = arr_max return out # code from colorsys module, should numpy'ify def hsv_to_rgb(h, s, v): if s == 0.0: return v, v, v i = int(h*6.0) # XXX assume int() truncates! f = (h*6.0) - i p = v*(1.0 - s) q = v*(1.0 - s*f) t = v*(1.0 - s*(1.0-f)) if i%6 == 0: return v, t, p if i == 1: return q, v, p if i == 2: return p, v, t if i == 3: return p, q, v if i == 4: return t, p, v if i == 5: return v, p, q # Cannot get here def hsv_to_rgb_arr(arr): # vectorize this! out = np.empty(arr.shape, arr.dtype) for i in range(arr.shape[0]): for j in range(arr.shape[1]): h,s,v = arr[i,j,0].item(),arr[i,j,1].item(),arr[i,j,2].item() r,g,b = hsv_to_rgb(h,s,v) out[i,j,0]=r; out[i,j,1]=g; out[i,j,2]=b return out def illumination(idata,azdeg=315.0,altdeg=45.): # convert alt, az to radians az = azdeg*np.pi/180.0 alt = altdeg*np.pi/180.0 # gradient in x and y directions dx, dy = np.gradient(idata) slope = 0.5*np.pi - np.arctan(np.hypot(dx, dy)) aspect = np.arctan2(dx, dy) odata = np.sin(alt)*np.sin(slope) + np.cos(alt)*np.cos(slope)*np.cos(-az -\ aspect - 0.5*np.pi) # rescale to interval -1,1 # +1 means maximum sun exposure and -1 means complete shade. odata = (odata - odata.min())/(odata.max() - odata.min()) odata = 2.*odata - 1. return odata # test data X,Y=np.mgrid[-5:5:0.1,-5:5:0.1] Z=np.sin(X**2+Y**2+1e-4)/(X**2+Y**2+1e-4) # Create the data to be plotted # imagine an artificial sun placed at infinity in # some azimuth and elevation position illuminating our surface. The parts of # the surface that slope toward the sun should brighten while those sides # facing away should become darker; no shadows are cast as a result of # topographic undulations. intensity = illumination(Z,altdeg=10) plt.figure() # plot original image im = plt.imshow(Z,cmap=plt.cm.hsv) # convert to rgb, then rgb to hsv rgb = im.to_rgba(Z) hsv =
[Matplotlib-users] Rotating in imshow
Hello, I was wondering whether there is a way to rotate a grayscale/ colorscale when using imshow. I have been using PGPLOT (a fortran/c plotting library) for many years now, and the equivalent to imshow is called PGGRAY (or PGIMAG). One of the arguments this function takes is a 6-element array TR which is a transformation matrix. From the PGPLOT documentation: The transformation matrix TR is used to calculate the world coordinates of the center of the cell that represents each array element. The world coordinates of the center of the cell corresponding to array element A(I,J) are given by: X = TR(1) + TR(2)*I + TR(3)*J Y = TR(4) + TR(5)*I + TR(6)*J This is actually the same as the ImageMatrix element in the Postscript language: ImageMatrix array (Required) An array of six numbers defining a transformation from user space to image space. (http://www.adobe.com/devnet/postscript/pdfs/PLRM.pdf , page 298) This allows arbitrary rotation/translation of the image to plotting without any loss in quality for vector graphics formats (EPS, PDF, SVG). So far, I have found that imshow has an 'extend' keyword, which is the equivalent of four of the matrix elements, but there is no way to specify the rotation. Is there such a feature in matplotlib already? If not, would it be possible to implement it? Just to be clear, I am not talking about rotating with some kind of interpolation, which would degrade the image (this can be done with PIL). What I want is to be able to specify a transformation matrix which includes rotation, which means that there is no resampling done if I save my plot in a vector graphics format. Thanks in advance for any help! Thomas -- Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are powering Web 2.0 with engaging, cross-platform capabilities. Quickly and easily build your RIAs with Flex Builder, the Eclipse(TM)based development software that enables intelligent coding and step-through debugging. Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] Rotating in imshow
Thomas Robitaille wrote: Hello, I was wondering whether there is a way to rotate a grayscale/ colorscale when using imshow. I have been using PGPLOT (a fortran/c plotting library) for many years now, and the equivalent to imshow is called PGGRAY (or PGIMAG). One of the arguments this function takes is a 6-element array TR which is a transformation matrix. From the PGPLOT documentation: The transformation matrix TR is used to calculate the world coordinates of the center of the cell that represents each array element. The world coordinates of the center of the cell corresponding to array element A(I,J) are given by: X = TR(1) + TR(2)*I + TR(3)*J Y = TR(4) + TR(5)*I + TR(6)*J You could do this with the Axes.pcolormesh method. You could start with an unrotated grid (generated by meshgrid, for example), apply your rotation, and use that transformed grid in pcolormesh. Note that pcolormesh requires the grid for the cell boundaries, not centers. Eric This is actually the same as the ImageMatrix element in the Postscript language: ImageMatrix array (Required) An array of six numbers defining a transformation from user space to image space. (http://www.adobe.com/devnet/postscript/pdfs/PLRM.pdf , page 298) This allows arbitrary rotation/translation of the image to plotting without any loss in quality for vector graphics formats (EPS, PDF, SVG). So far, I have found that imshow has an 'extend' keyword, which is the equivalent of four of the matrix elements, but there is no way to specify the rotation. Is there such a feature in matplotlib already? If not, would it be possible to implement it? Just to be clear, I am not talking about rotating with some kind of interpolation, which would degrade the image (this can be done with PIL). What I want is to be able to specify a transformation matrix which includes rotation, which means that there is no resampling done if I save my plot in a vector graphics format. Thanks in advance for any help! Thomas -- Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are powering Web 2.0 with engaging, cross-platform capabilities. Quickly and easily build your RIAs with Flex Builder, the Eclipse(TM)based development software that enables intelligent coding and step-through debugging. Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users -- Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are powering Web 2.0 with engaging, cross-platform capabilities. Quickly and easily build your RIAs with Flex Builder, the Eclipse(TM)based development software that enables intelligent coding and step-through debugging. Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] Reliefplot (imshow with shading)
Jeff Whitaker wrote: Timothée Lecomte wrote: Dear all, I am using matplotlib with a great pleasure, and I enjoy its capabilities. I have recently attended a conference where the invited speaker showed great visualizations of arrays from both experiments and simulations. His plots were basically looking like those produced by imshow, that is a luminance array rendered as a colormap image, but with the additionnal use of a shading, which gives a really great feeling to the image. You can feel the height of each part of the image. I have tried to find what software could have produced such a plot, and found the ReliefPlot function of Mathematica, which has precisely this purpose : rendering a colormap image from an array with a shading to give the perception of relief. The documentation and its examples are self-explanatory : http://reference.wolfram.com/mathematica/ref/ReliefPlot.html (look in particular at the first neat example at the bottom of that page) The two live demonstrations illustrate this plot style quite well too : http://demonstrations.wolfram.com/ReliefShadedElevationMap/ http://demonstrations.wolfram.com/VoronoiImage/ So here are my questions : Is there a trick to generate an image with such a shading in matplotlib ? If not, do you know of a python tool that could help ? Where could I start if I want to code it myself in matplotlib ? Thanks for your help. Best regards, Timothée Lecomte Timothée: There is nothing built-in, but it would be a nice thing to have. Here's a proof-of-concept hack that follows the approach used in the Generic Mapping Tools (explained here http://www.seismo.ethz.ch/gmt/doc/html/tutorial/node70.html), with some code borrowed from http://www.langarson.com.au/blog/?p=14. It's very rough, but if it looks promising to you I can try to polish it. -Jeff Found a bug, here's a fixed version. -Jeff -- Jeffrey S. Whitaker Phone : (303)497-6313 Meteorologist FAX: (303)497-6449 NOAA/OAR/PSD R/PSD1Email : jeffrey.s.whita...@noaa.gov 325 BroadwayOffice : Skaggs Research Cntr 1D-113 Boulder, CO, USA 80303-3328 Web: http://tinyurl.com/5telg import numpy as np import matplotlib.pyplot as plt def rgb_to_hsv_arr(arr): fast rgb_to_hsv using numpy array # adapted from Arnar Flatberg http://www.mail-archive.com/numpy-discuss...@scipy.org/msg06147.html it now handles # NaN properly and mimics colorsys.rgb_to_hsv output arr = arr/255. out = np.empty_like(arr) arr_max = arr.max(-1) delta = arr.ptp(-1) s = delta / arr_max s[delta==0] = 0 # red is max idx = (arr[:,:,0] == arr_max) out[idx, 0] = (arr[idx, 1] - arr[idx, 2]) / delta[idx] # green is max idx = (arr[:,:,1] == arr_max) out[idx, 0] = 2. + (arr[idx, 2] - arr[idx, 0] ) / delta[idx] # blue is max idx = (arr[:,:,2] == arr_max) out[idx, 0] = 4. + (arr[idx, 0] - arr[idx, 1] ) / delta[idx] out[:,:,0] = (out[:,:,0]/6.0) % 1.0 out[:,:,1] = s out[:,:,2] = arr_max return out # code from colorsys module, should numpy'ify def hsv_to_rgb(h, s, v): if s == 0.0: return v, v, v i = int(h*6.0) # XXX assume int() truncates! f = (h*6.0) - i p = v*(1.0 - s) q = v*(1.0 - s*f) t = v*(1.0 - s*(1.0-f)) if i%6 == 0: return v, t, p if i == 1: return q, v, p if i == 2: return p, v, t if i == 3: return p, q, v if i == 4: return t, p, v if i == 5: return v, p, q # Cannot get here def hsv_to_rgb_arr(arr): # vectorize this! out = np.empty(arr.shape, arr.dtype) for i in range(arr.shape[0]): for j in range(arr.shape[1]): h,s,v = arr[i,j,0].item(),arr[i,j,1].item(),arr[i,j,2].item() r,g,b = hsv_to_rgb(h,s,v) out[i,j,0]=r; out[i,j,1]=g; out[i,j,2]=b return out def illumination(idata,azdeg=315.0,altdeg=45.): # convert alt, az to radians az = azdeg*np.pi/180.0 alt = altdeg*np.pi/180.0 # gradient in x and y directions dx, dy = np.gradient(idata) slope = 0.5*np.pi - np.arctan(np.hypot(dx, dy)) aspect = np.arctan2(dx, dy) odata = np.sin(alt)*np.sin(slope) + np.cos(alt)*np.cos(slope)*np.cos(-az -\ aspect - 0.5*np.pi) # rescale to interval -1,1 # +1 means maximum sun exposure and -1 means complete shade. odata = (odata - odata.min())/(odata.max() - odata.min()) odata = 2.*odata - 1. return odata # test data X,Y=np.mgrid[-5:5:0.1,-5:5:0.1] Z=X+Y+np.sin(X**2+Y**2) # imagine an artificial sun placed at infinity in # some azimuth and elevation position illuminating our surface. The parts of # the surface that slope toward the sun should brighten while those sides # facing away should become darker; no shadows are cast as a result of # topographic undulations. intensity = illumination(Z) plt.figure() # plot original image im = plt.imshow(Z,cmap=plt.cm.copper) # convert to rgb, then rgb to hsv rgb =
Re: [Matplotlib-users] Rotating in imshow
Eric Firing wrote: Thomas Robitaille wrote: Hello, I was wondering whether there is a way to rotate a grayscale/ colorscale when using imshow. I have been using PGPLOT (a fortran/c plotting library) for many years now, and the equivalent to imshow is called PGGRAY (or PGIMAG). One of the arguments this function takes is a 6-element array TR which is a transformation matrix. From the PGPLOT documentation: The transformation matrix TR is used to calculate the world coordinates of the center of the cell that represents each array element. The world coordinates of the center of the cell corresponding to array element A(I,J) are given by: X = TR(1) + TR(2)*I + TR(3)*J Y = TR(4) + TR(5)*I + TR(6)*J You could do this with the Axes.pcolormesh method. You could start with an unrotated grid (generated by meshgrid, for example), apply your rotation, and use that transformed grid in pcolormesh. Note that pcolormesh requires the grid for the cell boundaries, not centers. It should work with imshow() as well if you can set the affine component of the transform to the desired values. Which it looks like you can in Affine2D(). (The affine matrix is the elements of TR listed above, it appears.) I have not tried to do this, however -- just saying that I think it's possible. -Andrew -- Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are powering Web 2.0 with engaging, cross-platform capabilities. Quickly and easily build your RIAs with Flex Builder, the Eclipse(TM)based development software that enables intelligent coding and step-through debugging. Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] Rotating in imshow
It looks like rotation/translation should be easy to do with Affine2D, so I tried using it, but I can't seem to get it to work as expected - here is an example of how I am using it: import numpy as np from matplotlib.pyplot import * from matplotlib.transforms import Affine2D im = np.random.random((10,10)) tr = Affine2D().rotate_deg(45.) fig = figure() ax = fig.add_subplot(111) ax.imshow(im,transform=tr) fig.canvas.draw() Am I doing something wrong? Thanks! Thomas On Mar 13, 2009, at 5:20 PM, Andrew Straw wrote: Eric Firing wrote: Thomas Robitaille wrote: Hello, I was wondering whether there is a way to rotate a grayscale/ colorscale when using imshow. I have been using PGPLOT (a fortran/c plotting library) for many years now, and the equivalent to imshow is called PGGRAY (or PGIMAG). One of the arguments this function takes is a 6-element array TR which is a transformation matrix. From the PGPLOT documentation: The transformation matrix TR is used to calculate the world coordinates of the center of the cell that represents each array element. The world coordinates of the center of the cell corresponding to array element A(I,J) are given by: X = TR(1) + TR(2)*I + TR(3)*J Y = TR(4) + TR(5)*I + TR(6)*J You could do this with the Axes.pcolormesh method. You could start with an unrotated grid (generated by meshgrid, for example), apply your rotation, and use that transformed grid in pcolormesh. Note that pcolormesh requires the grid for the cell boundaries, not centers. It should work with imshow() as well if you can set the affine component of the transform to the desired values. Which it looks like you can in Affine2D(). (The affine matrix is the elements of TR listed above, it appears.) I have not tried to do this, however -- just saying that I think it's possible. -Andrew -- Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are powering Web 2.0 with engaging, cross-platform capabilities. Quickly and easily build your RIAs with Flex Builder, the Eclipse(TM)based development software that enables intelligent coding and step-through debugging. Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] Rotating in imshow
Thanks to both of you for your help! I had spotted the transform argument in imshow, but didn't manage to find any information about how to use it. The Affine2D method looks like it will help, so I should be all set now. Thanks! Thomas On Mar 13, 2009, at 5:20 PM, Andrew Straw wrote: Eric Firing wrote: Thomas Robitaille wrote: Hello, I was wondering whether there is a way to rotate a grayscale/ colorscale when using imshow. I have been using PGPLOT (a fortran/c plotting library) for many years now, and the equivalent to imshow is called PGGRAY (or PGIMAG). One of the arguments this function takes is a 6-element array TR which is a transformation matrix. From the PGPLOT documentation: The transformation matrix TR is used to calculate the world coordinates of the center of the cell that represents each array element. The world coordinates of the center of the cell corresponding to array element A(I,J) are given by: X = TR(1) + TR(2)*I + TR(3)*J Y = TR(4) + TR(5)*I + TR(6)*J You could do this with the Axes.pcolormesh method. You could start with an unrotated grid (generated by meshgrid, for example), apply your rotation, and use that transformed grid in pcolormesh. Note that pcolormesh requires the grid for the cell boundaries, not centers. It should work with imshow() as well if you can set the affine component of the transform to the desired values. Which it looks like you can in Affine2D(). (The affine matrix is the elements of TR listed above, it appears.) I have not tried to do this, however -- just saying that I think it's possible. -Andrew -- Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are powering Web 2.0 with engaging, cross-platform capabilities. Quickly and easily build your RIAs with Flex Builder, the Eclipse(TM)based development software that enables intelligent coding and step-through debugging. Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
[Matplotlib-users] plotting without certain axes in matplotlib
hi all, i'm trying to generate a very simple plot, where only the left y axis and the bottom x axis are present. i.e. there is no top x axis or right y axis... this is the default for many plotting packages. in matlab, one can do this as follows: x = rand(1,100); hist(x) set(gca, 'Box', 'off', 'LineWidth', 1); so the set(gca, 'Box', 'off'...) command does the job. what's the analogous matplotlib command? i would like to do this preferably without any external packages. I attach the figure so you can see what i mean... thank you. attachment: matlab.png-- Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are powering Web 2.0 with engaging, cross-platform capabilities. Quickly and easily build your RIAs with Flex Builder, the Eclipse(TM)based development software that enables intelligent coding and step-through debugging. Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
[Matplotlib-users] parsing tab separated data into arrays
hi all, what's the most efficient / preferred python way of parsing tab separated data into arrays? for example if i have a file containing two columns one corresponding to names the other numbers: col1\t col 2 joe\t 12.3 jane \t 155.0 i'd like to parse into an array() such that i can do: mydata[:, 0] and mydata[:, 1] to easily access all the columns. right now i can iterate through the file, parse it manually using the split('\t') command and construct a list out of it, then convert it to arrays. but there must be a better way? also, my first column is just a name, and so it is variable in length -- is there still a way to store it as an array so i can access: mydata[:, 0] to get all the names (as a list)? thank you. -- Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are powering Web 2.0 with engaging, cross-platform capabilities. Quickly and easily build your RIAs with Flex Builder, the Eclipse(TM)based development software that enables intelligent coding and step-through debugging. Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] parsing tab separated data into arrays
per freem wrote: hi all, what's the most efficient / preferred python way of parsing tab separated data into arrays? for example if i have a file containing two Check out the python csv module. Documentation at http://docs.python.org/library/csv.html JLS -- Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are powering Web 2.0 with engaging, cross-platform capabilities. Quickly and easily build your RIAs with Flex Builder, the Eclipse(TM)based development software that enables intelligent coding and step-through debugging. Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] Rotating in imshow
Thomas Robitaille wrote: It looks like rotation/translation should be easy to do with Affine2D, so I tried using it, but I can't seem to get it to work as expected - here is an example of how I am using it: Based on a quick look at image.py and _image.cpp, it appears that there is a low-level capability to rotate an image in the latter, but no support at higher levels. It also looks to me like adding that support would not be trivial--doing it right would take more than just calling the low-level apply_rotation method. Mike D. would be the expert on this, though. Eric import numpy as np from matplotlib.pyplot import * from matplotlib.transforms import Affine2D im = np.random.random((10,10)) tr = Affine2D().rotate_deg(45.) fig = figure() ax = fig.add_subplot(111) ax.imshow(im,transform=tr) fig.canvas.draw() Am I doing something wrong? Thanks! Thomas On Mar 13, 2009, at 5:20 PM, Andrew Straw wrote: Eric Firing wrote: Thomas Robitaille wrote: Hello, I was wondering whether there is a way to rotate a grayscale/ colorscale when using imshow. I have been using PGPLOT (a fortran/c plotting library) for many years now, and the equivalent to imshow is called PGGRAY (or PGIMAG). One of the arguments this function takes is a 6-element array TR which is a transformation matrix. From the PGPLOT documentation: The transformation matrix TR is used to calculate the world coordinates of the center of the cell that represents each array element. The world coordinates of the center of the cell corresponding to array element A(I,J) are given by: X = TR(1) + TR(2)*I + TR(3)*J Y = TR(4) + TR(5)*I + TR(6)*J You could do this with the Axes.pcolormesh method. You could start with an unrotated grid (generated by meshgrid, for example), apply your rotation, and use that transformed grid in pcolormesh. Note that pcolormesh requires the grid for the cell boundaries, not centers. It should work with imshow() as well if you can set the affine component of the transform to the desired values. Which it looks like you can in Affine2D(). (The affine matrix is the elements of TR listed above, it appears.) I have not tried to do this, however -- just saying that I think it's possible. -Andrew -- Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are powering Web 2.0 with engaging, cross-platform capabilities. Quickly and easily build your RIAs with Flex Builder, the Eclipse(TM)based development software that enables intelligent coding and step-through debugging. Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] Rotating in imshow
It looks like rotation/translation should be easy to do with Affine2D, so I tried using it, but I can't seem to get it to work as expected - here is an example of how I am using it: Based on a quick look at image.py and _image.cpp, it appears that there is a low-level capability to rotate an image in the latter, but no support at higher levels. It also looks to me like adding that support would not be trivial--doing it right would take more than just calling the low-level apply_rotation method. Mike D. would be the expert on this, though. Does this mean that the transform= keyword has no effect on imshow in general? I tried doing a simple image translation, and this didn't work either: import numpy as np from matplotlib.pyplot import * from matplotlib.transforms import Affine2D im = np.random.random((10,10)) tr = Affine2D().translate(10.,10.) fig = figure() ax = fig.add_subplot(111) ax.imshow(im,transform=tr) fig.canvas.draw() I attempted to use the pcolormesh() method, which worked, but is impractical, as a 1000x1000 image produces a 300Mb EPS file when plotted in this way. Thanks, Thomas Eric import numpy as np from matplotlib.pyplot import * from matplotlib.transforms import Affine2D im = np.random.random((10,10)) tr = Affine2D().rotate_deg(45.) fig = figure() ax = fig.add_subplot(111) ax.imshow(im,transform=tr) fig.canvas.draw() Am I doing something wrong? Thanks! Thomas On Mar 13, 2009, at 5:20 PM, Andrew Straw wrote: Eric Firing wrote: Thomas Robitaille wrote: Hello, I was wondering whether there is a way to rotate a grayscale/ colorscale when using imshow. I have been using PGPLOT (a fortran/c plotting library) for many years now, and the equivalent to imshow is called PGGRAY (or PGIMAG). One of the arguments this function takes is a 6-element array TR which is a transformation matrix. From the PGPLOT documentation: The transformation matrix TR is used to calculate the world coordinates of the center of the cell that represents each array element. The world coordinates of the center of the cell corresponding to array element A(I,J) are given by: X = TR(1) + TR(2)*I + TR(3)*J Y = TR(4) + TR(5)*I + TR(6)*J You could do this with the Axes.pcolormesh method. You could start with an unrotated grid (generated by meshgrid, for example), apply your rotation, and use that transformed grid in pcolormesh. Note that pcolormesh requires the grid for the cell boundaries, not centers. It should work with imshow() as well if you can set the affine component of the transform to the desired values. Which it looks like you can in Affine2D(). (The affine matrix is the elements of TR listed above, it appears.) I have not tried to do this, however -- just saying that I think it's possible. -Andrew -- Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are powering Web 2.0 with engaging, cross-platform capabilities. Quickly and easily build your RIAs with Flex Builder, the Eclipse(TM)based development software that enables intelligent coding and step-through debugging. Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] parsing tab separated data into arrays
On Fri, Mar 13, 2009 at 5:18 PM, per freem perfr...@gmail.com wrote: hi all, what's the most efficient / preferred python way of parsing tab separated data into arrays? for example if i have a file containing two columns one corresponding to names the other numbers: col1\t col 2 joe\t 12.3 jane \t 155.0 i'd like to parse into an array() such that i can do: mydata[:, 0] and mydata[:, 1] to easily access all the columns. right now i can iterate through the file, parse it manually using the split('\t') command and construct a list out of it, then convert it to arrays. but there must be a better way? also, my first column is just a name, and so it is variable in length -- is there still a way to store it as an array so i can access: mydata[:, 0] to get all the names (as a list)? Try matplotlib.mlab.csv2rec or numpy.loadtxt Ryan -- Ryan May Graduate Research Assistant School of Meteorology University of Oklahoma Sent from: Norman Oklahoma United States. -- Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are powering Web 2.0 with engaging, cross-platform capabilities. Quickly and easily build your RIAs with Flex Builder, the Eclipse(TM)based development software that enables intelligent coding and step-through debugging. Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] Rotating in imshow
Thomas Robitaille wrote: It looks like rotation/translation should be easy to do with Affine2D, so I tried using it, but I can't seem to get it to work as expected - here is an example of how I am using it: Based on a quick look at image.py and _image.cpp, it appears that there is a low-level capability to rotate an image in the latter, but no support at higher levels. It also looks to me like adding that support would not be trivial--doing it right would take more than just calling the low-level apply_rotation method. Mike D. would be the expert on this, though. Does this mean that the transform= keyword has no effect on imshow in general? It does look like it is ignored. It is a kwarg for Artists that is not supported by all. The fact that one can specify it and get no feedback is a bug. I attempted to use the pcolormesh() method, which worked, but is impractical, as a 1000x1000 image produces a 300Mb EPS file when plotted in this way. There is some infrastructure for handling this via selective rasterization of artists, but I can never remember exactly what its status is; I don't see anything in the examples. The topic comes up on the list at perhaps 6-month intervals. Personally, I would very much like to see the selective rasterization capability fully developed and exposed, complete with documentation and examples; it is important for exactly the reason you note above. It is not something I will be able to work on myself, unfortunately. Eric -- Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are powering Web 2.0 with engaging, cross-platform capabilities. Quickly and easily build your RIAs with Flex Builder, the Eclipse(TM)based development software that enables intelligent coding and step-through debugging. Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users