Pyecharts学习的随手记录
首先需要安装pyecharts这个库
pip3 install pyecharts
这个是geo图,用的geo_type是effect_scatter line,但是很难受的是geo不难像map那样把一个区域都涂上颜色,只能是单点,之前疫情的那种效果就是用map做的
from pyecharts.globals import GeoType
from pyecharts.charts import *
from pyecharts.globals import *
from pyecharts import options as opts
infectedlist = [('广东', 518), ('江苏', 173), ('上海', 65), ('浙江', 369), ('北京', 181), ('四川', 237), ('湖南', 244), ('湖北', 43252),
('山东', 395), ('新疆', 44), ('西藏', 0), ('青海', 0), ('甘肃', 9), ('内蒙古', 41), ('黑龙江', 225), ('吉林', 29),
('辽宁', 37),
('河北', 57), ('河南', 250), ('陕西', 58), ('山西', 37), ('宁夏', 10), ('安徽', 271), ('广西', 116), ('云南', 43),
('贵州', 40), ('重庆', 221), ('江西', 250), ('海南', 39), ('福建', 94), ('香港', 40), ('台湾', 25), ('澳门', 20)]
def geo_lines(infected=None, route=None) -> Geo:
if infected is None:
infected = infectedlist
c = (
Geo()
.add_schema(maptype="china",
zoom=1.5,
is_roam=False,
center=Geo().get_coordinate('重庆'), # 视角中心
# itemstyle_opts=opts.ItemStyleOpts(color="#323c48", ),#地图背景色
emphasis_itemstyle_opts=opts.ItemStyleOpts(color="#31708f"), # 高亮颜色
# emphasis_label_opts=opts.ItemStyleOpts(color="white", ) # 字体颜色
)
.add(
"geo",
infected,
# symbol = 'point',
type_=GeoType.EFFECT_SCATTER,
# type_=ChartType.HEATMAP,
)
.add(
"geo",
route,
type_=ChartType.LINES,
symbol= '',
effect_opts=opts.EffectOpts(
# symbol=SymbolType.ARROW,
symbol_size=6, color='#5f99bb'
), color='blue',
linestyle_opts=opts.LineStyleOpts(curve=0.2),
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(),
title_opts=opts.TitleOpts(title="教师出行路线")
)
)
return c
route = [('自贡', '绵阳'), ('绵阳', '宜宾'), ('宜宾', '北京'), ('北京', '宜宾'), ('宜宾', '绵阳'), ('绵阳', '德阳'), ('德阳', '成都'), ('成都', '绵阳'), ('绵阳', '德阳')]
day = ['2020.1.13', '2020.1.14', '2020.1.15', '2020.1.17', '2020.1.18', '2020.1.19', '2020.1.20', '2020.1.20', '2020.1.22']
def timeline(route, day):
c = Timeline()
temp = list()
for i in range(len(route)):
temp.append(route[i])
c.add(geo_lines(route=temp), day[i])
# c = c.dump_options_with_quotes()
return c
a = timeline(route,day)
a.render_notebook()
# 因为是在jupyter notebook里写的,所以可以直接调用render_notebook()
因为放的是图片,所以不能看到动起来的效果,做完以后是会动的
折线图
from pyecharts.charts import *
import pyecharts.options as opts
def line() -> Line:
week_name_list = ['1.10','1.11','1.12','1.13','1.14','1.15','1.16','1.17','1.18','1.19','1.20','1.21','1.22','1.23','1.24','1.25','1.26','1.27','1.28','1.29','1.30','1.31']
car = [0, 0, 1, 7, 3, 6, 11, 9, 13, 10, 6, 13, 3, 3, 4, 6, 5, 5, 2, 2, 1, 0]
plane = [0, 0, 0, 3, 1, 2, 0, 2, 1, 2, 1, 2, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0]
bus = [0, 0, 2, 2, 3, 0, 4, 1, 1, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
etc = [1, 0, 2, 1, 3, 0, 0, 1, 4, 1, 3, 0, 2, 2, 2, 1, 0, 0, 0, 0, 0, 0]
c = (
Line(init_opts=opts.InitOpts(
#width="1200px", height="600px"
))
.add_xaxis(xaxis_data=week_name_list)
.add_yaxis(
series_name="自驾",
y_axis=car,
is_smooth=True,
markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(type_="max"),
opts.MarkPointItem(type_="min"),
]
),
markline_opts=opts.MarkLineOpts(
data=[opts.MarkLineItem(type_="average", name="平均值")]
),
linestyle_opts=opts.LineStyleOpts(
width=4,
# type_="dashed"
),
)
.add_yaxis(
series_name="飞机",
y_axis=plane,
is_smooth=True,
markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(type_="max"),
opts.MarkPointItem(type_="min"),
]
),
markline_opts=opts.MarkLineOpts(
data=[opts.MarkLineItem(type_="average", name="平均值")]
),
linestyle_opts=opts.LineStyleOpts(
width=4,
# type_="dashed"
),
)
.add_yaxis(
series_name="其他",
y_axis=etc,
is_smooth=True,
markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(type_="max"),
opts.MarkPointItem(type_="min"),
]
),
markline_opts=opts.MarkLineOpts(
data=[opts.MarkLineItem(type_="average", name="平均值")]
),
linestyle_opts=opts.LineStyleOpts(
width=4,
# type_="dashed"
),
)
.add_yaxis(
series_name="乘车",
y_axis=bus,
is_smooth=True,
markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(type_="max"),
opts.MarkPointItem(type_="min"),
]
),
markline_opts=opts.MarkLineOpts(
data=[opts.MarkLineItem(type_="average", name="平均值")]
),
linestyle_opts=opts.LineStyleOpts(
width=4,
# type_="dashed"
),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="教师出行交通工具"),
tooltip_opts=opts.TooltipOpts(trigger="axis"),
toolbox_opts=opts.ToolboxOpts(is_show=True),
xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
)
)
return c
a = line()
a.render_notebook()
柱状图
from pyecharts.charts import *
import pyecharts.options as opts
from pyecharts.commons.utils import JsCode
# [('76581.0', 9), ('64518.0', 7), ('64206.0', 6), ('64227.0', 4), ('64171.0', 4), ('64222.0', 4), ('64167.0', 4), ('64162.0', 4), ('675917.0', 4), ('6338.0', 3)]
teacher = ['76581','64518','64206','64227','64171','644222','64167','64162','675917','6338']
t = ['6338','675917','64162','64167','644222','64171','64227','64206','64518','76581']
rank = [3,4,4,4,4,4,4,6,7,9]
c = (
Bar()
.add_xaxis(t)
.add_yaxis('教师到达城市数量 TOP 10',rank,category_gap="35%")
.reversal_axis()
.set_series_opts(
itemstyle_opts={
"normal": {
"barBorderRadius": [0, 30, 30, 0],
'color':"#749f83",
"shadowColor": "rgb(0, 160, 221)",
}
},
#itemstyle_opts=opts.ItemStyleOpts(color="#d48265"),
)
.set_series_opts(label_opts=opts.LabelOpts(position="right"))
.set_global_opts(toolbox_opts=opts.ToolboxOpts(is_show=True),)
)
c.render_notebook()
这个pyecharts有很多图可以画,具体可以参考官方的手册
类似的还有AntV,阿里巴巴做的,看起来还蛮不错的
如果是专注地图类的图表的话,可以学习mapbox中文官网
还有folium也可以尝试
下面记录一下之前遇到的错误:
好像单纯的pyecharts没有遇到什么错误,有前后端交互的问题。
上次那个项目是用Django写的后端。问题还满多的