graph.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:plumeria 作者: sk89q 项目源码 文件源码
def pie(message):
    """
    Generate a pie graph. To specify the parts of the pie, make a
    list separated by new lines, semi-colons or commas where each
    list entry has a percentage in it (if there is more than one, the
    first percentage is used).

    To specify a graph title, don't provide a percentage for one of the
    list items.

    Example::

        /pie Materials, 40% Glass, 22%

    The percentages do not need to add up to 100%. If they do not, then
    the percentage values will be normalized so that they do add up to 100%.

    """
    title, labels, data = extract_data(message.content, pattern=PERCENTAGE_PATTERN, normalize=True)

    def execute():
        with lock:
            plt.figure(1, figsize=(5, 5))
            ax = plt.axes([0.1, 0.1, 0.4, 0.4])

            plt.pie(data, labels=labels, autopct='%1.0f%%', startangle=90)

            if title:
                plt.title(title)

            prop = fm.FontProperties(fname=font_path, size=11)
            for text in ax.texts:
                text.set_fontproperties(prop)

            buf = io.BytesIO()
            plt.savefig(buf, bbox_inches='tight', transparent=False, pad_inches=0.1)

            plt.clf()
        return buf

    buf = await asyncio.get_event_loop().run_in_executor(None, execute)

    return Response("", attachments=[MemoryAttachment(buf, "graph.png", "image/png")])
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号