Pyecharts
Pyecharts = Python + Echarts
数据分析遇上数据可视化时,Pyecharts就诞生了。
绘图
语法
from pyecharts.charts import*
fun = (Function()
.add...
.add...
)
fun.render()
虚拟数据
Jupyter Notebook
点击橙框框,浏览器会跳出如下页面(打开的窗口不能删)
点击红框框创建新页面
#放在最前的
from pyecharts.globals import CurrentConfig, NotebookType
CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_NOTEBOOK
CurrentConfig.ONLINE_HOST
#导入相关库
from pyecharts.charts import *
from pyecharts.faker import Faker
import pyecharts.options as opts
import random
图表绘制
漏斗图
graph = (Funnel()
.add("漏斗图",
[list(z) for z in zip(Faker.choose(), Faker.values())])
)
graph.render_notebook()
仪表盘
graph = (Gauge()
.add("时速仪表盘",
[("时速", 40)],
detail_label_opts = opts.GaugeDetailOpts(
offset_center=[0, "30%"], formatter="{value}Km/m")
)
)
graph.render_notebook()
水球图
graph = (Liquid()
.add("水资源占比", [0.8, 0.6]))
graph.render_notebook()
饼图
graph = (Pie()
.add("饮料销售数量",
[list(z) for z in zip(Faker.choose(), Faker.values())]
)
)
graph.render_notebook()
极坐标系
graph = (Polar()
.add("南海各岛屿", Faker.values(), type_='scatter'))
graph.render_notebook()
雷达图
graph = (Radar()
.add_schema(schema=[
opts.RadarIndicatorItem(name="物攻", color="#000"),
opts.RadarIndicatorItem(name="物防", color="#000"),
opts.RadarIndicatorItem(name="法术", color="#000"),
opts.RadarIndicatorItem(name="法防", color="#000"),
opts.RadarIndicatorItem(name="生命值", color="#000"),
opts.RadarIndicatorItem(name="法力值", color="#000"),
opts.RadarIndicatorItem(name="暴击值", color="#000")
])
.add('',[Faker.values(), Faker.values(), Faker.values()]))
graph.render_notebook()
旭日图
data = [
{"name": "湖南",
"children": [
{"name": "长沙",
"children": [
{"name": "雨花区", "value": 55},
{"name": "岳麓区", "value": 34},
{"name": "天心区", "value": 144},
]},
{"name": "常德",
"children": [
{"name": "武陵区", "value": 156},
{"name": "鼎城区", "value": 134},
]},
{"name": "湘潭", "value": 87},
{"name": "株洲", "value": 23},
],
},
{"name": "湖北",
"children": [
{"name": "武汉",
"children": [
{"name": "洪山区", "value": 55},
{"name": "东湖高新", "value": 78},
{"name": "江夏区", "value": 34},
]},
{"name": "鄂州", "value": 67},
{"name": "襄阳", "value": 34},
],
},
{"name": "北京", "value": 235}
]
graph = (Sunburst()
.add("企业数量",data_pair=data))
graph.render_notebook()
词云图
words = [
("hey", 230),
("jude", 124),
("dont", 436),
("make", 255),
("it", 247),
("bad", 244),
("Take", 138),
("a sad song", 184),
("and", 12),
("make", 165),
("it", 247),
("better", 182),
("remember", 255),
("to", 150),
("let", 162),
("her", 266),
("into", 60),
("your", 82),
("heart", 173),
("then", 365),
("you", 360),
("can", 282),
("start", 273),
("make", 265),
]
graph = (WordCloud()
.add("lyh词频统计", words))
graph.render_notebook()
折线图
graph = (Line()
.add_xaxis(Faker.choose())
.add_yaxis('销售数量',Faker.values())
)
graph.render_notebook()
柱形图
graph = (Bar()
.add_xaxis(Faker.choose())
.add_yaxis("当地某商品销量",Faker.values())
)
graph.render_notebook()
箱线图
graph = (Boxplot()
.add_xaxis(Faker.choose())
.add_yaxis("",Boxplot.prepare_data([Faker.values() for i in Faker.choose()]))
)
graph.render_notebook()
散点图
graph = (Scatter()
.add_xaxis(Faker.choose())
.add_yaxis("",Faker.values())
)
graph.render_notebook()
涟漪特效散点图
graph = (EffectScatter()
.add_xaxis(Faker.choose())
.add_yaxis("",Faker.values()))
graph.render_notebook()
矩形树图
data = [
{"value": 40, "name": "我是A"},
{
"value": 180,
"name": "我是B",
"children": [
{
"value": 76,
"name": "我是B.children",
"children": [
{"value": 12, "name": "我是B.children.a"},
{"value": 28, "name": "我是B.children.b"},
{"value": 20, "name": "我是B.children.c"},
{"value": 16, "name": "我是B.children.d"},
],
}
],
},
]
graph = (
TreeMap()
.add("", data)
)
graph.render_notebook()
GEO地图
province = [
'广东', '湖北', '湖南', '四川', '重庆', '黑龙江',
'浙江', '山西', '河北', '安徽', '河南', '山东']
data = [(p, random.randint(50, 150)) for p in province]
geo = (Geo()
.add_schema(maptype='china')
.add('企业A',data,type_='scatter'))
geo.render_notebook()
Map地图
province = [
'广东', '湖北', '湖南', '四川', '重庆', '黑龙江',
'浙江', '山西', '河北', '安徽', '河南', '山东']
data = [(p, random.randint(50, 150)) for p in province]
Map = (Map()
.add('企业A', data, 'china')
)
Map.render_notebook()
三维散点图
data = [(random.ranint(0, 100), random.randint(0, 100), random.randint(0, 100)) for _ in range(100)]
scatter3D = (Scatter3D()
.add("", data)
)
scatter3D.render_notebook()
三维折线图
import math
data = []
for t in range(0, 1000):
x = math.cos(t/10)
y = math.sin(t/10)
z = t/10
data.append([x, y, z])
line3D = (Line3D()
.add("",data,
xaxis3d_opts=opts.Axis3DOpts(type_='value'),
yaxis3d_opts=opts.Axis3DOpts(type_='value')
)
)
line3D.render_notebook()
三位地图
province = [
'广东', '湖北', '湖南', '四川', '重庆', '黑龙江',
'浙江', '山西', '河北', '安徽', '河南', '山东']
data = [(i, random.randint(50, 150)) for i in province]
map3d = (
Map3D()
.add("",data_pair=data, maptype='china')
)
map3d.render_notebook()
(上面的三张地图发布不出来)