Skip to main content

使用 matplotlib 生成简单的 HTML 仪表板

项目描述

Matplotboard:为了您的健康!

构建状态

使用 matplotlib 生成 html 仪表板的实用程序。Matplotboard 可以轻松包装绘图功能并将绘图转储到可搜索的网页或降价报告中。最好用一个例子来说明这一点。

import numpy as np
import matplotlib.pyplot as plt
import matplotboard as mpb

@mpb.decl_fig
def cool_fig():
    xs = np.linspace(-10, 10, 100)
    ys = xs**2
    plt.plot(xs, ys)

if __name__ == '__main__':
    figures = {
        'cool_fig': cool_fig(),
    }

    mpb.render(figures)
    mpb.generate_report(figures, 'Report')

您可以在此处查看结果。让我们一步一步来看看这一部分。

首先,我们分别导入numpymatplotlib进行一些计算和绘图。以及它matplotboard自己。

import numpy as np
import matplotlib.pyplot as plt
import matplotboard as mpb

matplotboard依赖于matplotlib底层渲染引擎,因此不支持其他绘图库。matplotlib但是,诸如此类的包装器 seaborn应该可以工作。

接下来,我们声明实际要进行绘图的函数。

@mpb.decl_fig
def cool_fig():
    xs = np.linspace(-10,10, 100)
    ys = xs**2
    plt.plot(xs, ys)

装饰器decl_fig修改函数以使用matplotboard. 用 装饰的绘图函数decl_fig必须满足以下约定:

  • figure已启动清理,该函数将在该图形上进行任何绘图。
  • 可以根据需要自由地将图形细分为多个轴,但不应创建其他Figure对象。
  • 该函数可以选择返回将与绘图一起呈现的 Markdown 文本。
  • 该函数不应调用savefig. 这是由matplotboard 自动处理的。

最后,我们声明我们想要生成的实际图形,并告诉matplotboard渲染这些图形并将它们组装成一个交互式网页。

if __name__ == '__main__':
    figures = {
        'cool_fig': cool_fig(),
    }

    mpb.render(figures)
    mpb.generate_report(figures, 'Report')

render, 并generate_report把字典作为他们的第一个参数。字典键是被解释为单个图形名称的字符串,字典值是我们要生成的图。请注意,该函数在将其插入字典之前被调用。由于装饰器对原始函数的修改,这实际上并没有调用该函数,而是将函数和任何参数捆绑到一个Figure对象中,然后返回该对象以供以后处理matplotboard

通过编写带参数的绘图函数,可以重复使用单个函数来制作许多不同的绘图。例如,您可能有一个分为多个类别的数据集,并且您希望为每个类别绘制一些变量。您可以通过编写一个绘图函数并使用不同的参数调用它来指定每个类别来做到这一点。

尝试运行示例。如果一切正常,在当前目录中应该有一个名为 的新文件夹,其中dashboard有一个名为 report.html. 用您的浏览器打开它以查看包含单个图的仪表板。尝试单击它以查看放大视图!

单个情节不是很有趣。当matplotboard你有大量的情节要生成时,真正开始变得有用的地方。看看下面的例子。

from itertools import product
import numpy as np
import matplotlib.pyplot as plt
import matplotboard as mpb

@mpb.decl_fig
def cool_fig(func, scale, color='b'):
    xs = np.linspace(-scale, scale, 100)
    f = {
        'sin': lambda xs: np.sin(xs),
        'tan': lambda xs: np.tan(xs),
        'exp': lambda xs: np.exp(xs),
    }[func]
    ys = f(xs)
    plt.plot(xs, ys, color=color)

if __name__ == '__main__':
    mpb.configure(multiprocess=True)
    figures = {}

    for color, function, scale in product('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')

有什么改变?你可以在这里查看页面

首先,绘图功能已得到增强,可以接受一些修改其行为的参数。您现在可以指定是否要绘制 sintanexp以及有效地设置 x 长度比例。

其次,我们现在正在以编程方式使用函数绘制颜色、函数和比例的所有组合,并product为每个组合声明一个绘图。这归结为4*3*20=240不同的情节。为了加快速度,这个例子还开启了matplotboard的多处理支持。尝试运行此示例并像以前一样打开生成的网页。请注意分页功能限制了一次显示的图形数量。此外,尝试选择一个绘图并使用箭头键在页面上的图形中移动。最后,试试右上角的过滤框。一些有趣的搜索可能是“sin_”、“_r_”或“tan_g_9”,分别搜索所有 sin图、所有红色图和仅tan_g_9图。

最后一个例子,让我们看看对编写报告并合并生成的数字的支持。

from itertools import product
import numpy as np
import matplotlib.pyplot as plt
import matplotboard as mpb


@mpb.decl_fig
def cool_fig(func, scale, color="b"):
    xs = np.linspace(-scale, scale, 100)
    f = {
        "sin": lambda xs: np.sin(xs),
        "tan": lambda xs: np.tan(xs),
        "exp": lambda xs: np.exp(xs),
    }[func]
    ys = f(xs)
    plt.plot(xs, ys, color=color)


report = """\
Authors: Will Hunting
Date: December 2, 1997

# Report On Functions

## Introduction

As we all know, there are many functions. An example is the sine function seen below.
fig::sin_b_1

## Other Functions

However, 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 Figures

I happened to have a couple *really* fantastic figures on my computer that I
want to include as well. How do I include them? It's easy! Just add them to
the list of figures with the `loc_fig` function and they will be marked to be
copied 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 = {}

    for color, function, scale in product(
        "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)

在此处查看此示例的结果。

generate_report函数支持一个可选body参数,该参数matplotboard表示将降价渲染到报告中,而不是制作简单的绘图转储。一种特殊的语法用于嵌入生成的图形。

fig::figure_name|Optional Caption

Bootstrap默认情况下包含多个图形,因此可以通过使用 div 并排显示多个图形,row如示例中所示。

除了通过fig::构造包含生成的图形外,静态图形(例如图表或照片)还可以通过locfig:: (想想本地图形)构造包含,其中您指定文件的路径而不是图形名称。最后,可以通过 指定互联网上的图片extfig::

下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

matplotboard-1.2.0.tar.gz (186.3 kB 查看哈希

已上传