Py之mpld3:mpld3的简介、安装、使用方法之详细攻略

本文介绍了一个结合matplotlib与D3.js的工具mpld3,它能够将matplotlib生成的图表转化为可在浏览器中显示的交互式HTML图表。文章详细讲解了mpld3的安装步骤及使用方法,并提供了一个具体的代码示例来展示如何创建交互式图例。
该文章已生成可运行项目,

Py之mpld3:mpld3的简介、安装、使用方法之详细攻略

 

 

目录

mpld3的简介

mpld3的安装

mpld3的使用方法


 

 

 

mpld3的简介

       mpld3——Bringing Matplotlib to the Browser .   mpld3 是matplotlib 和 javascript D3js 得到的可以在网页上绘图的工具。mpld3基于python的graphing library和D3js,汇集了Matplotlib流行的项目的Java库,用于创建web交互式数据可视化。通过一个简单的API,将matplotlib图形导出为HTML代码,这些HTML代码可以在浏览器内使用。
      mpld3项目汇集了流行的基于Python的图形库MaPutTLIB和D3JS,这是用于创建Web交互式数据可视化的流行JavaScript库。结果是一个简单的API,用于将MatMattLIB图形导出到HTML代码中,这些代码可以在浏览器内使用,在标准网页、博客或工具如iPython笔记本中使用。

mpld3
mpld pypi
 

mpld3的安装

pip install mpld3

 

 

mpld3的使用方法

输出结果

实现代码


import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import mpld3
from mpld3 import plugins
np.random.seed(9615)

# generate df
N = 100
df = pd.DataFrame((.1 * (np.random.random((N, 5)) - .5)).cumsum(0),
                  columns=['a', 'b', 'c', 'd', 'e'],)

# plot line + confidence interval
fig, ax = plt.subplots()
ax.grid(True, alpha=0.3)

for key, val in df.iteritems():
    l, = ax.plot(val.index, val.values, label=key)
    ax.fill_between(val.index,
                    val.values * .5, val.values * 1.5,
                    color=l.get_color(), alpha=.4)

# define interactive legend

handles, labels = ax.get_legend_handles_labels() # return lines and labels
interactive_legend = plugins.InteractiveLegendPlugin(zip(handles,
                                                         ax.collections),
                                                     labels,
                                                     alpha_unsel=0.5,
                                                     alpha_over=1.5, 
                                                     start_visible=True)
plugins.connect(fig, interactive_legend)

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('Interactive legend', size=20)

mpld3.show()

 

 

相关文章推荐

mpld3 Example

 

本文章已经生成可运行项目
"C:\Program Files\Python38\python.exe" C:\Users\paobu\PycharmProjects\lab_all\test2.py 回归模型: y = 158.625 + 0.049x1 + -0.913x2 即: y = -366.584 + 0.088x1 + 2.203x2 决定系数 R² = 0.948 C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 32784 (\N{CJK UNIFIED IDEOGRAPH-8010}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 29992 (\N{CJK UNIFIED IDEOGRAPH-7528}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 28040 (\N{CJK UNIFIED IDEOGRAPH-6D88}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 36153 (\N{CJK UNIFIED IDEOGRAPH-8D39}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 21697 (\N{CJK UNIFIED IDEOGRAPH-54C1}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 25903 (\N{CJK UNIFIED IDEOGRAPH-652F}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 20986 (\N{CJK UNIFIED IDEOGRAPH-51FA}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 22810 (\N{CJK UNIFIED IDEOGRAPH-591A}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 20803 (\N{CJK UNIFIED IDEOGRAPH-5143}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 32447 (\N{CJK UNIFIED IDEOGRAPH-7EBF}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 24615 (\N{CJK UNIFIED IDEOGRAPH-6027}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 22238 (\N{CJK UNIFIED IDEOGRAPH-56DE}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 24402 (\N{CJK UNIFIED IDEOGRAPH-5F52}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 27169 (\N{CJK UNIFIED IDEOGRAPH-6A21}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 22411 (\N{CJK UNIFIED IDEOGRAPH-578B}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 25311 (\N{CJK UNIFIED IDEOGRAPH-62DF}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 21512 (\N{CJK UNIFIED IDEOGRAPH-5408}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 25928 (\N{CJK UNIFIED IDEOGRAPH-6548}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 26524 (\N{CJK UNIFIED IDEOGRAPH-679C}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 24180 (\N{CJK UNIFIED IDEOGRAPH-5E74}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 20221 (\N{CJK UNIFIED IDEOGRAPH-4EFD}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 23454 (\N{CJK UNIFIED IDEOGRAPH-5B9E}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 38469 (\N{CJK UNIFIED IDEOGRAPH-9645}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 20540 (\N{CJK UNIFIED IDEOGRAPH-503C}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 39044 (\N{CJK UNIFIED IDEOGRAPH-9884}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\JetBrains\PyCharm 2023.3.5\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 27979 (\N{CJK UNIFIED IDEOGRAPH-6D4B}) missing from current font. FigureCanvasAgg.draw(self) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 32784 (\N{CJK UNIFIED IDEOGRAPH-8010}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 29992 (\N{CJK UNIFIED IDEOGRAPH-7528}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 28040 (\N{CJK UNIFIED IDEOGRAPH-6D88}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 36153 (\N{CJK UNIFIED IDEOGRAPH-8D39}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 21697 (\N{CJK UNIFIED IDEOGRAPH-54C1}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 25903 (\N{CJK UNIFIED IDEOGRAPH-652F}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 20986 (\N{CJK UNIFIED IDEOGRAPH-51FA}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 22810 (\N{CJK UNIFIED IDEOGRAPH-591A}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 20803 (\N{CJK UNIFIED IDEOGRAPH-5143}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 32447 (\N{CJK UNIFIED IDEOGRAPH-7EBF}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 24615 (\N{CJK UNIFIED IDEOGRAPH-6027}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 22238 (\N{CJK UNIFIED IDEOGRAPH-56DE}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 24402 (\N{CJK UNIFIED IDEOGRAPH-5F52}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 27169 (\N{CJK UNIFIED IDEOGRAPH-6A21}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 22411 (\N{CJK UNIFIED IDEOGRAPH-578B}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 25311 (\N{CJK UNIFIED IDEOGRAPH-62DF}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 21512 (\N{CJK UNIFIED IDEOGRAPH-5408}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 25928 (\N{CJK UNIFIED IDEOGRAPH-6548}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 26524 (\N{CJK UNIFIED IDEOGRAPH-679C}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 24180 (\N{CJK UNIFIED IDEOGRAPH-5E74}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 20221 (\N{CJK UNIFIED IDEOGRAPH-4EFD}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 23454 (\N{CJK UNIFIED IDEOGRAPH-5B9E}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 38469 (\N{CJK UNIFIED IDEOGRAPH-9645}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 20540 (\N{CJK UNIFIED IDEOGRAPH-503C}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 39044 (\N{CJK UNIFIED IDEOGRAPH-9884}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) C:\Program Files\Python38\lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 27979 (\N{CJK UNIFIED IDEOGRAPH-6D4B}) missing from current font. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) 进程已结束,退出代码为 0
最新发布
10-27
D:\Tools\PyCharm 2024.1.4\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 20540 (\N{CJK UNIFIED IDEOGRAPH-503C}) missing from font(s) DejaVu Sans. FigureCanvasAgg.draw(self) D:\Tools\PyCharm 2024.1.4\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 30340 (\N{CJK UNIFIED IDEOGRAPH-7684}) missing from font(s) DejaVu Sans. FigureCanvasAgg.draw(self) D:\Tools\PyCharm 2024.1.4\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 24179 (\N{CJK UNIFIED IDEOGRAPH-5E73}) missing from font(s) DejaVu Sans. FigureCanvasAgg.draw(self) D:\Tools\PyCharm 2024.1.4\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 26041 (\N{CJK UNIFIED IDEOGRAPH-65B9}) missing from font(s) DejaVu Sans. FigureCanvasAgg.draw(self) D:\Tools\PyCharm 2024.1.4\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 25968 (\N{CJK UNIFIED IDEOGRAPH-6570}) missing from font(s) DejaVu Sans. FigureCanvasAgg.draw(self) D:\Tools\Python312\Lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 20540 (\N{CJK UNIFIED IDEOGRAPH-503C}) missing from font(s) DejaVu Sans. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) D:\Tools\Python312\Lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 30340 (\N{CJK UNIFIED IDEOGRAPH-7684}) missing from font(s) DejaVu Sans. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) D:\Tools\Python312\Lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 24179 (\N{CJK UNIFIED IDEOGRAPH-5E73}) missing from font(s) DejaVu Sans. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) D:\Tools\Python312\Lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 26041 (\N{CJK UNIFIED IDEOGRAPH-65B9}) missing from font(s) DejaVu Sans. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) D:\Tools\Python312\Lib\site-packages\mpld3\mplexporter\exporter.py:47: UserWarning: Glyph 25968 (\N{CJK UNIFIED IDEOGRAPH-6570}) missing from font(s) DejaVu Sans. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) 这些错误是什么意思
07-01
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个处女座的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值