[Matplotlib-users] Using indexed color maps for images-not getting correct colors
I have been working to display an image of a USGS 7.5 minute quad sheet. These are provided at various locations about the Web. Since the range of colors on these maps is limited, the *.tif files appear to use an indexed color map wherein each pixel has a value 0 to 255 and the color is found from a table with 256 entries having triplets of the RGB in the range of 0-255.I have not been able to sort out how to get the gdal package to give me the color map from within python, so I dumped it from the image file using gdalinfo and then cut and pasted to get the following script: - import numpy as np import matplotlib.pyplot as plt import matplotlib.cm as cm from matplotlib.colors import ListedColormap import osgeo.gdal as gdal from osgeo.gdalconst import * gd = gdal.Open('o37122d1.tif') #Setup to compute the colormap from the RGB triplets from the geotif file div = np.zeros( (256,3), np.float32) div = 255.0 ctab = np.array([ [ 255,255,255], [ 0,0,0], [ 255,255,255], [ 91,159,230], [ 230,45,30], [ 162,96,71], [ 210,255,177], [ 197,101,197], [ 255,240,0], [ 202,225,245], ...snip (deleted many lines:) [ 250,202,250], [ 230,230,230], [ 222,167,146], [ 255,255,255], [ 255,255,255] ], dtype=np.uint8) #Compute colors in range 0.0 to 1.0. fctab= ctab/div usgscm = ListedColormap(fctab, name='usgs',N=None) doq = gd.ReadAsArray() # doq proves to be a uint8 array: 8802 rows and 7066 columns # Cut out a subset from the main array--to large to display and # slow as 'molasses in January' to process:) suba = doq[0:1101 ,0:1801 ] fig = plt.figure() ax = fig.add_subplot(111) ax.imshow(suba, cmap=usgscm, origin='upper') plt.show() - This script does give me an image--but in badly wrong colors:( The script does properly display gray-scaled Digital Ortho-quadrangles using cm.gray as the color map in imshow. Consequently something is not quite correct with respect to the definition or the use of the color map. It appears that each map, and there are about 56,000 of them available on one site, could have its own color map. Thus my application must be able to compute a color map unique to each of the topographic maps. Questions: 1. What am I missing to get imshow to pick out the correct colors from the color map? 2. Should I be using the name, usgs, given in the ListedColormap instance someplace? Not clear to me what role that name plays. 3. Is there a way to use ctab directly in the ListedColormap instance? Class Colormap has a bytes argument, False by default, but I am not yet sure if it has any bearing on my problem. I am using Matplotlib 0.98 with the latest numpy, and gdal. I am not using Basemap, after some study, because it contains far more "horsepower" than my simple topographic-map viewer application requires. Also, this is my first foray into "image processing". I am finding the learning curve a bit steep, as usual, with multiple names for the same thing popping up in descriptions from various sources--business as usual in the computer business:) Thanks from a happy matplotlib user in California! Delbert - Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://sourceforge.net/services/buy/index.php ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] Using indexed color maps for images-not getting correct colors
On Monday 23 June 2008 23:00, Eric Firing wrote: > Delbert Franz wrote: > > I have been working to display an image of a USGS 7.5 minute quad sheet. > > These are provided at various locations about the Web. Since the > > range of colors on these maps is limited, the *.tif files appear to > > use an indexed color map wherein each pixel has a value 0 to 255 and > > the color is found from a table with 256 entries having triplets of > > the RGB in the range of 0-255.I have not been able to sort out > > how to get the gdal package to give me the color map from within python, so > > I dumped it > > from the image file using gdalinfo and then cut and pasted to get > > the following script: > > > > - > > import numpy as np > > import matplotlib.pyplot as plt > > import matplotlib.cm as cm > > from matplotlib.colors import ListedColormap > > from matplotlib.colors import NoNorm > > > > > import osgeo.gdal as gdal > > from osgeo.gdalconst import * > > > > gd = gdal.Open('o37122d1.tif') > > > > #Setup to compute the colormap from the RGB triplets from the geotif file > > div = np.zeros( (256,3), np.float32) > > > > ax = fig.add_subplot(111) > > ax.imshow(suba, cmap=usgscm, origin='upper') > > Instead, try: > > ax.imshow(suba, cmap=usgscm, norm=NoNorm(), origin='upper') > > > > > > plt.show() > > - > > This script does give me an image--but in badly wrong colors:( The script > > does > > properly display gray-scaled Digital Ortho-quadrangles using cm.gray as the > > color > > map in imshow. Consequently something is not quite correct with respect to > > the > > definition or the use of the color map. It appears that each map, and > > there > > are about 56,000 of them available on one site, could have its own color > > map. > > Thus my application must be able to compute a color map unique to each of > > the > > topographic maps. > > > > Questions: > > > > 1. What am I missing to get imshow to pick out the correct colors from the > > color map? > > The default norm will scale your inputs; specifying the norm as a NoNorm > instance will pass the integers through directly, so they will be used > as indices into the colormap. > > > > > > 2. Should I be using the name, usgs, given in the ListedColormap instance > > someplace? > >Not clear to me what role that name plays. > > > > None, really. I can imagine ways in which it could be useful, but > unless you know you need it, consider it optional. > > > > 3. Is there a way to use ctab directly in the ListedColormap instance? > > Class Colormap > > has a bytes argument, False by default, but I am not yet sure if it has any > > bearing > > on my problem. > > No, sorry, but the bytes argument is only in the __call__ method, where > it is used to improve efficiency within mpl. There is no facility for > using ints in the lookup table, and no recognition of 0-255 ints within > mpl colorspecs, hence no way to feed your ctab in directly. Even if > there were, though, I don't think it would make much difference in > plotting time. > > > Eric > > > Eric, That did the trick! The images are displaying properly now. Thanks for the quick help. Delbert - Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://sourceforge.net/services/buy/index.php ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] Tkinter problems
On Monday 23 February 2009, Gregor Skrt wrote: >1. Where can I find a good tutorial or set of examples for > embeding matplotlib in Tkinter ? >2. Problem: I created a simple test with Tkinter. First I plot my > graph on __init__ (it works ok). Then I want to clear graph and > plot on the same canvas with different parameters. The thing is > that plot shows up only when I resize my window. Any idea what > could I be doing wrong ? I was trying draw method but it doasn't > work... > > > Here is my code: > > Thanks for your help. Gregor Skrt > > > > Here are a few links for item #1 in your list. I'm not yet skilled enough to quickly answer #2. In summary, information about Tkinter is scattered about in various places:)--at least my take on it. Here are two links to get you started: http://www.astro.washington.edu/users/rowen/TkinterSummary.html This page has quite a few links to resources on Tkinter: http://wiki.python.org/moin/TkInter There is a complete book on the subject by Grayson, that is from 2000. All the examples were online the last time I checked ( some years ago). Also there are used book copies for about $60 US that can be found from the wiki.python.org link. I am planning to get back into Tkinter soon. I have developed a time-series plotting package but have not touched it in almost two years:( Thus I have to re-learn a few things because I wasn't an expert when I had to move to other things--things that earned me billable hours! One final observation: I find GUI programming to be a challenge but a lot of fun. However, it takes time, at least for me, to work out many of the details. However, matplotlib is an excellent tool to put at the heart of anything used to plot technical data. Almost all of my software development from 1963 on has been in Fortran. Still working on getting my "mind wrapped" around object-oriented software and Python:) Python is my platform of choice for any GUI development that I do. I find it a fantastic language!! Hope this helps your. Delbert -- Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA -OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise -Strategies to boost innovation and cut costs with open source participation -Receive a $600 discount off the registration fee with the source code: SFAD http://p.sf.net/sfu/XcvMzF8H ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
[Matplotlib-users] UnboundLocalError from multiple traces with plot_date in 0.90
I'm back to developing with matplotlib after a bit of a hiatus. I am refactoring an app I wrote about two years ago. I suspect I have uncovered a bug in 0.90 which was not present in 0.87.7. The small test script, which I created to reproduce the problem, fails with the follow traceback in 0.90: [EMAIL PROTECTED]:$ python debug.py Traceback (most recent call last): File "debug.py", line 43, in ? plot_date(jt2, f2, ':') File "/usr/lib/python2.4/site-packages/matplotlib/pylab.py", line 2064, in plot_date ret = gca().plot_date(*args, **kwargs) File "/usr/lib/python2.4/site-packages/matplotlib/axes.py", line 2395, in plot_date self.xaxis_date(tz) File "/usr/lib/python2.4/site-packages/matplotlib/axes.py", line 1564, in xaxis_date formatter = AutoDateFormatter(locator) UnboundLocalError: local variable 'locator' referenced before assignment but running in 0.87.7 produces a figure with two traces. If only one trace is done, 0.90 produces a figure. I can drop back to 0.87.7 but I would prefer to continue working with 0.90. Glad to be back using Python and matplotlib! I'm running on Debian Etch on both machines. 0.90 is compiled from the latest tarball since Etch only has 0.87.7 available. Delbert debug.zpi Description: Zip archive - Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users