Belle II Software development
Plotter Class Reference
Inheritance diagram for Plotter:
Box Correlation CorrelationMatrix Diagonal Difference Distribution Importance Multiplot Overtraining PurityAndEfficiencyOverCut PurityOverEfficiency RejectionOverEfficiency SignalToNoiseOverCut TSNE VerboseDistribution

Public Member Functions

def __init__ (self, figure=None, axis=None)
 
def add_subplot (self, gridspecs)
 
def save (self, filename)
 
def set_plot_options (self, plot_kwargs={ 'linestyle':''})
 
def set_errorbar_options (self, errorbar_kwargs={ 'fmt':'.', 'elinewidth':3, 'alpha':1})
 Overrides default errorbar options for datapoint errorbars.
 
def set_errorband_options (self, errorband_kwargs={ 'alpha':0.5})
 
def set_fill_options (self, fill_kwargs=None)
 
def add (self, *args, **kwargs)
 
def finish (self, *args, **kwargs)
 
def scale_limits (self)
 

Public Attributes

 figure
 create figure
 
 axis
 divide figure into subplots
 
 plots
 create empty list for plots
 
 labels
 create empty list for labels
 
 xmax
 set x limits
 
 ymax
 set y limits
 
 yscale
 y limit scale
 
 xscale
 x limit scale
 
 plot_kwargs
 Default keyword arguments for plot function.
 
 errorbar_kwargs
 Default keyword arguments for errorbar function.
 
 errorband_kwargs
 Default keyword arguments for errorband function.
 
 fill_kwargs
 Default keyword arguments for fill_between function.
 
 prop_cycler
 Property cycler used to give plots unique colors.
 

Static Public Attributes

None plots = None
 Plots added to the axis so far.
 
None labels = None
 Labels of the plots added so far.
 
None xmin = None
 Minimum x value.
 
None xmax = None
 Maximum x value.
 
None ymin = None
 Minimum y value.
 
None ymax = None
 Maximum y value.
 
float yscale = 0.0
 limit scale
 
float xscale = 0.0
 limit scale
 
None figure = None
 figure which is used to draw
 
None axis = None
 Main axis which is used to draw.
 

Protected Member Functions

def _plot_datapoints (self, axis, x, y, xerr=None, yerr=None)
 

Detailed Description

Base class for all Plotters.

Definition at line 43 of file plotting.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  figure = None,
  axis = None 
)
Creates a new figure and axis if None is given, sets the default plot parameters
@param figure default draw figure which is used
@param axis default draw axis which is used

Reimplemented in Multiplot, Overtraining, Correlation, CorrelationMatrix, VerboseDistribution, Difference, Distribution, and Box.

Definition at line 77 of file plotting.py.

77 def __init__(self, figure=None, axis=None):
78 """
79 Creates a new figure and axis if None is given, sets the default plot parameters
80 @param figure default draw figure which is used
81 @param axis default draw axis which is used
82 """
83 b2.B2INFO("Create new figure for class " + str(type(self)))
84 if figure is None:
85
86 self.figure = matplotlib.figure.Figure(figsize=(12, 8), dpi=120)
87 self.figure.set_tight_layout(True)
88 else:
89 self.figure = figure
90
91 if axis is None:
92
93 self.axis = self.figure.add_subplot(1, 1, 1)
94 else:
95 self.axis = axis
96
97
98 self.plots = []
99
100 self.labels = []
101
102 self.xmin, self.xmax = float(0), float(1)
103
104 self.ymin, self.ymax = float(0), float(1)
105
106 self.yscale = 0.1
107
108 self.xscale = 0.0
109
110
111 self.plot_kwargs = None
112
113 self.errorbar_kwargs = None
114
115 self.errorband_kwargs = None
116
117 self.fill_kwargs = None
118
119 self.set_plot_options()
120 self.set_errorbar_options()
121 self.set_errorband_options()
122 self.set_fill_options()
123
124
125 self.prop_cycler = itertools.cycle(plt.rcParams["axes.prop_cycle"])
126

Member Function Documentation

◆ _plot_datapoints()

def _plot_datapoints (   self,
  axis,
  x,
  y,
  xerr = None,
  yerr = None 
)
protected
Plot the given datapoints, with plot, errorbar and make a errorband with fill_between
@param x coordinates of the data points
@param y coordinates of the data points
@param xerr symmetric error on x data points
@param yerr symmetric error on y data points

Definition at line 182 of file plotting.py.

182 def _plot_datapoints(self, axis, x, y, xerr=None, yerr=None):
183 """
184 Plot the given datapoints, with plot, errorbar and make a errorband with fill_between
185 @param x coordinates of the data points
186 @param y coordinates of the data points
187 @param xerr symmetric error on x data points
188 @param yerr symmetric error on y data points
189 """
190 p = e = f = None
191 plot_kwargs = copy.copy(self.plot_kwargs)
192 errorbar_kwargs = copy.copy(self.errorbar_kwargs)
193 errorband_kwargs = copy.copy(self.errorband_kwargs)
194 fill_kwargs = copy.copy(self.fill_kwargs)
195
196 if plot_kwargs is None or 'color' not in plot_kwargs:
197 color = next(self.prop_cycler)
198 color = color['color']
199 plot_kwargs['color'] = color
200 else:
201 color = plot_kwargs['color']
202 color = matplotlib.colors.ColorConverter().to_rgb(color)
203 patch = matplotlib.patches.Patch(color=color, alpha=0.5)
204 patch.get_color = patch.get_facecolor
205 patches = [patch]
206
207 if plot_kwargs is not None:
208 p, = axis.plot(x, y, rasterized=True, **plot_kwargs)
209 patches.append(p)
210
211 if errorbar_kwargs is not None and (xerr is not None or yerr is not None):
212 if 'color' not in errorbar_kwargs:
213 errorbar_kwargs['color'] = color
214 if 'ecolor' not in errorbar_kwargs:
215 errorbar_kwargs['ecolor'] = [0.5 * x for x in color]
216
217 # fully mask nan values.
218 # Needed until https://github.com/matplotlib/matplotlib/pull/23333 makes it into the externals.
219 # TODO: remove in release 8.
220 if not isinstance(xerr, (numpy.ndarray, list)):
221 xerr = xerr*numpy.ones(len(x))
222 mask = numpy.logical_and.reduce([numpy.isfinite(v) for v in [x, y, xerr, yerr]])
223
224 e = axis.errorbar(
225 x[mask], y[mask], xerr=numpy.where(
226 xerr[mask] < 0, 0.0, xerr[mask]), yerr=numpy.where(
227 yerr[mask] < 0, 0.0, yerr[mask]), rasterized=True, **errorbar_kwargs)
228 patches.append(e)
229
230 if errorband_kwargs is not None and yerr is not None:
231 if 'color' not in errorband_kwargs:
232 errorband_kwargs['color'] = color
233 if xerr is not None:
234 # Ensure that xerr and yerr are iterable numpy arrays
235 xerr = x + xerr - x
236 yerr = y + yerr - y
237 for _x, _y, _xe, _ye in zip(x, y, xerr, yerr):
238 axis.add_patch(matplotlib.patches.Rectangle((_x - _xe, _y - _ye), 2 * _xe, 2 * _ye, rasterized=True,
239 **errorband_kwargs))
240 else:
241 f = axis.fill_between(x, y - yerr, y + yerr, interpolate=True, rasterized=True, **errorband_kwargs)
242
243 if fill_kwargs is not None:
244 # to fill the last bin of a histogram
245 x = numpy.append(x, x[-1]+2*xerr[-1])
246 y = numpy.append(y, y[-1])
247 xerr = numpy.append(xerr, xerr[-1])
248
249 axis.fill_between(x-xerr, y, 0, rasterized=True, **fill_kwargs)
250
251 return (tuple(patches), p, e, f)
252

◆ add()

def add (   self,
args,
**  kwargs 
)
Add a new plot to this plotter

Reimplemented in Correlation, Box, Distribution, VerboseDistribution, Difference, Diagonal, PurityOverEfficiency, RejectionOverEfficiency, PurityAndEfficiencyOverCut, SignalToNoiseOverCut, Overtraining, TSNE, CorrelationMatrix, Importance, and Multiplot.

Definition at line 253 of file plotting.py.

253 def add(self, *args, **kwargs):
254 """
255 Add a new plot to this plotter
256 """
257 return NotImplemented
258

◆ add_subplot()

def add_subplot (   self,
  gridspecs 
)
Adds a new subplot to the figure, updates all other axes
according to the given gridspec
@param gridspecs gridspecs for all axes including the new one

Definition at line 127 of file plotting.py.

127 def add_subplot(self, gridspecs):
128 """
129 Adds a new subplot to the figure, updates all other axes
130 according to the given gridspec
131 @param gridspecs gridspecs for all axes including the new one
132 """
133 for gs, ax in zip(gridspecs[:-1], self.figure.axes):
134 ax.set_position(gs.get_position(self.figure))
135 ax.set_subplotspec(gs)
136 axis = self.figure.add_subplot(gridspecs[-1], sharex=self.axis)
137 return axis
138

◆ finish()

def finish (   self,
args,
**  kwargs 
)
Finish plotting and set labels, legends and stuff

Reimplemented in PurityAndEfficiencyOverCut, SignalToNoiseOverCut, PurityOverEfficiency, RejectionOverEfficiency, Multiplot, Diagonal, Distribution, Box, Overtraining, VerboseDistribution, Correlation, TSNE, Importance, CorrelationMatrix, and Difference.

Definition at line 259 of file plotting.py.

259 def finish(self, *args, **kwargs):
260 """
261 Finish plotting and set labels, legends and stuff
262 """
263 return NotImplemented
264

◆ save()

def save (   self,
  filename 
)
Save the figure into a file
@param filename of the file

Definition at line 139 of file plotting.py.

139 def save(self, filename):
140 """
141 Save the figure into a file
142 @param filename of the file
143 """
144 b2.B2INFO("Save figure for class " + str(type(self)))
145 from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
146 canvas = FigureCanvas(self.figure)
147 canvas.print_figure(filename, dpi=50)
148 return self
149

◆ scale_limits()

def scale_limits (   self)
Scale limits to increase distance to boundaries

Definition at line 265 of file plotting.py.

265 def scale_limits(self):
266 """
267 Scale limits to increase distance to boundaries
268 """
269 self.ymin *= 1.0 - math.copysign(self.yscale, self.ymin)
270 self.ymax *= 1.0 + math.copysign(self.yscale, self.ymax)
271 self.xmin *= 1.0 - math.copysign(self.xscale, self.xmin)
272 self.xmax *= 1.0 + math.copysign(self.xscale, self.xmax)
273 return self
274
275

◆ set_errorband_options()

def set_errorband_options (   self,
  errorband_kwargs = {'alpha': 0.5} 
)
Overrides default errorband options for datapoint errorband
@param errorbar_kwargs keyword arguments for the fill_between function

Definition at line 166 of file plotting.py.

166 def set_errorband_options(self, errorband_kwargs={'alpha': 0.5}):
167 """
168 Overrides default errorband options for datapoint errorband
169 @param errorbar_kwargs keyword arguments for the fill_between function
170 """
171 self.errorband_kwargs = copy.copy(errorband_kwargs)
172 return self
173

◆ set_errorbar_options()

def set_errorbar_options (   self,
  errorbar_kwargs = {'fmt': '.', 'elinewidth': 3, 'alpha': 1} 
)

Overrides default errorbar options for datapoint errorbars.

Overrides default errorbar options for datapoint errorbars
@param errorbar_kwargs keyword arguments for the errorbar function

Definition at line 158 of file plotting.py.

158 def set_errorbar_options(self, errorbar_kwargs={'fmt': '.', 'elinewidth': 3, 'alpha': 1}):
159 """
160 Overrides default errorbar options for datapoint errorbars
161 @param errorbar_kwargs keyword arguments for the errorbar function
162 """
163 self.errorbar_kwargs = copy.copy(errorbar_kwargs)
164 return self
165

◆ set_fill_options()

def set_fill_options (   self,
  fill_kwargs = None 
)
Overrides default fill_between options for datapoint errorband
@param fill_kwargs keyword arguments for the fill_between function

Definition at line 174 of file plotting.py.

174 def set_fill_options(self, fill_kwargs=None):
175 """
176 Overrides default fill_between options for datapoint errorband
177 @param fill_kwargs keyword arguments for the fill_between function
178 """
179 self.fill_kwargs = copy.copy(fill_kwargs)
180 return self
181

◆ set_plot_options()

def set_plot_options (   self,
  plot_kwargs = {'linestyle': ''} 
)
Overrides default plot options for datapoint plot
@param plot_kwargs keyword arguments for the plot function

Definition at line 150 of file plotting.py.

150 def set_plot_options(self, plot_kwargs={'linestyle': ''}):
151 """
152 Overrides default plot options for datapoint plot
153 @param plot_kwargs keyword arguments for the plot function
154 """
155 self.plot_kwargs = copy.copy(plot_kwargs)
156 return self
157

Member Data Documentation

◆ axis [1/2]

None axis = None
static

Main axis which is used to draw.

Definition at line 75 of file plotting.py.

◆ axis [2/2]

axis

divide figure into subplots

Definition at line 93 of file plotting.py.

◆ errorband_kwargs

errorband_kwargs

Default keyword arguments for errorband function.

Definition at line 115 of file plotting.py.

◆ errorbar_kwargs

errorbar_kwargs

Default keyword arguments for errorbar function.

Definition at line 113 of file plotting.py.

◆ figure [1/2]

None figure = None
static

figure which is used to draw

Definition at line 73 of file plotting.py.

◆ figure [2/2]

figure

create figure

Definition at line 86 of file plotting.py.

◆ fill_kwargs

fill_kwargs

Default keyword arguments for fill_between function.

Definition at line 117 of file plotting.py.

◆ labels [1/2]

None labels = None
static

Labels of the plots added so far.

Definition at line 61 of file plotting.py.

◆ labels [2/2]

labels

create empty list for labels

Definition at line 100 of file plotting.py.

◆ plot_kwargs

plot_kwargs

Default keyword arguments for plot function.

Definition at line 111 of file plotting.py.

◆ plots [1/2]

None plots = None
static

Plots added to the axis so far.

Definition at line 59 of file plotting.py.

◆ plots [2/2]

plots

create empty list for plots

Definition at line 98 of file plotting.py.

◆ prop_cycler

prop_cycler

Property cycler used to give plots unique colors.

Definition at line 125 of file plotting.py.

◆ xmax [1/2]

None xmax = None
static

Maximum x value.

Definition at line 65 of file plotting.py.

◆ xmax [2/2]

xmax

set x limits

Definition at line 102 of file plotting.py.

◆ xmin

None xmin = None
static

Minimum x value.

Definition at line 63 of file plotting.py.

◆ xscale [1/2]

xscale = 0.0
static

limit scale

Definition at line 71 of file plotting.py.

◆ xscale [2/2]

xscale

x limit scale

Definition at line 108 of file plotting.py.

◆ ymax [1/2]

None ymax = None
static

Maximum y value.

Definition at line 69 of file plotting.py.

◆ ymax [2/2]

ymax

set y limits

Definition at line 104 of file plotting.py.

◆ ymin

None ymin = None
static

Minimum y value.

Definition at line 67 of file plotting.py.

◆ yscale [1/2]

yscale = 0.0
static

limit scale

Definition at line 70 of file plotting.py.

◆ yscale [2/2]

yscale

y limit scale

Definition at line 106 of file plotting.py.


The documentation for this class was generated from the following file: