pyecharts在数据可视化中的应用

**

使用pyecharts进行数据可视化分析

**

1. 树图绘制

1、使用以下JSON数据绘制树图、矩形树图。代码片

from pyecharts import options as opts
from pyecharts.charts import Tree
from pyecharts.charts import TreeMap
data = [{
    "name": "flare",
    "children": [
        {
            "name": "flex",
            "children": [
                {"name": "FlareVis", "value": 4116}
            ]
        },
        {
            "name": "scale",
            "children": [
                {"name": "IScaleMap", "value": 2105},
                {"name": "LinearScale", "value": 1316},
                {"name": "LogScale", "value": 3151},
                {"name": "OrdinalScale", "value": 3770},
                {"name": "QuantileScale", "value": 2435},
                {"name": "QuantitativeScale", "value": 4839},
                {"name": "RootScale", "value": 1756},
                {"name": "Scale", "value": 4268},
                {"name": "ScaleType", "value": 1821},
                {"name": "TimeScale", "value": 5833}
           ]
        },
        {
            "name": "display",
            "children": [
                {"name": "DirtySprite", "value": 8833}
           ]
        }
    ]
}]

b = (
    Tree()
    .add("", data)
    .set_global_opts(title_opts=opts.TitleOpts(title="使用以下JSON数据绘制树图", subtitle="xuxiaozhen"))
    .render("树形图.html")
)

c = (
    TreeMap()
    .add("数据", data,)
    .set_global_opts(title_opts=opts.TitleOpts(title="使用以下JSON数据绘制矩形树图", subtitle="xuxiaozhen"))
    .render("矩形树图.html")
)

pyecharts在数据可视化中的应用_第1张图片
pyecharts在数据可视化中的应用_第2张图片

2. 散点图矩阵绘制

2、绘制鸢尾花数据的相关性矩阵。代码片

from matplotlib import pyplot as plt
import pandas as pd
import seaborn as sns

file = pd.read_csv("/Users/*****/Downloads/iris.csv")
sns.pairplot(file,hue="Species")
plt.show()

pyecharts在数据可视化中的应用_第3张图片

3. 地理热力图、地图上标注点的绘制

3、在地图上用圆点标出各省的销售额数据。代码片

from pyecharts import options as opts
from pyecharts.charts import Map
import random

province = ['广东', '湖北', '湖南', '四川', '重庆', '黑龙江', '浙江', '山西', '河北', '安徽', '河南', '山东', '西藏']
c = (
    Map()
    .add("销售额", [(i, random.randint(50, 150)) for i in province], "china")
    .set_global_opts(title_opts=opts.TitleOpts(title="各省的销售额数据", subtitle="xuxiaozhen"))
    .render("地图.html")
)

pyecharts在数据可视化中的应用_第4张图片
4、绘制地理热点图展示某连锁企业在湖北省各城市的门店数。代码片

from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType
import random

province = ['武汉', '十堰', '鄂州', '宜昌', '荆州', '孝感', '黄石', '咸宁', '仙桃']
c = (
    Geo()
    .add_schema(maptype="湖北")
    .add("门店数", [(i, random.randint(50, 150)) for i in province],type_=ChartType.HEATMAP)
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    .set_global_opts(
        title_opts=opts.TitleOpts(title="某连锁企业在湖北省各城市的门店数", subtitle="xuxiaozhen"),
        visualmap_opts=opts.VisualMapOpts()
    )
    .render("地理热点图.html")
)

pyecharts在数据可视化中的应用_第5张图片

热力图关键:type_=ChartType.HEATMAP

4. 词云图绘制

5、绘制词云图。代码片

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

data = pd.read_csv("/Users/****/Downloads/word_data.csv")
data2 = data.groupby(by=['category']).agg({'views':sum}).reset_index().values
wordcloud = (
    WordCloud()
    .add("", data2, word_size_range=[8,80])
    .set_global_opts(
        title_opts=opts.TitleOpts(
            title="绘制词云图", title_textstyle_opts=opts.TextStyleOpts(font_size=24), subtitle="xuxiaozhen"),
        tooltip_opts=opts.TooltipOpts(is_show=True)
    )
    .render("词云图.html")
)

pyecharts在数据可视化中的应用_第6张图片

5. 主题河流图绘制

6、绘制主题河流图。代码片

import pyecharts.options as opts
from pyecharts.charts import ThemeRiver
datax = ['分支1', '分支2', '分支3', '分支4', '分支5', '分支6']
datay = [
    ['2015/11/08', 10, '分支1'], ['2015/11/09', 15, '分支1'], ['2015/11/10', 35, '分支1'],
    ['2015/11/14', 7, '分支1'], ['2015/11/15', 2, '分支1'], ['2015/11/16', 17, '分支1'],
    ['2015/11/17', 33, '分支1'], ['2015/11/18', 40, '分支1'], ['2015/11/19', 32, '分支1'],
    ['2015/11/20', 26, '分支1'], ['2015/11/21', 35, '分支1'], ['2015/11/22', 40, '分支1'],
    ['2015/11/23', 32, '分支1'], ['2015/11/24', 26, '分支1'], ['2015/11/25', 22, '分支1'],
    ['2015/11/08', 35, '分支2'], ['2015/11/09', 36, '分支2'], ['2015/11/10', 37, '分支2'],
    ['2015/11/11', 22, '分支2'], ['2015/11/12', 24, '分支2'], ['2015/11/13', 26, '分支2'],
    ['2015/11/14', 34, '分支2'], ['2015/11/15', 21, '分支2'], ['2015/11/16', 18, '分支2'],
    ['2015/11/17', 45, '分支2'], ['2015/11/18', 32, '分支2'], ['2015/11/19', 35, '分支2'],
    ['2015/11/20', 30, '分支2'], ['2015/11/21', 28, '分支2'], ['2015/11/22', 27, '分支2'],
    ['2015/11/23', 26, '分支2'], ['2015/11/24', 15, '分支2'], ['2015/11/25', 30, '分支2'],
    ['2015/11/26', 35, '分支2'], ['2015/11/27', 42, '分支2'], ['2015/11/28', 42, '分支2'],
    ['2015/11/08', 21, '分支3'], ['2015/11/09', 25, '分支3'], ['2015/11/10', 27, '分支3'],
    ['2015/11/11', 23, '分支3'], ['2015/11/12', 24, '分支3'], ['2015/11/13', 21, '分支3'],
    ['2015/11/14', 35, '分支3'], ['2015/11/15', 39, '分支3'], ['2015/11/16', 40, '分支3'],
    ['2015/11/17', 36, '分支3'], ['2015/11/18', 33, '分支3'], ['2015/11/19', 43, '分支3'],
    ['2015/11/20', 40, '分支3'], ['2015/11/21', 34, '分支3'], ['2015/11/22', 28, '分支3'],
    ['2015/11/14', 7, '分支4'], ['2015/11/15', 2, '分支4'], ['2015/11/16', 17, '分支4'],
    ['2015/11/17', 33, '分支4'], ['2015/11/18', 40, '分支4'], ['2015/11/19', 32, '分支4'],
    ['2015/11/20', 26, '分支4'], ['2015/11/21', 35, '分支4'], ['2015/11/22', 40, '分支4'],
    ['2015/11/23', 32, '分支4'], ['2015/11/24', 26, '分支4'], ['2015/11/25', 22, '分支4'],
    ['2015/11/26', 16, '分支4'], ['2015/11/27', 22, '分支4'], ['2015/11/28', 10, '分支4'],
    ['2015/11/08', 10, '分支5'], ['2015/11/09', 15, '分支5'], ['2015/11/10', 35, '分支5'],
    ['2015/11/11', 38, '分支5'], ['2015/11/12', 22, '分支5'], ['2015/11/13', 16, '分支5'],
    ['2015/11/14', 7, '分支5'], ['2015/11/15', 2, '分支5'], ['2015/11/16', 17, '分支5'],
    ['2015/11/17', 33, '分支5'], ['2015/11/18', 40, '分支5'], ['2015/11/19', 32, '分支5'],
    ['2015/11/20', 26, '分支5'], ['2015/11/21', 35, '分支5'], ['2015/11/22', 4, '分支5'],
    ['2015/11/23', 32, '分支5'], ['2015/11/24', 26, '分支5'], ['2015/11/25', 22, '分支5'],
    ['2015/11/26', 16, '分支5'], ['2015/11/27', 22, '分支5'], ['2015/11/28', 10, '分支5'],
    ['2015/11/08', 10, '分支6'], ['2015/11/09', 15, '分支6'], ['2015/11/10', 35, '分支6'],
    ['2015/11/11', 38, '分支6'], ['2015/11/12', 22, '分支6'], ['2015/11/13', 16, '分支6'],
    ['2015/11/14', 7, '分支6'], ['2015/11/15', 2, '分支6'], ['2015/11/16', 17, '分支6'],
    ['2015/11/17', 33, '分支6'], ['2015/11/18', 4, '分支6'], ['2015/11/19', 32, '分支6'],
    ['2015/11/20', 26, '分支6'], ['2015/11/21', 35, '分支6'], ['2015/11/22', 40, '分支6'],
    ['2015/11/23', 32, '分支6'], ['2015/11/24', 26, '分支6'], ['2015/11/25', 22, '分支6']
]

c = (
    ThemeRiver(init_opts=opts.InitOpts(width="1000px",height="500px"))
    .add(series_name=datax,data=datay,singleaxis_opts=opts.SingleAxisOpts(pos_top="50", pos_bottom="50", type_="time"))
    .set_global_opts(
        tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="line"),
        title_opts=opts.TitleOpts(title="绘制主题河流图", subtitle="xuxiaozhen")
    )
    .render("6主题河流图.html")
)

pyecharts在数据可视化中的应用_第7张图片

你可能感兴趣的:(pyecharts在数据可视化中的应用)