matplotlib生成的图可以嵌入到网页吗?
matplotlib与django如何集成?
在实际开发中难免会有这样的需求,将matplotlib生成的图片嵌入到网页当中,并且可以进行数据交互,万幸的是官方给出了解决方案,最新版也给出了网页显示的相关接口,唯一缺陷就是生成的是图片类型,不能很好地做一些页面交互效果!
- import base64
- from io import BytesIO
- from matplotlib.figure import Figure
-
- class IndexView(TemplateView):
-
- template_name = "erv/index.html"
-
- def get_context_data(self, **kwargs):
- context = super().get_context_data(**kwargs)
- context['img'] = self.get_img()
- return context
-
- def get_img(self):
- # matplotlib画饼图
- labels = (
- 'Simple sequence repeats',
- 'Other',
- 'LINEs',
- 'SINEs',
- 'DNA transposons',
- 'Protein-coding genes',
- 'LTR RETROTRANSPOSONS'
- )
- sizes = [3, 50, 21