pyecharts画图

有了matplotlib和seaborn,为什么要用pyecharts?
多种库有类似特性时,多半是为了解决某一些问题或者场景
附官方文档:https://pyecharts.org/#/zh-cn/intro 全中文注释,YYDS

pyecharts的特性

  • 简洁的 API 设计,支持链式调用
  • 囊括了 30+ 种常见图表
  • 支持主流 Notebook 环境,Jupyter Notebook 和 JupyterLab
  • 可轻松集成至 Flask,Django 等主流 Web 框架
  • 高度灵活的配置项,可轻松搭配出精美的图表
  • 多达 400+ 地图文件以及原生的百度地图,为地理数据可视化提供强有力的支持

常见图表

柱状图Bar

from pyecharts.charts import Bar

bar = Bar()
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
# render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
# 也可以传入路径参数,如 bar.render("mycharts.html")
bar.render()
image.png

散点图

from pyecharts.charts import Scatter

sca = Scatter()
sca.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
sca.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
# render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
# 也可以传入路径参数,如 bar.render("mycharts.html")
sca.render()
image.png

折线图

from pyecharts.charts import Line

line = Line()
line.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
line.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
# render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
# 也可以传入路径参数,如 bar.render("mycharts.html")
line.render()
image.png

饼图

from pyecharts.charts import Pie
from pyecharts import options as opts

产品 = ['苹果','香蕉','橙子','草莓','葡萄']
价格 = [0.4,0.1,0.2,0.1,0.2]

data = list(zip(产品, 价格))

pie = (
    Pie().add(
        # 系列名称,用于 tooltip 的显示,legend 的图例筛选。
        series_name='水果购买比例',
        # 系列数据项,格式为 [(key1, value1), (key2, value2)]
        data_pair=data,
        # 饼图的半径,数组的第一项是内半径,第二项是外半径
        # 默认设置成百分比,相对于容器高宽中较小的一项的一半
        radius=(80,200),
        # 设置图表中的标签选项
        # {b}代表的就是饼图的类别名称  {a}系列名称 {c} 数值 {d}指该数据的具体数值的百分比
        label_opts=opts.LabelOpts(is_show=True,formatter='{b}:{d}%'),
          rosetype= 'radius' # 变成南丁格尔图
              )
)
pie.render('饼图.html')
image.png

词云图

from pyecharts.charts import WordCloud
from pyecharts import options as opts

data = [('我',12),('你',2),('他',22)]

word = WordCloud()
word.add(
    series_name='词云',
# 系列数据项,[(word1, count1), (word2, count2)]
    data_pair = data
)
word.render('词云.html')
image.png

词云图比较适用于一些数据分析和清洗,这里结合一个小案例
这里选用网易云的一个新闻



提取其中文章写成txt


image.png
import jieba
from collections import Counter
from pyecharts.charts import WordCloud
from pyecharts import options as opts

with open('news', 'r', encoding='utf-8') as f1:
    text = f1.read()

# 使用结巴进行分词处理
words = jieba.lcut(text)

# 统计词频
data = Counter(words)

# 筛选出前300个高频词汇
word_counts = data.most_common(300)

wordcloud = WordCloud()
wordcloud.add(
    "news",
    word_counts,
    word_size_range=[10, 100],
    shape='diamond'
)
wordcloud.set_global_opts(
    title_opts=opts.TitleOpts(title="词云图"),
)
wordcloud.render("test_wordcloud.html")
image.png

这样就精准的看到这个文章的一些核心关键词

但实际 类似 的、和、更、特殊字符之类的单个词语在实际中毫无意义,这里在出图前对数据进行一次清洗,这里稍微加个清洗逻辑

import jieba
from collections import Counter
from pyecharts.charts import WordCloud
from pyecharts import options as opts

with open('news', 'r', encoding='utf-8') as f1:
    text = f1.read()

with open('stopwords.txt','r', encoding='utf-8') as f2:
    stopwords = f2.read().splitlines()

# 使用结巴进行分词处理
words = jieba.lcut(text)

# 使用停止词汇筛选数据
filtered_words = [i for i in words if i not in stopwords and i.split()]

# 统计词频
data = Counter(filtered_words)

# 筛选出前300个高频词汇
word_counts = data.most_common(300)

wordcloud = WordCloud()
wordcloud.add(
    "news",
    word_counts,
    word_size_range=[10, 100],
    shape='diamond'
)
wordcloud.set_global_opts(
    title_opts=opts.TitleOpts(title="词云图"),
)
wordcloud.render("test_wordcloud.html")
image.png

这样就比较直观分析到文章的核心意思

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容