- Timestamp:
- 20/01/12 14:43:50 (4 months ago)
- File:
-
- 1 edited
-
trunk/bindings/python/nxs/tree.py (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/bindings/python/nxs/tree.py
r1804 r1805 1 1 #!/usr/bin/env python 2 # This program is public domain 2 # This program is public domain 3 # Author: Paul Kienzle, Ray Osborn 3 4 4 5 """ … … 218 219 class. The plotter class has one method:: 219 220 220 plot(signal, axes, entry, title )221 plot(signal, axes, entry, title, format, **opts) 221 222 222 223 where signal is the field containing the data, axes are the fields listing the … … 772 773 print "yielding",self.nxname,self.nxclass 773 774 if False: yield 774 775 775 776 776 def dir(self,attrs=False,recursive=False): … … 1651 1651 """ 1652 1652 1653 def plot(self, signal, axes, title, errors, **opts): 1653 def plot(self, signal, axes, title, errors, fmt, 1654 xmin, xmax, ymin, ymax, zmin, zmax, **opts): 1654 1655 """ 1655 1656 Plot the data entry. … … 1658 1659 """ 1659 1660 try: 1660 import pylab1661 import matplotlib.pyplot as plt 1661 1662 except ImportError: 1662 1663 raise NeXusError, "Plotting package not available." 1664 1665 over = False 1663 1666 if "over" in opts.keys(): 1664 over = True1667 if opts["over"]: over = True 1665 1668 del opts["over"] 1666 else: 1667 over = False 1668 if not over: pylab.clf() 1669 1669 1670 log = logx = logy = False 1670 1671 if "log" in opts.keys(): 1671 logplot= True1672 if opts["log"]: log = True 1672 1673 del opts["log"] 1673 else: 1674 logplot = False 1674 if "logy" in opts.keys(): 1675 if opts["logy"]: logy = True 1676 del opts["logy"] 1677 if "logx" in opts.keys(): 1678 if opts["logx"]: logx = True 1679 del opts["logx"] 1680 1681 if over: 1682 plt.autoscale(enable=False) 1683 else: 1684 plt.autoscale(enable=True) 1685 plt.clf() 1675 1686 1676 1687 # Provide a new view of the data if there is a dimension of length 1 … … 1685 1696 #One-dimensional Plot 1686 1697 if len(data.shape) == 1: 1698 plt.ioff() 1687 1699 if hasattr(signal, 'units'): 1688 1700 if not errors and signal.units == 'counts': 1689 1701 errors = NXfield(np.sqrt(data)) 1690 if logplot: 1691 data = np.log10(np.clip(data,0,1e8)) 1692 if errors: ebars = np.log10(errors) 1693 elif errors: 1702 if errors: 1694 1703 ebars = errors.nxdata 1695 if errors: 1696 myopts=copy(opts) 1697 myopts.setdefault('fmt','o') 1698 myopts.setdefault('linestyle','None') 1699 pylab.scatter(axis_data[0], data, **opts) 1700 pylab.errorbar(axis_data[0], data, ebars, **myopts) 1704 plt.errorbar(axis_data[0], data, ebars, fmt=fmt, **opts) 1701 1705 else: 1702 p ylab.scatter(axis_data[0], data, **opts)1706 plt.plot(axis_data[0], data, fmt, **opts) 1703 1707 if not over: 1704 pylab.xlabel(label(axes[0])) 1705 pylab.ylabel(label(signal)) 1706 pylab.title(title) 1708 ax = plt.gca() 1709 xlo, xhi = ax.set_xlim(auto=True) 1710 ylo, yhi = ax.set_ylim(auto=True) 1711 if xmin: xlo = xmin 1712 if xmax: xhi = xmax 1713 ax.set_xlim(xlo, xhi) 1714 if ymin: ylo = ymin 1715 if ymax: yhi = ymax 1716 ax.set_ylim(ylo, yhi) 1717 if logx: ax.set_xscale('symlog') 1718 if log or logy: ax.set_yscale('symlog') 1719 plt.xlabel(label(axes[0])) 1720 plt.ylabel(label(signal)) 1721 plt.title(title) 1722 plt.ion() 1723 plt.show() 1707 1724 1708 1725 #Two dimensional plot 1709 1726 else: 1727 from matplotlib.image import NonUniformImage 1728 from matplotlib.colors import LogNorm 1729 1710 1730 if len(data.shape) > 2: 1711 1731 slab = [slice(None), slice(None)] … … 1714 1734 data = data[slab].view().reshape(data.shape[:2]) 1715 1735 print "Warning: Only the top 2D slice of the data is plotted" 1716 #from api.nexus import meshgl 1717 #gridplot = meshgl.pcolor_gl 1718 #gridplot = pylab.pcolormesh 1719 gridplot = imshow_irregular 1720 if logplot: 1721 gridplot(axis_data[0], axis_data[1], 1722 np.log10(np.clip(data,0.,1e8)+1).T, **opts) 1736 1737 x = axis_data[0] 1738 y = axis_data[1] 1739 if not zmin: zmin = np.min(data) 1740 if not zmax: zmax = np.max(data) 1741 z = np.clip(data,zmin,zmax).T 1742 1743 ax = plt.gca() 1744 extent = (x[0],x[-1],y[0],y[-1]) 1745 if log: 1746 opts["norm"] = LogNorm() 1747 if z.min() < 1e-8: 1748 z = np.clip(z,0.1,zmax) 1749 1750 im = NonUniformImage(ax, extent=extent, origin=None, **opts) 1751 im.set_data(x,y,z) 1752 ax.images.append(im) 1753 xlo, xhi = ax.set_xlim(x[0],x[-1]) 1754 ylo, yhi = ax.set_ylim(y[0],y[-1]) 1755 if xmin: 1756 xlo = xmin 1723 1757 else: 1724 gridplot(axis_data[0], axis_data[1], np.clip(data,-1e8,1e8).T, **opts) 1725 pylab.xlabel(label(axes[0])) 1726 pylab.ylabel(label(axes[1])) 1727 pylab.title(title) 1758 xlo = x[0] 1759 if xmax: 1760 xhi = xmax 1761 else: 1762 xhi = x[-1] 1763 if ymin: 1764 yhi = ymin 1765 else: 1766 yhi = y[0] 1767 if ymax: 1768 yhi = ymax 1769 else: 1770 yhi = y[-1] 1771 ax.set_xlim(xlo, xhi) 1772 ax.set_ylim(ylo, yhi) 1773 plt.xlabel(label(axes[0])) 1774 plt.ylabel(label(axes[1])) 1775 plt.title(title) 1776 plt.colorbar(im) 1777 plt.gcf().canvas.draw_idle() 1778 1779 return plt.gcf() 1728 1780 1729 1781 @staticmethod 1730 1782 def show(): 1731 import pylab1732 p ylab.show()1783 import matplotlib.pyplot as plt 1784 plt.show() 1733 1785 1734 1786 … … 1850 1902 1851 1903 >>> entry.sample.tree = 100.0 1852 >>> entry.sample.tree1904 >>> print entry.sample.tree 1853 1905 sample:NXsample 1854 1906 tree = 100.0 … … 1870 1922 >>> entry.sample.temperature = 40.0 1871 1923 >>> entry.sample.attrs['tree'] = 10.0 1872 >>> entry.sample.tree1924 >>> print entry.sample.tree 1873 1925 sample:NXsample 1874 1926 @tree = 10.0 … … 1897 1949 1898 1950 tree: 1899 Printthe group tree.1951 Return the group tree. 1900 1952 1901 1953 It invokes the 'dir' method with both 'attrs' and 'recursive' 1902 set to True. Note that this method is defined as a property attribute and 1903 does not require parentheses. 1954 set to True. 1904 1955 1905 1956 save(self, filename, format='w5') … … 1916 1967 >>> entry.sample = NXgroup(temperature=NXfield(40.0,units='K'), 1917 1968 nxclass='NXsample') 1918 >>> entry.sample.tree1969 >>> print entry.sample.tree 1919 1970 sample:NXsample 1920 1971 temperature = 40.0 … … 2163 2214 raise NeXusError, "Link target must be an NXobject" 2164 2215 2165 2166 2216 def sum(self, axis=None): 2167 2217 """ … … 2186 2236 signal.long_name = "Integral from %s to %s %s" % \ 2187 2237 (summedaxis[0], summedaxis[-1], units) 2188 average = NXfield(0.5*(summedaxis.nxdata[0]+summedaxis.nxdata[-1]), name=summedaxis.nxname) 2238 average = NXfield(0.5*(summedaxis.nxdata[0]+summedaxis.nxdata[-1]), 2239 name=summedaxis.nxname) 2189 2240 if units: average.units = units 2190 2241 result = NXdata(signal, axes, average) … … 2296 2347 entries = property(_getentries,doc="NeXus objects within group") 2297 2348 2298 2299 def plot(self, **opts):2349 def plot(self, fmt='bo', xmin=None, xmax=None, ymin=None, ymax=None, 2350 zmin=None, zmax=None, **opts): 2300 2351 """ 2301 2352 Plot data contained within the group. 2353 2354 The format argument is used to set the color and type of the 2355 markers or lines for one-dimensional plots, using the standard 2356 matplotlib syntax. The default is set to blue circles. All 2357 keyword arguments accepted by matplotlib.pyplot.plot can be 2358 used to customize the plot. 2359 2360 In addition to the matplotlib keyword arguments, the following 2361 are defined: 2362 2363 log = True - plot the intensity on a log scale 2364 logy = True - plot the y-axis on a log scale 2365 logx = True - plot the x-axis on a log scale 2366 over = True - plot on the current figure 2302 2367 2303 2368 Raises NeXusError if the data could not be plotted. … … 2328 2393 2329 2394 # Plot with the available plotter 2330 group._plotter.plot(signal, axes, title, errors, **opts) 2331 2395 group._plotter.plot(signal, axes, title, errors, fmt, 2396 xmin, xmax, ymin, ymax, zmin, zmax, **opts) 2397 2398 def oplot(self, fmt='bo', **opts): 2399 """ 2400 Plot the data contained within the group over the current figure. 2401 """ 2402 self.plot(fmt=fmt, over=True, **opts) 2403 2404 def logplot(self, fmt='bo', xmin=None, xmax=None, ymin=None, ymax=None, 2405 zmin=None, zmax=None, **opts): 2406 """ 2407 Plot the data intensity contained within the group on a log scale. 2408 """ 2409 self.plot(fmt=fmt, log=True, 2410 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, 2411 zmin=zmin, zmax=zmax, **opts) 2332 2412 2333 2413 class NXlink(NXobject): … … 2852 2932 return field.nxname 2853 2933 2854 def imshow_irregular(x,y,z):2855 import pylab2856 # from matplotlib.ticker import LogFormatter2857 ax = pylab.gca()2858 im = pylab.mpl.image.NonUniformImage(ax, extent=(x[0],x[-1],y[0],y[-1]), origin=None)2859 im.set_data(x,y,z)2860 ax.images.append(im)2861 ax.set_xlim(x[0],x[-1])2862 ax.set_ylim(y[0],y[-1])2863 pylab.colorbar(im)#, format=LogFormatter())2864 pylab.gcf().canvas.draw_idle()2865 2866 2934 # File level operations 2867 2935 def load(filename, mode='r'):
Note: See TracChangeset
for help on using the changeset viewer.
