Belle II Software light-2405-quaxo
Plotter Class Reference
Inheritance diagram for Plotter:

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.
 

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, Box, VerboseDistribution, Difference, and Distribution.

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=(32, 18))
87 self.figure.set_tight_layout(False)
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

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 179 of file plotting.py.

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

◆ 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 247 of file plotting.py.

247 def add(self, *args, **kwargs):
248 """
249 Add a new plot to this plotter
250 """
251 return NotImplemented
252

◆ 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 124 of file plotting.py.

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

◆ 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 253 of file plotting.py.

253 def finish(self, *args, **kwargs):
254 """
255 Finish plotting and set labels, legends and stuff
256 """
257 return NotImplemented
258

◆ save()

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

Definition at line 136 of file plotting.py.

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

◆ scale_limits()

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

Definition at line 259 of file plotting.py.

259 def scale_limits(self):
260 """
261 Scale limits to increase distance to boundaries
262 """
263 self.ymin *= 1.0 - math.copysign(self.yscale, self.ymin)
264 self.ymax *= 1.0 + math.copysign(self.yscale, self.ymax)
265 self.xmin *= 1.0 - math.copysign(self.xscale, self.xmin)
266 self.xmax *= 1.0 + math.copysign(self.xscale, self.xmax)
267 return self
268
269

◆ 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 163 of file plotting.py.

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

◆ 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 155 of file plotting.py.

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

◆ 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 171 of file plotting.py.

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

◆ 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 147 of file plotting.py.

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

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.

◆ 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: