Everybody loves plots!

Visualizing of training result

There are different plotting backends supported:

  • matplotlib (default, de-facto standard plotting library),
  • plotly (proprietary package with interactive plots, information is kept on the server),
  • ROOT (the library used by CERN employer),
  • bokeh (open-source package with interactive plots)

You create plotting object and can show it using the following functions:

  • matplotlib plot()
  • plotly plot_plotly()
  • ROOT plot_tmva()
  • bokeh plot_bokeh()

mpld3 library allows you plot matplotlib in intarecative regime.


In [1]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [2]:
import mpld3

FunctionsPlot


In [3]:
from rep.plotting import FunctionsPlot

In [4]:
n_points = 30
first_func = (numpy.linspace(0, 10, n_points), numpy.random.random(n_points))
second_func = (numpy.linspace(-10, 0, n_points), numpy.random.random(n_points))

obj = FunctionsPlot({'first': first_func, 'second': second_func})

matplotlib


In [5]:
obj.plot(new_plot=True)


matplotlib using mpld3 interactive plots


In [6]:
obj.plot(new_plot=True, xlim=(-5, 5), ylim=(0.2, 0.8), title='example', xlabel='x', ylabel='y', fontsize=15)
mpld3.display()


Out[6]:

plotly interactive plots


In [7]:
obj.plot_plotly("functions")


Out[7]:

bokeh interactive plots


In [8]:
obj.plot_bokeh(figsize=(15, 10), xlabel='x', title='Example', ylim=(0, 0.5))


BokehJS successfully loaded.

tmva style


In [9]:
obj.plot_tmva(new_plot=True, figsize=(6, 6), title='TMVA example')


Out[9]:

separate two lines


In [10]:
obj1 = FunctionsPlot({'first': first_func})
obj2 = FunctionsPlot({'second': second_func})

In [11]:
# put new_plot to separate figures for lines in matplotlib
obj1.plot(new_plot=True)
obj2.plot(new_plot=True, xlabel='x')



In [12]:
obj1.plot(new_plot=True)
obj2.plot(new_plot=True, xlabel='x', show_legend=False)



In [13]:
obj1.plot(new_plot=True)
obj2.plot(new_plot=True, xlabel='x', show_legend=False)
mpld3.display()


Out[13]:

In [14]:
obj1.plot_plotly('one', title='plotly', xlabel='x', ylabel='points')
obj2.plot_plotly('two', title='plotly', xlabel='x', ylabel='points', xlim=(-8, -2), ylim=(0.2, 0.8), fontsize=13,
                 show_legend=False)


Out[14]:

In [15]:
obj1.plot_bokeh()
obj2.plot_bokeh(title='bokeh', xlabel='x', ylabel='points', xlim=(-8, -2), ylim=(0.2, 0.8), fontsize=13,
                show_legend=False)


BokehJS successfully loaded.
BokehJS successfully loaded.

GridPlot


In [16]:
from rep.plotting import GridPlot, HStackPlot, VStackPlot
grid = GridPlot(2, obj1, obj2, obj1)

In [17]:
# parameters doesn't work here, set them for each plot in grid
grid.plot(title='grid', xlabel='x', ylim=(0.2, 0.8))



In [18]:
obj1.xlabel = 'x'
obj2.xlabel = 'y'
obj1.figsize = (8, 6)
obj2.figsize = (4, 3)

grid = GridPlot(2, obj1, obj2, obj1)
# Only inner parameters of plot is used in grid, so grid ignores all parameters in plot(...)
grid.plot(xlabel='try', ylim=(0.2, 0.8))



In [19]:
# Only show_legend and fontsize can be set using parameters
grid.plot(show_legend=False, fontsize=30)
mpld3.display()


Out[19]:

In [20]:
grid.plot_plotly("grid")


This is the format of your plot grid:
[ (1,1) x1,y1 ]  [ (1,2) x2,y2 ]
[ (2,1) x3,y3 ]  [ (2,2) x4,y4 ]

Out[20]:

In [21]:
grid.plot_bokeh()


BokehJS successfully loaded.

HStackPLot, VStackPlot


In [22]:
hstack = HStackPlot(obj1, obj2, obj1)

In [23]:
hstack.plot(title='stack', xlabel='x', ylim=(0.2, 0.8))



In [24]:
hstack.plot(show_legend=False, fontsize=20)
mpld3.display()


Out[24]:

In [25]:
hstack.plot_plotly("hstack")


This is the format of your plot grid:
[ (1,1) x1,y1 ]  [ (1,2) x2,y2 ]  [ (1,3) x3,y3 ]

Out[25]:

In [26]:
hstack.plot_bokeh()


BokehJS successfully loaded.

In [27]:
vstack = VStackPlot(obj1, obj2, obj1)

In [28]:
vstack.plot(new_plot=True, figsize=(6, 12), title='stack', xlabel='x', ylim=(0.2, 0.8))



In [29]:
vstack.plot(new_plot=True, figsize=(6, 12), show_legend=False, fontsize=10)
mpld3.display()


Out[29]:

In [30]:
vstack.plot_plotly("vstack", figsize=(8, 13))


This is the format of your plot grid:
[ (1,1) x1,y1 ]
[ (2,1) x2,y2 ]
[ (3,1) x3,y3 ]

Out[30]:

In [31]:
vstack.plot_bokeh(figsize=(8, 13))


BokehJS successfully loaded.

ColorMap


In [32]:
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)])

In [33]:
cm.plot(show_legend=False)


/Users/antares/.virtualenvs/rep_open/lib/python2.7/site-packages/matplotlib/collections.py:590: FutureWarning:

elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison


In [34]:
cm.plot()
mpld3.display()


/Users/antares/.virtualenvs/rep_open/lib/python2.7/site-packages/matplotlib/axes/_axes.py:475: UserWarning:

No labelled objects found. Use label='...' kwarg on individual plots.

Out[34]:

In [35]:
cm.plot_plotly('colormap', figsize=(6, 6))


Out[35]:

In [36]:
cm.plot_bokeh()


BokehJS successfully loaded.

BarPlot


In [37]:
from rep.plotting import BarPlot
data = {'normal': (random.normal(0, 0.5, 100), ones(100), 'filled'),
        'gamma': (random.gamma(1.0, 2.0, 100), ones(100), '')}
bar = BarPlot(data)

In [38]:
bar.plot()



In [39]:
bar.plot()
mpld3.display()


Out[39]:

In [40]:
bar.plot_plotly('bar')


Out[40]:

In [41]:
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')

In [42]:
bar_c.plot()



In [43]:
bar_c.plot()
mpld3.display()


Out[43]:

In [44]:
bar_c.plot_plotly('bar_c')


Out[44]:

In [45]:
bar_c.plot_bokeh()


BokehJS successfully loaded.

ErrorPlot


In [46]:
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)

In [47]:
err.plot()



In [48]:
err.plot()
mpld3.display()


/Users/antares/.virtualenvs/rep_open/lib/python2.7/site-packages/mpld3/mplexporter/exporter.py:171: UserWarning:

Legend element <matplotlib.collections.LineCollection object at 0x10deb5dd0> not impemented

/Users/antares/.virtualenvs/rep_open/lib/python2.7/site-packages/mpld3/mplexporter/exporter.py:171: UserWarning:

Legend element <matplotlib.collections.LineCollection object at 0x10dec7450> not impemented

Out[48]:

In [49]:
err.plot_plotly('err')


Out[49]:

ScatterPlot


In [50]:
from rep.plotting import ScatterPlot
x_val = random.uniform(0.0, 1000.0, 100)
y_val = random.uniform(0.0, 1000.0, 100)
sp = ScatterPlot({'simple': (x_val, y_val),
                  'second': (y_val, x_val)},
                 alpha=1, size=30)

In [51]:
sp.plot()



In [52]:
sp.plot()
mpld3.display()


/Users/antares/.virtualenvs/rep_open/lib/python2.7/site-packages/mpld3/mplexporter/exporter.py:171: UserWarning:

Legend element <matplotlib.collections.PathCollection object at 0x10fafb0d0> not impemented

/Users/antares/.virtualenvs/rep_open/lib/python2.7/site-packages/mpld3/mplexporter/exporter.py:171: UserWarning:

Legend element <matplotlib.collections.PathCollection object at 0x10fafb350> not impemented

Out[52]:

In [53]:
sp.plot_plotly('scatter')


Out[53]:

In [54]:
sp.plot_bokeh()


BokehJS successfully loaded.

Function2D_Plot


In [55]:
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)

In [56]:
func_plot.plot()



In [57]:
func_plot.plot()
mpld3.display()


Out[57]:

In [58]:
func_plot.plot_plotly("function2d")


Out[58]:

Histogram2d Plot


In [59]:
from rep.plotting import Histogram2D_Plot
hist2d_plot = Histogram2D_Plot([numpy.random.random(100), numpy.random.random(100)], bins=30, 
                               range=[[0.1, 0.6], [0.2, 0.7]])

In [60]:
hist2d_plot


Out[60]:

In [61]:
hist2d_plot.plot_plotly("hist2d")


Out[61]:

Matplotlib interactive

Now it is posiible to use magic function for interactive matplotlib

  • use new_plot=True for corectness
  • interactive regime will be absent after refreshing

In [62]:
%matplotlib notebook


/Users/antares/.virtualenvs/rep_open/lib/python2.7/site-packages/IPython/kernel/__init__.py:13: ShimWarning:

The `IPython.kernel` package has been deprecated. You should import from ipykernel or jupyter_client instead.

/Users/antares/.virtualenvs/rep_open/lib/python2.7/site-packages/ipykernel/inprocess/socket.py:34: DeprecationWarning:

SocketABC is deprecated.


In [63]:
hist2d_plot.plot(new_plot=True)