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()


matplotlib using mpld3 interactive plots


In [6]:
obj.plot(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")


/Users/antares/.virtualenvs/rep/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning:

A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.

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 se separate figures for lines in matplotlib
obj1.plot(new_plot=True)
obj2.plot(new_plot=True, xlabel='x')



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



In [13]:
obj1.plot()
obj2.plot(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)


/Users/antares/.virtualenvs/rep/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning:

A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.

/Users/antares/.virtualenvs/rep/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning:

A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.


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)


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")


/Users/antares/.virtualenvs/rep/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning:

A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.


In [21]:
grid.plot_bokeh()


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")


/Users/antares/.virtualenvs/rep/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning:

A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.


In [26]:
hstack.plot_bokeh()



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))


/Users/antares/.virtualenvs/rep/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning:

A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.


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


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)



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


/Users/antares/.virtualenvs/rep/lib/python2.7/site-packages/matplotlib/axes.py:4747: UserWarning:

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

Out[34]:

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


/Users/antares/.virtualenvs/rep/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning:

A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.


In [36]:
cm.plot_bokeh()


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')


/Users/antares/.virtualenvs/rep/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning:

A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.


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')


/Users/antares/.virtualenvs/rep/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning:

A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.


In [45]:
bar_c.plot_bokeh()


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))})

In [47]:
err.plot()



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


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

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

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

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

Out[48]:

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


/Users/antares/.virtualenvs/rep/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning:

A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.

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/lib/python2.7/site-packages/mpld3/mplexporter/exporter.py:171: UserWarning:

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

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

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

Out[52]:

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


/Users/antares/.virtualenvs/rep/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning:

A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.


In [54]:
sp.plot_bokeh()


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)

In [56]:
func_plot.plot()



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


Out[57]:

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


/Users/antares/.virtualenvs/rep/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning:

A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.