Coverage for python/lsst/sims/maf/plots/twoDPlotters.py : 14%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import numpy as np
2from matplotlib import colors
3import matplotlib.pyplot as plt
4from .plotHandler import BasePlotter
5from .perceptual_rainbow import makePRCmap
7__all__ = ['TwoDMap', 'VisitPairsHist']
9perceptual_rainbow = makePRCmap()
11class TwoDMap(BasePlotter):
12 def __init__(self):
13 self.plotType = 'TwoD'
14 self.objectPlotter = False
15 self.defaultPlotDict = {'title': None, 'xlabel': None, 'ylabel': None, 'label': None,
16 'logScale': False, 'cbarFormat': None, 'cbarTitle': 'Count',
17 'cmap': perceptual_rainbow,
18 'percentileClip': None, 'colorMin': None, 'colorMax': None,
19 'zp': None, 'normVal': None,
20 'cbar_edge': True, 'nTicks': None, 'aspect': 'auto',
21 'xextent': None, 'origin': None}
23 def __call__(self, metricValue, slicer, userPlotDict, fignum=None):
24 """
25 Parameters
26 ----------
27 metricValue : numpy.ma.MaskedArray
28 slicer : lsst.sims.maf.slicers.BaseSpatialSlicer
29 (any spatial slicer)
30 userPlotDict: dict
31 Dictionary of plot parameters set by user (overrides default values).
32 fignum : int
33 Matplotlib figure number to use (default = None, starts new figure).
35 Returns
36 -------
37 int
38 Matplotlib figure number used to create the plot.
39 """
40 if 'Healpix' in slicer.slicerName:
41 self.defaultPlotDict['ylabel'] = 'Healpix ID'
42 elif 'Opsim' in slicer.slicerName:
43 self.defaultPlotDict['ylabel'] = 'Field ID'
44 self.defaultPlotDict['origin'] = 'lower'
45 elif 'User' in slicer.slicerName:
46 self.defaultPlotDict['ylabel'] = 'User Field ID'
48 plotDict = {}
49 plotDict.update(self.defaultPlotDict)
50 # Don't clobber with None
51 for key in userPlotDict:
52 if userPlotDict[key] is not None:
53 plotDict[key] = userPlotDict[key]
55 if plotDict['xextent'] is None:
56 plotDict['xextent'] = [0, metricValue[0, :].size]
58 if plotDict['logScale']:
59 norm = colors.LogNorm()
60 else:
61 norm = None
63 # Mask out values below the color minimum so they show up as white
64 if plotDict['colorMin'] is not None:
65 lowVals = np.where(metricValue.data < plotDict['colorMin'])
66 metricValue.mask[lowVals] = True
68 figure = plt.figure(fignum)
69 ax = figure.add_subplot(111)
70 yextent = slicer.spatialExtent
71 xextent = plotDict['xextent']
72 extent = []
73 extent.extend(xextent)
74 extent.extend(yextent)
75 image = ax.imshow(metricValue, vmin=plotDict['colorMin'], vmax=plotDict['colorMax'],
76 aspect=plotDict['aspect'], cmap=plotDict['cmap'], norm=norm,
77 extent=extent,
78 interpolation='none', origin=plotDict['origin'])
79 cb = plt.colorbar(image)
81 ax.set_xlabel(plotDict['xlabel'])
82 ax.set_ylabel(plotDict['ylabel'])
83 ax.set_title(plotDict['title'])
84 cb.set_label(plotDict['cbarTitle'])
86 # Fix white space on pdf's
87 if plotDict['cbar_edge']:
88 cb.solids.set_edgecolor("face")
89 return figure.number
91class VisitPairsHist(BasePlotter):
92 """
93 Given an opsim2dSlicer, figure out what fraction of observations are in singles, pairs, triples, etc.
94 """
95 def __init__(self):
96 self.plotType = 'TwoD'
97 self.objectPlotter = False
98 self.defaultPlotDict = {'title': None, 'xlabel': 'N visits per night per field',
99 'ylabel': 'N Visits', 'label': None, 'color': 'b',
100 'xlim': [0, 20], 'ylim': None}
102 def __call__(self, metricValue, slicer, userPlotDict, fignum=None):
103 """
104 Parameters
105 ----------
106 metricValue : numpy.ma.MaskedArray
107 slicer : lsst.sims.maf.slicers.TwoDSlicer
108 userPlotDict: dict
109 Dictionary of plot parameters set by user (overrides default values).
110 fignum : int
111 Matplotlib figure number to use (default = None, starts new figure).
113 Returns
114 -------
115 int
116 Matplotlib figure number used to create the plot.
117 """
118 plotDict = {}
119 plotDict.update(self.defaultPlotDict)
120 # Don't clobber with None
121 for key in userPlotDict:
122 if userPlotDict[key] is not None:
123 plotDict[key] = userPlotDict[key]
125 maxVal = metricValue.max()
126 bins = np.arange(0.5, maxVal + 0.5, 1)
128 vals, bins = np.histogram(metricValue, bins)
129 xvals = (bins[:-1] + bins[1:]) / 2.
131 figure = plt.figure(fignum)
132 ax = figure.add_subplot(111)
133 ax.bar(xvals, vals * xvals, color=plotDict['color'], label=plotDict['label'])
134 ax.set_xlabel(plotDict['xlabel'])
135 ax.set_ylabel(plotDict['ylabel'])
136 ax.set_title(plotDict['title'])
137 ax.set_xlim(plotDict['xlim'])
138 ax.set_ylim(plotDict['ylim'])
140 return figure.number