Coverage for python/lsst/sims/maf/plots/onedPlotters.py : 11%

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
1from builtins import zip
2import numpy as np
3import matplotlib.pyplot as plt
4from lsst.sims.maf.utils import percentileClipping
6from .plotHandler import BasePlotter
8__all__ = ['OneDBinnedData']
10class OneDBinnedData(BasePlotter):
11 def __init__(self):
12 self.plotType = 'BinnedData'
13 self.objectPlotter = False
14 self.defaultPlotDict = {'title': None, 'label': None, 'xlabel': None, 'ylabel': None,
15 'filled': False, 'alpha': 0.5, 'linestyle': '-', 'linewidth': 1,
16 'logScale': False, 'percentileClip': None,
17 'xMin': None, 'xMax': None, 'yMin': None, 'yMax': None,
18 'fontsize': None, 'figsize': None, 'grid': False}
20 def __call__(self, metricValues, slicer, userPlotDict, fignum=None):
21 """
22 Plot a set of oneD binned metric data.
23 """
24 if slicer.slicerName != 'OneDSlicer':
25 raise ValueError('OneDBinnedData plotter is for use with OneDSlicer')
26 if 'bins' not in slicer.slicePoints:
27 errMessage = 'OneDSlicer must contain "bins" in slicePoints metadata.'
28 errMessage += ' SlicePoints only contains keys %s.' % (slicer.slicePoints.keys())
29 raise ValueError(errMessage)
30 plotDict = {}
31 plotDict.update(self.defaultPlotDict)
32 plotDict.update(userPlotDict)
33 fig = plt.figure(fignum, figsize=plotDict['figsize'])
34 # Plot the histogrammed data.
35 leftedge = slicer.slicePoints['bins'][:-1]
36 width = np.diff(slicer.slicePoints['bins'])
37 if plotDict['filled']:
38 plt.bar(leftedge, metricValues.filled(), width, label=plotDict['label'],
39 linewidth=0, alpha=plotDict['alpha'], log=plotDict['logScale'],
40 color=plotDict['color'])
41 else:
42 good = np.where(metricValues.mask == False)
43 x = np.ravel(list(zip(leftedge[good], leftedge[good] + width[good])))
44 y = np.ravel(list(zip(metricValues[good], metricValues[good])))
45 if plotDict['logScale']:
46 plt.semilogy(x, y, label=plotDict['label'], color=plotDict['color'],
47 linestyle=plotDict['linestyle'], linewidth=plotDict['linewidth'],
48 alpha=plotDict['alpha'])
49 else:
50 plt.plot(x, y, label=plotDict['label'], color=plotDict['color'],
51 linestyle=plotDict['linestyle'], linewidth=plotDict['linewidth'],
52 alpha=plotDict['alpha'])
53 if 'ylabel' in plotDict:
54 plt.ylabel(plotDict['ylabel'], fontsize=plotDict['fontsize'])
55 if 'xlabel' in plotDict:
56 plt.xlabel(plotDict['xlabel'], fontsize=plotDict['fontsize'])
57 # Set y limits (either from values in args, percentileClipping or compressed data values).
58 if plotDict['percentileClip'] is not None:
59 yMin, yMax = percentileClipping(metricValues.compressed(),
60 percentile=plotDict['percentileClip'])
61 if plotDict['yMin'] is None:
62 plotDict['yMin'] = yMin
63 if plotDict['yMax'] is None:
64 plotDict['yMax'] = yMax
66 plt.grid(plotDict['grid'], alpha=0.3)
68 if plotDict['yMin'] is None and metricValues.filled().min() == 0:
69 plotDict['yMin'] = 0
71 # Set y and x limits, if provided.
72 if plotDict['yMin'] is not None:
73 plt.ylim(bottom=plotDict['yMin'])
74 if plotDict['yMax'] is not None:
75 plt.ylim(top=plotDict['yMax'])
76 if plotDict['xMin'] is not None:
77 plt.xlim(left=plotDict['xMin'])
78 if plotDict['xMax'] is not None:
79 plt.xlim(right=plotDict['xMax'])
80 plt.title(plotDict['title'])
81 return fig.number