Package: matplotboard
A utility to generate html dashboards using matplotlib. Matplotboard makes it easy to
wrap your plotting functions and dump the plots into a searchable webpage or a markdown report. This is best
demonstrated with an example.importnumpyasnpimportmatplotlib.pyplotaspltimportmatplotboardasmpb@mpb.decl_figdefcool_fig():xs=np.linspace(-10,10,100)ys=xs**2plt.plot(xs,ys)if__name__=='__main__':figures={'cool_fig':cool_fig(),}mpb.render(figures)mpb.generate_report(figures,'Report')You can view the resultshere. Let's walk through this one part at a time.First, we importnumpyandmatplotlibfor some calculations, and plotting,
respectively. As well asmatplotboarditself.importnumpyasnpimportmatplotlib.pyplotaspltimportmatplotboardasmpbmatplotboardrelies uponmatplotlibfor the underlying rendering engine so
other plotting libraries are not supported. However, wrappers aroundmatplotlibsuch asseabornshould work.Next, we declare the function that is actually going to do the plotting.@mpb.decl_figdefcool_fig():xs=np.linspace(-10,10,100)ys=xs**2plt.plot(xs,ys)Thedecl_figdecorator modifies the function to work withmatplotboard. A
plotting function decorated withdecl_figmust fulfill the following
contract:A cleanfigurehas been initiated and the function will do any plotting
on that figure.It is free to subdivide the figure into as many axes as required, but
shouldn't create additionalFigureobjects.The function can optionally return Markdown text that will be rendered
along with the plot.The function shouldn't callsavefig. This is handled bymatplotboardautomatically.Finally, we declare the actual figures that we want to generate, and tellmatplotboardto render the figures and assemble them into an interactive webpage.if__name__=='__main__':figures={'cool_fig':cool_fig(),}mpb.render(figures)mpb.generate_report(figures,'Report')Bothrender, andgenerate_reporttake a dictionary as their first argument.
The dictionary keys are strings that are interpreted as the individual figure
names, and the dictionary values are the plots we want to generate. Note that
the function is called before inserting it into the dictionary. Due to the
modification of the original function by the decorator, this doesn't actually
call the function yet, but bundles the function and any arguments together
into aFigureobject which it then returns for later processing bymatplotboard.By writing plotting functions with arguments, a single function can be reused
to make many different plots. For example, you may have a dataset that is
divided into several categories and you would like to plot some variable for
each category. You could do this by writing one plotting function and calling
it with different arguments to specify each of the categories.Try running the example. If everything works, there should be a new folder in
the current directory calleddashboard, and within it an html file calledreport.html. Open it with your browser to see a dashboard containing a single
plot. Try clicking on it for a zoomed view!A single plot is not very interesting. Wherematplotboardstarts to really become useful is when you have lots of plots to generate. Check out the following example.fromitertoolsimportproductimportnumpyasnpimportmatplotlib.pyplotaspltimportmatplotboardasmpb@mpb.decl_figdefcool_fig(func,scale,color='b'):xs=np.linspace(-scale,scale,100)f={'sin':lambdaxs:np.sin(xs),'tan':lambdaxs:np.tan(xs),'exp':lambdaxs:np.exp(xs),}[func]ys=f(xs)plt.plot(xs,ys,color=color)if__name__=='__main__':mpb.configure(multiprocess=True)figures={}forcolor,function,scaleinproduct('rbgk',['sin','tan','exp'],np.linspace(1,20,20)):figures[f'{function}_{color}_{scale}']=cool_fig(function,scale,color=color)mpb.render(figures)mpb.generate_report(figures,'Report')What's changed? You can view the pagehereFirst of all, the plotting function has been enhanced to take a few arguments
that modify it's behavior. You can now specify whether you would like to plotsin,tan, orexpas well as effectively set the x length scale.Second, we now are programatically making all combinations of plotting color,
function, and scale with theproductfunction and declaring a plot for each
combination. This comes down to4*3*20=240different plots. To speed things
up a bit, this example also switches onmatplotboard's multiprocessing
support. Try running this example and open up the resulting web-page just as
before. Note the pagination feature limiting the number of figures displayed at
once. Also, try selecting a plot and moving through the figures on the page
with the arrow keys. Finally, try out the filter box in the top right. A few
interesting searches may be "sin_", "_r_", or "tan_g_9" to search for allsinplots, all red plots, and just thetan_g_9plot, respectively.For one final example, let's look at the support for writing reports the incorporate generated figures.fromitertoolsimportproductimportnumpyasnpimportmatplotlib.pyplotaspltimportmatplotboardasmpb@mpb.decl_figdefcool_fig(func,scale,color="b"):xs=np.linspace(-scale,scale,100)f={"sin":lambdaxs:np.sin(xs),"tan":lambdaxs:np.tan(xs),"exp":lambdaxs:np.exp(xs),}[func]ys=f(xs)plt.plot(xs,ys,color=color)report="""\Authors: Will HuntingDate: December 2, 1997# Report On Functions## IntroductionAs we all know, there are many functions. An example is the sine function seen below.fig::sin_b_1## Other FunctionsHowever, there are many other functions such as the tangent or exponential.<div class="row"><div class="col-md-6 row_fig">fig::tan_r_1|The rugged tangent function</div><div class="col-md-6 row_fig">fig::exp_g_2|The majestic exponential function</div></div>The decision of which function is best is up to *you*!## Local FiguresI happened to have a couple *really* fantastic figures on my computer that Iwant to include as well. How do I include them? It's easy! Just add them tothe list of figures with the `loc_fig` function and they will be marked to becopied to the output directory. Here are a couple examples:<div class="row"><div class="col-md-6 row_fig">fig::image8</div><div class="col-md-6 row_fig">fig::image10</div></div>"""if__name__=="__main__":mpb.configure(multiprocess=True)figures={}forcolor,function,scaleinproduct("rbgk",["sin","tan","exp"],np.linspace(1,5,5)):figures[f"{function}_{color}_{int(scale)}"]=cool_fig(function,scale,color=color)figures["image8"]=mpb.loc_fig("figures/image8.png")figures["image10"]=mpb.loc_fig("figures/image10.png")mpb.render(figures)mpb.generate_report(figures,"Report",body=report)See result of this examplehere.Thegenerate_reportfunction supports an optionalbodyargument which
signalsmatplotboardto render the markdown into a report, rather than making
a simple plot dump. A special syntax is used for embedding generated figures.fig::figure_name|Optional CaptionBootstrapis included by default so multiple figures side-by-side are
possible by use of arowdiv as shown in the example.In addition to including generated figures via thefig::construct, static
figures (such as diagrams or photographs) can be included via thelocfig::(think local figure) construct, where instead of the figure name, you specify
the path to the file. Finally, pictures out on the internet can be specified
viaextfig::.