REP has simple internal represntation of plots prepared in report.
In this notebook some details are given. This may help you to use REP efficiently, but it is not needed to use REP at all.
There are different plotting backends supported:
Typically this works like:
plot_object = report.roc()
plot_object.plot()
So, report functions return an object, which contains all the information to be plotted. It's internal structire is always as simple as possible and can be inspected.
To draw the plot with different backends use the following functions:
Also mpld3 library allows you to turn matplotlib plots into interactive (see examples below).
# magic function to show matplotlib plots in the notebook
%matplotlib inline
import numpy
import matplotlib
import mpld3
Contains plots of lines (with labels, axes, etc.)
from rep.plotting import FunctionsPlot
n_points = 200
x_array = numpy.linspace(-5, 5, n_points)
first_func = (x_array, numpy.sin(6 * numpy.tanh(x_array)))
second_func = (x_array, numpy.sinc(x_array))
third_func = (numpy.sin(x_array) * numpy.linspace(0, 1, n_points), numpy.cos(x_array) * numpy.linspace(0, 1, n_points))
function_plot = FunctionsPlot({'first': first_func, 'second': second_func})
function_plot.plot(new_plot=True)
function_plot.plot(new_plot=True, xlim=(-5, 5), ylim=(0., 1.), title='example', xlabel='x', ylabel='y', fontsize=15)
mpld3.display()
function_plot.plot_tmva(new_plot=True, figsize=(6, 6), title='TMVA example',
xlabel='x variable', ylabel='y variable', )
function_plot1 = FunctionsPlot({'first': first_func})
function_plot2 = FunctionsPlot({'second': second_func})
function_plot3 = FunctionsPlot({'third': third_func})
# put new_plot to separate figures for lines in matplotlib
function_plot1.plot(new_plot=True)
function_plot2.plot(new_plot=True, xlabel='x')
function_plot1.plot(new_plot=True)
function_plot2.plot(new_plot=True, xlabel='x', show_legend=False)
function_plot1.plot_bokeh()
function_plot2.plot_bokeh(title='bokeh', xlabel='x', ylabel='points', xlim=(-5, 0), fontsize=13,
show_legend=False)
Organizes lots of plots in grid. Used when need to show many plots of same nature, this saves space :)
from rep.plotting import GridPlot, HStackPlot, VStackPlot
grid = GridPlot(2, function_plot1, function_plot2, function_plot3)
# parameters doesn't work here, set them for each plot in grid
grid.plot(title='grid', xlabel='x', ylim=(0.2, 0.8))
function_plot1.xlabel = 'x'
function_plot2.xlabel = 'y'
# these parameters will be ignored
function_plot1.figsize = (8, 6)
function_plot2.figsize = (4, 3)
grid = GridPlot(2, function_plot1, function_plot2, function_plot3)
# Only inner parameters of plot is used in grid, so grid ignores all parameters in plot(...)
# fot the exception of `show_legend` and `fontsize`.
grid.plot(xlabel='try', ylim=(0.2, 0.8))
# can be done with mpld3
# grid.plot(show_legend=False, fontsize=10, figsize=[12, 10])
# mpld3.display()
grid.plot_bokeh()
Incorporates other plots and aligns them horizontally / vertically
hstack = HStackPlot(function_plot1, function_plot2, function_plot3)
hstack.plot(title='stack', xlabel='x', ylim=(0.2, 0.8))
# hstack.plot(show_legend=False, fontsize=20)
# mpld3.display()
hstack.plot_bokeh()
vstack = VStackPlot(function_plot1, function_plot2, function_plot3)
vstack.plot(new_plot=True, figsize=(6, 12), title='stack', xlabel='x', ylim=(0.2, 0.8))
vstack.plot(new_plot=True, figsize=(6, 12), show_legend=False, fontsize=10)
mpld3.display()
# vstack.plot_bokeh(figsize=(8, 13))
from rep.plotting import ColorMap
matrix = numpy.ndarray(shape=(3, 3), buffer=numpy.random.random(size=9))
cm = ColorMap(matrix, labels=['feature {}'.format(index) for index in range(3)])
cm.plot(show_legend=False)
cm.plot(show_legend=False)
mpld3.display()
from rep.plotting import BarPlot
data = {'normal': (numpy.random.normal(0, 0.5, 100), numpy.ones(100), 'filled'),
'gamma': (numpy.random.gamma(1.0, 2.0, 100), numpy.ones(100), '')}
bar = BarPlot(data)
bar.plot()
bar.plot()
mpld3.display()
from rep.plotting import BarComparePlot
data = {'normal': {'one': 23, 'two': 34, 'three': 45},
'gamma': {'one': 11, 'two': 23, 'three': 33}}
bar_c = BarComparePlot(data, sortby='normal')
bar_c.plot()
bar_c.plot()
mpld3.display()
# bar_c.plot_bokeh()
from rep.plotting import ErrorPlot
err = ErrorPlot({'uniform': (numpy.random.random(size=3), numpy.random.random(size=3),
numpy.random.random(size=3), numpy.random.random(size=3))}, size=5)
err.plot()
err.plot()
mpld3.display()
/Users/axelr/.conda/envs/rep/lib/python2.7/site-packages/mpld3/mplexporter/exporter.py:171: UserWarning: Legend element <matplotlib.collections.LineCollection object at 0x113066790> not impemented warnings.warn("Legend element %s not impemented" % child) /Users/axelr/.conda/envs/rep/lib/python2.7/site-packages/mpld3/mplexporter/exporter.py:171: UserWarning: Legend element <matplotlib.collections.LineCollection object at 0x114580490> not impemented warnings.warn("Legend element %s not impemented" % child)
from rep.plotting import ScatterPlot
x_val = numpy.random.uniform(0.0, 1000.0, 100)
y_val = numpy.random.uniform(0.0, 1000.0, 100)
sp = ScatterPlot({'first': (x_val, y_val),
'second': (y_val, x_val)},
alpha=1, size=30)
sp.plot()
sp.plot()
mpld3.display()
/Users/axelr/.conda/envs/rep/lib/python2.7/site-packages/mpld3/mplexporter/exporter.py:171: UserWarning: Legend element <matplotlib.collections.PathCollection object at 0x1130a6d50> not impemented warnings.warn("Legend element %s not impemented" % child) /Users/axelr/.conda/envs/rep/lib/python2.7/site-packages/mpld3/mplexporter/exporter.py:171: UserWarning: Legend element <matplotlib.collections.PathCollection object at 0x1130a6e90> not impemented warnings.warn("Legend element %s not impemented" % child)
from rep.plotting import Function2D_Plot
def func(x, y):
return numpy.sin(x + y*y)
func_plot = Function2D_Plot(func, xlim=(0, 5), ylim=(0, 5), xsteps=100, ysteps=100, vmin=0.5, vmax=1)
func_plot.plot()
/Users/axelr/.conda/envs/rep/lib/python2.7/site-packages/matplotlib/axes/_axes.py:519: UserWarning: No labelled objects found. Use label='...' kwarg on individual plots. warnings.warn("No labelled objects found. "
from rep.plotting import Histogram2D_Plot
hist2d_plot = Histogram2D_Plot([numpy.random.normal(size=1000), numpy.random.normal(size=1000)], bins=15,
range=[[-1.5, 1.5], [-1, 2]])
hist2d_plot
hist2d_plot.plot()
mpld3.display()
One more plotting option is to use magic function for interactive matplotlib
new_plot=True
for correctness# %matplotlib notebook
# hist2d_plot.plot(new_plot=True)