pyecharts的浅浅学习
好吧,其实不太难,主要是参数太多了
模板太多了,不过那样子可以直接拿模板
废话少说直接上代码,解释都在注释里面了,没时间扣出来了,每个图都是实操过的,而且都明白原理和代码的含义
and有两种办法创建图例:链式和普通,链式比较简洁
文章目录
- pyecharts的浅浅学习
-
-
- 好吧,其实不太难,主要是参数太多了
- 模板太多了,不过那样子可以直接拿模板
-
-
- 废话少说直接上代码,解释都在注释里面了,没时间扣出来了,每个图都是实操过的,而且都明白原理和代码的含义
- and有两种办法创建图例:链式和普通,链式比较简洁
- geo式
- 地图式
- 饼状图
- 词云图
- 热力图
- 散点图
- 动态散点图
- 树状图!!!!
- 箱型图
- 旭日图,也不太难,就是费眼睛和时间
- 柱状图
-
geo式
from pyecharts.globals import GeoType
city = '杭州'
g = Geo()
g.add_schema(maptype=city)
g.add_coordinate('杭州师范大学仓前校区',120.0109472358, 30.2910373720)
g.add_coordinate('萧山城厢街道',120.2740081170, 30.1605364542)
data_pair = [('杭州师范大学仓前校区', 100),('萧山城厢街道', 500),("杭州",50)]
g.add('', data_pair,type_=GeoType.EFFECT_SCATTER, symbol_size=20)
g.set_global_opts(
visualmap_opts=opts.VisualMapOpts(is_piecewise=True,max_=800),
title_opts=opts.TitleOpts(title="{}-店铺分布".format(city)),
)
g.render('杭州.html')
输出
瑞士的地图
from pyecharts.charts import Geo
import pyecharts.options as opts
geo = (
Geo()
.add_schema(maptype="瑞士")
.set_global_opts(title_opts=opts.TitleOpts(title="瑞士"))
.render("瑞士.html")
)
输出
地图式
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Map
customMap = (
Map()
.add("商家A",
[list(z) for z in zip(Faker.provinces, Faker.days_values)],
"china"
)
.set_series_opts(
label_opts=opts.LabelOpts(
is_show=False
)
)
.set_global_opts(
title_opts=opts.TitleOpts(
title="中国地图",
pos_left="center"
),
legend_opts=opts.LegendOpts(
pos_right="right",
pos_bottom="bottom"
),
visualmap_opts=opts.VisualMapOpts(
is_piecewise=True,
pieces=[
{"value": "1", "label": "A", "color": "red"},
{"value": "2", "label": "B", "color": "orange"},
{"value": "3", "label": "C", "color": "yellow"},
{"value": "4", "label": "D", "color": "green"},
{"value": "5", "label": "E", "color": "blue"},
{"value": "6", "label": "F", "color": "cyan"},
{"value": "7", "label": "G", "color": "purple"}
]
)
)
)
customMap.render("地图式.html")
输出
饼状图
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.faker import Faker
c = (
Pie()
.add("", [list(z) for z in zip(Faker.choose(), Faker.values())])
.set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本示例"))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
.render("饼状图.html")
)
输出
词云图
import pyecharts.options as opts
from pyecharts.charts import WordCloud
data = [
。。。。。。。。。。。。省略掉了
]
原理
系统自动根据里面的占比进行排列。。。
very easy
(
WordCloud()
.add(series_name="热点分析", data_pair=data, word_size_range=[6, 66])
.set_global_opts(
title_opts=opts.TitleOpts(
title="热点分析", title_textstyle_opts=opts.TextStyleOpts(font_size=23)
),
tooltip_opts=opts.TooltipOpts(is_show=True),
)
.render("词云图.html")
)
输出
热力图
import random
from pyecharts import options as opts
from pyecharts.charts import HeatMap
from pyecharts.faker import Faker
value = [[i, j, random.randint(0, 50)] for i in range(24) for j in range(7)]
c = (
HeatMap()
.add_xaxis(Faker.clock)
.add_yaxis("series0", Faker.week, value)
.set_global_opts(
title_opts=opts.TitleOpts(title="HeatMap-基本示例"),
visualmap_opts=opts.VisualMapOpts(),
)
.render("热力图.html")
)
输出
散点图
from pyecharts.charts import Scatter
from pyecharts.faker import Faker
import pyecharts.options as opts
c = (
Scatter()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title="Scatter-VisualMap(Size)"),
visualmap_opts=opts.VisualMapOpts(type_="size", max_=150, min_=20),
)
.render("散点图.html")
)
输出
动态散点图
from pyecharts import options as opts
from pyecharts.charts import EffectScatter
from pyecharts.faker import Faker
from pyecharts.globals import SymbolType
c = (
EffectScatter()
.add_xaxis(Faker.choose())
.add_yaxis("", Faker.values(), symbol=SymbolType.ARROW)
.set_global_opts(title_opts=opts.TitleOpts(title="EffectScatter-不同Symbol"))
.set_global_opts(
xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),
yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),
)
.render("动态散点图.html")
)
输出
树状图!!!!
from pyecharts import options as opts
from pyecharts.charts import Tree
data = [
{
"children": [
{"name": "中山"},
{
"children": [{"children": [{"name": "深圳技术大学,这样好吗。。。"}], "name": "深圳大学"}, {"name": "南方科技大学"}],
"name": "深圳",
},
{
"children": [
{"children": [{"name": "广州大学"}, {"name": "中山大学"}], "name": "某区"},
{"name": "白云区"},
],
"name": "广州",
},
],
"name": "广东",
}
]
c = (
Tree()
.add("", data)
.set_global_opts(title_opts=opts.TitleOpts(title="Tree-基本示例"))
.render("树状图.html")
)
输出
箱型图
from pyecharts import options as opts
from pyecharts.charts import Boxplot
v1 = [
[850, 740, 900, 1070, 930, 850, 950, 980, 980, 880, 1000, 980],
[960, 940, 960, 940, 880, 800, 850, 880, 900, 840, 830, 790],
]
v2 = [
[890, 810, 810, 820, 800, 770, 760, 740, 750, 760, 910, 920],
[890, 840, 780, 810, 760, 810, 790, 810, 820, 850, 870, 870],
]
c = Boxplot()
c.add_xaxis(["expr1", "expr2"])
c.add_yaxis("A", c.prepare_data(v1))
c.add_yaxis("B", c.prepare_data(v2))
c.set_global_opts(title_opts=opts.TitleOpts(title="BoxPlot-基本示例"))
c.render("箱型图.html")
输出
旭日图,也不太难,就是费眼睛和时间
from pyecharts.charts import Sunburst
from pyecharts import options as opts
data = [
opts.SunburstItem(
name="Grandpa",
children=[
opts.SunburstItem(
name="Uncle Leo",
value=15,
children=[
opts.SunburstItem(name="Cousin Jack", value=2),
opts.SunburstItem(
name="Cousin Mary",
value=5,
children=[opts.SunburstItem(name="Jackson", value=2)],
),
opts.SunburstItem(name="Cousin Ben", value=4),
],
),
opts.SunburstItem(
name="Father",
value=10,
children=[
opts.SunburstItem(name="Me", value=5),
opts.SunburstItem(name="Brother Peter", value=1),
],
),
],
),
opts.SunburstItem(
name="Nancy",
children=[
opts.SunburstItem(
name="Uncle Nike",
children=[
opts.SunburstItem(name="Cousin Betty", value=1),
opts.SunburstItem(name="Cousin Jenny", value=2),
],
)
],
),
]
sunburst = (
Sunburst(init_opts=opts.InitOpts(width="1000px", height="600px"))
.add(series_name="", data_pair=data, radius=[0, "90%"])
.set_global_opts(title_opts=opts.TitleOpts(title="Sunburst-基本示例"))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}"))
.render("旭日图.html")
)
输出
柱状图
2个y的
from pyecharts import options as opts
from pyecharts.charts import Bar, Page
from pyecharts.faker import Faker
from pyecharts.globals import ThemeType
from pyecharts import options as pots
bar = Bar()
bar.add_xaxis(["a",'b','c','d'])
bar.add_yaxis('一号选手',[1,2,3,4])
bar.add_yaxis('二号选手',[5,6,7,8])
bar.set_global_opts(title_opts={"text": "同学们的生活开支"})
bar.render('柱状图.html')
输出
缩放的柱状图,可以拖动的
c = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
.add_xaxis(Faker.days_attrs)
.add_yaxis("商家1", Faker.days_values, color=Faker.rand_color())
.set_global_opts(
title_opts=opts.TitleOpts(title="缩放的柱状图"),
datazoom_opts=opts.DataZoomOpts(orient="horizontal"),
)
.render("nmd.html")
)
输出
3d的柱状图
import random
from pyecharts import options as opts
from pyecharts.charts import Bar3D
from pyecharts.faker import Faker
data = [(i, j, random.randint(0, 12)) for i in range(6) for j in range(24)]
c = (
Bar3D()
.add(
"",
[[d[1], d[0], d[2]] for d in data],
xaxis3d_opts=opts.Axis3DOpts(Faker.clock, type_="category"),
yaxis3d_opts=opts.Axis3DOpts(Faker.week_en, type_="category"),
zaxis3d_opts=opts.Axis3DOpts(type_="value"),
)
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(max_=20),
title_opts=opts.TitleOpts(title="Bar3D-基本示例"),
)
.render("3d柱状图.html")
)
输出
赶ddl真好玩,xs