Belle II Software light-2406-ragdoll
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.
 
 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 44 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 78 of file plotting.py.

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

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

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

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

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

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

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

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

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

◆ save()

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

Definition at line 140 of file plotting.py.

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

◆ scale_limits()

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

Definition at line 266 of file plotting.py.

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

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

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

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

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

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

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

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

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

Member Data Documentation

◆ axis [1/2]

None axis = None
static

Main axis which is used to draw.

Definition at line 76 of file plotting.py.

◆ axis [2/2]

axis

divide figure into subplots

Definition at line 94 of file plotting.py.

◆ errorband_kwargs

errorband_kwargs

Default keyword arguments for errorband function.

Definition at line 116 of file plotting.py.

◆ errorbar_kwargs

errorbar_kwargs

Default keyword arguments for errorbar function.

Definition at line 114 of file plotting.py.

◆ figure [1/2]

None figure = None
static

figure which is used to draw

Definition at line 74 of file plotting.py.

◆ figure [2/2]

figure

create figure

Definition at line 87 of file plotting.py.

◆ fill_kwargs

fill_kwargs

Default keyword arguments for fill_between function.

Definition at line 118 of file plotting.py.

◆ labels [1/2]

None labels = None
static

Labels of the plots added so far.

Definition at line 62 of file plotting.py.

◆ labels [2/2]

labels

create empty list for labels

Definition at line 101 of file plotting.py.

◆ plot_kwargs

plot_kwargs

Default keyword arguments for plot function.

Definition at line 112 of file plotting.py.

◆ plots [1/2]

None plots = None
static

Plots added to the axis so far.

Definition at line 60 of file plotting.py.

◆ plots [2/2]

plots

create empty list for plots

Definition at line 99 of file plotting.py.

◆ prop_cycler

prop_cycler

Property cycler used to give plots unique colors.

Definition at line 126 of file plotting.py.

◆ xmax [1/2]

None xmax = None
static

Maximum x value.

Definition at line 66 of file plotting.py.

◆ xmax [2/2]

xmax

set x limits

Definition at line 103 of file plotting.py.

◆ xmin

None xmin = None
static

Minimum x value.

Definition at line 64 of file plotting.py.

◆ xscale [1/2]

xscale = 0.0
static

limit scale

Definition at line 72 of file plotting.py.

◆ xscale [2/2]

xscale

x limit scale

Definition at line 109 of file plotting.py.

◆ ymax [1/2]

None ymax = None
static

Maximum y value.

Definition at line 70 of file plotting.py.

◆ ymax [2/2]

ymax

set y limits

Definition at line 105 of file plotting.py.

◆ ymin

None ymin = None
static

Minimum y value.

Definition at line 68 of file plotting.py.

◆ yscale [1/2]

yscale = 0.0
static

limit scale

Definition at line 71 of file plotting.py.

◆ yscale [2/2]

yscale

y limit scale

Definition at line 107 of file plotting.py.


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