pyecharts 官方文档
https://pyecharts.org/#/
1.Django 与 pyecharts
1.1 官网的在 Django 中使用 pyecharts教程 :
https://pyecharts.org/#/zh-cn/web_django
其中配置pyecharts_django_demo/urls.py
中,修改如下,可以不用再demo/下创建urls.py(当然这也可能是新版本的标准)
from django.conf.urls import include, url
from django.contrib import admin
from demo.views import index
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', index)
]
这样打开 http://127.0.0.1:8000/index/ , 即可在浏览器中看到如下界面
绘制词云
按照网上教程操作一直报错,原来是官方导包的方式改了
from pyecharts import options as opts
from pyecharts.charts import Page, WordCloud
from pyecharts.globals import SymbolType
words = [
("Sam S Club", 10000),
("Macys", 6181),
("Amy Schumer", 4386),
("Jurassic World", 4055),
("Charter Communications", 2467),
("Chick Fil A", 2244),
("Planet Fitness", 1868),
("Pitch Perfect", 1484),
("Express", 1112),
("Home", 865),
("Johnny Depp", 847),
("Lena Dunham", 582),
("Lewis Hamilton", 555),
("KXAN", 550),
("Mary Ellen Mark", 462),
("Farrah Abraham", 366),
("Rita Ora", 360),
("Serena Williams", 282),
("NCAA baseball tournament", 273),
("Point Break", 265),
]
def wordcloud_base() -> WordCloud:
c = (
WordCloud()
.add("", words, word_size_range=[20, 100])
.set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-基本示例"))
)
return c
解释:上面的函数wordcloud_base()
后面的-> WordCloud
是指这个函数返回值为一个 WordCloud类的对象 (有待修改),这是python中的 注解 . 还有一种是
def wordcloud_base(s: 'str')-> WordCloud:
括号中的是希望传入的参数类型.
如果我们想在Django中显示词云的话,只需对上面demo做修改,
(1) views.py中添加
def wordcloud(request):
words = [
("Sam S Club", 10000),
("Macys", 6181),
("Amy Schumer", 4386),
("Jurassic World", 4055),
("Charter Communications", 2467),
("Chick Fil A", 2244),
("Planet Fitness", 1868),
("Pitch Perfect", 1484),
("Express", 1112),
("Home", 865),
("Johnny Depp", 847),
("Lena Dunham", 582),
("Lewis Hamilton", 555),
("KXAN", 550),
("Mary Ellen Mark", 462),
("Farrah Abraham", 366),
("Rita Ora", 360),
("Serena Williams", 282),
("NCAA baseball tournament", 273),
("Point Break", 265),
]
c = (
WordCloud()
.add("", words, word_size_range=[20, 100])
.set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-基本示例"))
)
return HttpResponse(c.render_embed())
(2) 然后在urls.py中添加
from django.conf.urls import include, url
from django.contrib import admin
from demo.views import index,wordcloud
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url(r'demo/', include('demo.urls')), # <---
url(r'^index/', index),
url(r'^wordcloud/', wordcloud)
]
(3)浏览器中打开 http://127.0.0.1:8000/wordcloud/ 即可看到如下
对于官方这个demo,使用的哪个模板有点搞不清楚,于是借鉴下面博客的内容,加以改进,自己制定一个前端页面
推荐方式
def wordcloud(request):
words = [
("Sam S Club", 10000),
("Macys", 6181),
("Amy Schumer", 4386),
("Jurassic World", 4055),
("Charter Communications", 2467),
("Chick Fil A", 2244),
("Planet Fitness", 1868),
("Pitch Perfect", 1484),
("Express", 1112),
("Home", 865),
("Johnny Depp", 847),
("Lena Dunham", 582),
("Lewis Hamilton", 555),
("KXAN", 550),
("Mary Ellen Mark", 462),
("Farrah Abraham", 366),
("Rita Ora", 360),
("Serena Williams", 282),
("NCAA baseball tournament", 273),
("Point Break", 265),
]
c = (
WordCloud()
.add("", words, word_size_range=[20, 100])
.set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-基本示例"))
)
# return HttpResponse(c.render_embed())
data = {'data': c.render_embed()}
return render(request,"simple_chart2.html",data)
1.2 Django中实现页面跳转
现在有两个html文件,分别为simple_chart.html
和 simple_chart2.html
,我们在simple_chart2.html
中加一个链接实现点击之后跳转到simple_chart.html
,
- simple_chart.html在urls.py中对应index
- simple_chart2.html在urls.py中对应wordcloud
在simple_chart2.html中添加如下链接
<a href="{% url 'index' %}">static_page_B</a>
注意,urls.py中配置url必须加上name
参数才行,否则会反解析错误
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url(r'demo/', include('demo.urls')), # <---
url(r'^index/', index,name='index'),
url(r'^wordcloud/', wordcloud,name='wordcloud')
]
1.3 一个页面内多个图表
实现这个功能要充分理解views.py
中函数的返回值,也就是render()
的三个参数
return render(request, "simple_chart2.html", data)
最后的data(context)
是一个字典,里面包含了要在simple_chart2.html
界面渲染的数据,simple_chart2.html
内容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>词云</title>
<body>
{{ data1|safe }}
{{ data2|safe }}
<a href="{% url 'index' %}">static_page_B</a>
</body>
</html>
其中data1
和data2
是在views.py
中生成的
data = {}
data['data1'] = c.render_embed()
data['data2'] = c2.render_embed()
return render(request, "simple_chart2.html", data)
2.生成词云所需的元组
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
from pyecharts import options as opts
from pyecharts.charts import Page, WordCloud
from pyecharts.globals import SymbolType
data = {'词语':['续航好','速度快','发热小'],'词频':[100,500,120]}
df = DataFrame(data)
word_frequence = [(x[0],x[1]) for x in df.values]
# word_frequence
def wordcloud_base() -> WordCloud:
c = (
WordCloud()
.add("", word_frequence, word_size_range=[20, 100])
.set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-基本示例"))
)
return c
wordcloud_base().render() # 生成的HTML存在了 C:\\Users\\asus\\render.html