PyEcharts学习
官网
安装
pip install pyecharts
from pyecharts.charts import Bar
bar=Bar()
bar.add_xaxis(['衬衣','短库','长袖','短袖','棉花','鞋子','裤子'])
bar.add_yaxis('商家',[2,23,45,56,34,90,12])
bar.render()#渲染
快速使用
简单配置
from pyecharts.charts import Bar
bar=(
Bar()
.add_xaxis(['衬衣','短库','长袖','短袖','棉花','鞋子','裤子'])
.add_yaxis('商家',[2,23,45,56,34,90,12])
#配置
.set_global_opts(title_opts={'text':'主标题','subtext':'副标题'})
)
bar.render()#渲染
渲染成图片文件
安装
pip install snapshot-selenium
from pyecharts.charts import Bar
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
bar=(
Bar()
.add_xaxis(['衬衣','短库','长袖','短袖','棉花','鞋子','裤子'])
.add_yaxis('商家',[2,23,45,56,34,90,12])
)
make_snapshot(snapshot,bar.render(),'bar.png')
使用主题
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
from pyecharts.globals import ThemeType
bar=(
Bar(
init_opts=opts.InitOpts(theme=ThemeType.DARK)
)
.add_xaxis(['衬衣','短库','长袖','短袖','棉花','鞋子','裤子'])
.add_yaxis('商家',[2,23,45,56,34,90,12])
)
bar.render()
全局配置项
初始化配置项
from pyecharts.faker import Faker
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType,RenderType
bar=(
Bar(
#InitOpts:初始化配置项
init_opts=opts.InitOpts(
width='700px',
height='800px',#图表画布大小,css长度单位
renderer=RenderType.CANVAS,#渲染风格,可选:canvas,svg
page_title='网页标题',
theme=ThemeType.DARK,#主题
bg_color='red'#背景颜色
)
)
.add_xaxis(Faker.choose())
.add_yaxis('商家A',Faker.values())
.add_yaxis('商家B',Faker.values())
)
bar.render()
标题配置项
from pyecharts.faker import Faker
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
bar = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis('商家A', Faker.values())
.add_yaxis('商家B', Faker.values())
# 全局配置项
.set_global_opts(
# TitleOpts:标题配置项
title_opts=opts.TitleOpts(
title='柱形图', # 主标题
title_link='https://www.baidu.com', # 主标题点击跳转链接
title_target='blank', # blank新窗口打开,self 当前窗口打开
subtitle='副标题',
subtitle_link='https://www.baidu.com',
subtitle_target='blank',
# 位置
pos_left='20px',
pos_top='0px',
padding=10, # 内边距
item_gap=4, # 主标题与副标题的间隙
),
)
)
bar.render()
区域缩放项
from pyecharts.faker import Faker
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
bar = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis('商家A', Faker.values())
.add_yaxis('商家B', Faker.values())
# 全局配置项
.set_global_opts(
#DataZoomOpts:区域缩放项
datazoom_opts=opts.DataZoomOpts(
is_show=True, # 是否显行组件
type_='slider', # 组件的类型,slider,inside
is_realtime=True, # 拖动时是否实时更新图表
range_start=20, # 数据窗口的起始位置,百分比
range_end=80, # 数据窗口的结束位置,百分比
orient='horizontal', # 组件放置位置,默认水平horizontal或垂直vertical
is_show_data_shadow=False # 是否锁定选择区域
),
)
)
bar.render()
图例配置
from pyecharts.faker import Faker
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
bar = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis('商家A', Faker.values())
.add_yaxis('商家B', Faker.values())
# 全局配置项
.set_global_opts(
#LegendOpts:图例配置项
legend_opts=opts.LegendOpts(
type_='plain',#图例类型:plain普通类型,scroll:滚动翻页类型
is_show=True,#是否显示图例
pos_left='20%',#图例位置
orient='vertical',#方向
selected_mode='single',#选择模式:True 开启图例点击,False 关闭图例点击,single 单选,multiple 多选
align='right',#图例和图标的位置
padding=10,#内边距
item_gap=5,#图例中每项之间的间距
item_width=30,#项的宽度
item_height=15,#项的高度
inactive_color='#ccc',#图列关闭时的颜色
legend_icon='roundRect',#常见图标:circle,rect,roundRect,triangle,diamond,arrov
)
)
)
bar.render()
视觉映射配置
from pyecharts.faker import Faker
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
bar = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis('商家A', Faker.values())
.add_yaxis('商家B', Faker.values())
# 全局配置项
.set_global_opts(
#VisualMapOpts:视觉映射配置
visualmap_opts=opts.VisualMapOpts(
is_show=True,
type_='color',#color 或size
min_=0,#最小值
max_=150,#最大值
range_opacity=0.7,#图元和文字透明度
range_text=['max','min'],#两段的文本
range_color=['blue','red','pink'],#过渡颜色
is_piecewise=True,#是否分段
is_inverse=True,#是否反转
)
)
)
bar.render()
提示框配置项
from pyecharts.faker import Faker
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
bar = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis('商家A', Faker.values())
.add_yaxis('商家B', Faker.values())
# 全局配置项
.set_global_opts(
#TooltipOpts:提示框配置项
tooltip_opts=opts.TooltipOpts(
is_show=True,
#触发类型:item 数据项,一般用于:散点图,柱形图,饼图
# :axis 坐标轴,提示线,主要用于条形图,折线图等
trigger='item',
#触发条件:mousemove,click,mousemove|click
trigger_on='click',
is_show_content=True,#是否显示提示框浮层
#标签内容的格式
#字符串中的模板变量:
#{a}:系列名series_name
#{b}:数据名
#{c}:值
formatter='{a}:{b}-{c}',
border_color='pink',#边框颜色
border_width=1,#边框宽度
background_color='blue'#背景颜色
)
)
)
bar.render()
坐标轴配置项
from pyecharts.faker import Faker
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
bar = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis('商家A', Faker.values())
.add_yaxis('商家B', Faker.values())
# 全局配置项
.set_global_opts(
#AxisOpts:坐标轴配置项
xaxis_opts=opts.AxisOpts(
is_show=True,#是否显示X轴
#坐标轴类型:
# value:数值轴,用于连续数据
# category:类目轴,适用于离散数据,比如,星期一,星期二等
# time:时间轴,适用于连续的时序数据
type_='category'
),
yaxis_opts=opts.AxisOpts(
is_show=True,
#不显示y轴的线
axisline_opts=opts.AxisLineOpts(is_show=False),
#不显示y轴的刻度
axistick_opts=opts.AxisTickOpts(is_show=False)
)
)
)
bar.render()
系列配置项
图元样式配置项
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
bar = (
Line()
.add_xaxis(Faker.choose())
.add_yaxis('商家A', Faker.values())
.add_yaxis('商家B', Faker.values())
#全局配置
.set_global_opts(
title_opts=opts.TitleOpts(title='折线图'),
#提示线
tooltip_opts=opts.TooltipOpts(trigger='axis')
)
#系列配置项
.set_series_opts(
#ItemStyleOpts:图元样式配置项
#图的颜色
# 使用纯色
# RGB,rgb(120,120,120)
# RGBA,rgba(120,120,120,0.5)
# 十六进制:#ccc
color='blue',
opacity=0.6,
border_color='green',
border_width=2
)
)
bar.render()
线样式配置项
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
bar = (
Line()
.add_xaxis(Faker.choose())
.add_yaxis('商家A', Faker.values())
.add_yaxis('商家B', Faker.values())
#全局配置
.set_global_opts(
title_opts=opts.TitleOpts(title='折线图'),
#提示线
tooltip_opts=opts.TooltipOpts(trigger='axis')
)
#系列配置项
.set_series_opts(
#LineStyleOpts:线样式配置项
linestyle_opts=opts.LineStyleOpts(
is_show=True,
width=2,#线宽
color='green',#线颜色
type_='dashed',#直线solid,虚线dashed,点线dotted
)
)
)
bar.render()
标签配置项
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
bar = (
Line()
.add_xaxis(Faker.choose())
.add_yaxis('商家A', Faker.values())
.add_yaxis('商家B', Faker.values())
#全局配置
.set_global_opts(
title_opts=opts.TitleOpts(title='折线图'),
#提示线
tooltip_opts=opts.TooltipOpts(trigger='axis')
)
#系列配置项
.set_series_opts(
#LabelOpts:标签配置项
label_opts=opts.LabelOpts(
is_show=True,
position='top',#位置
color='red',#颜色
font_size=14,#大小
font_family='Arial',#字体
font_style='italic',#是否斜体,italic
font_weight='bold',#是否加粗 bold
#标签旋转,-90到90
rotate=-40
)
)
)
bar.render()
标记点配置项
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
bar = (
Line()
.add_xaxis(Faker.choose())
.add_yaxis('商家A', Faker.values())
.add_yaxis('商家B', Faker.values())
#全局配置
.set_global_opts(
title_opts=opts.TitleOpts(title='折线图'),
#提示线
tooltip_opts=opts.TooltipOpts(trigger='axis')
)
#系列配置项
.set_series_opts(
#MarkPointOpts:标记点配置项
markpoint_opts=opts.MarkPointOpts(
data=[
#type_:特殊标记类型,min,max,average
#symbol:标记点的图形
#symbol_size:标记点的大小
opts.MarkPointItem(type_='max',symbol='pin',symbol_size=50),
opts.MarkPointItem(type_='min')
]
)
)
)
bar.render()
标记线
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
bar = (
Line()
.add_xaxis(Faker.choose())
.add_yaxis('商家A', Faker.values())
.add_yaxis('商家B', Faker.values())
#全局配置
.set_global_opts(
title_opts=opts.TitleOpts(title='折线图'),
#提示线
tooltip_opts=opts.TooltipOpts(trigger='axis')
)
#系列配置项
.set_series_opts(
#MarkLineOpts:标记线
markline_opts=opts.MarkLineOpts(
data=[
opts.MarkLineItem(type_='average')
],
label_opts=opts.LabelOpts(
color='red'
)
)
)
)
bar.render()
饼图
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
pie=(
Pie()
.add('',[list(i) for i in zip(Faker.choose(),Faker.values())])
.set_colors(['red','blue','green','orange','yellow','pink','black'])
.set_global_opts(
title_opts=opts.TitleOpts(title='设置颜色'),
legend_opts=opts.LegendOpts(type_='scroll',pos_left='80%',orient='vertical')
)
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
)
pie.render()
玫瑰图
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
pie=(
Pie()
.add('',[list(i) for i in zip(Faker.choose(),Faker.values())],
radius=['30%','75%'],
center=['25%','50%'],
rosetype='radius'
)
.set_global_opts(
title_opts=opts.TitleOpts(title='玫瑰图'),
legend_opts=opts.LegendOpts(type_='scroll',pos_left='80%',orient='vertical')
)
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
)
pie.render()
柱形图
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
bar=(
Bar(
init_opts=opts.InitOpts(
animation_opts=opts.AnimationOpts(
animation_delay=1000,#动画延时1秒钟
animation_easing='elasticOut'#弹性动画
)
)
)
.add_xaxis(Faker.choose())
.add_yaxis('商家A',Faker.values())
.add_yaxis('商家B',Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title='柱形图'),
legend_opts=opts.LegendOpts(type_='scroll',pos_left='80%',orient='vertical')
)
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
)
bar.render()
添加js代码
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
from pyecharts.commons.utils import JsCode
bar=(
Bar(
init_opts=opts.InitOpts(
bg_color={
'image':JsCode('img'),
'repeat':'no-repeat'
}
)
)
.add_xaxis(Faker.choose())
.add_yaxis('商家A',Faker.values())
.add_yaxis('商家B',Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title='柱形图添加JS代码'),
legend_opts=opts.LegendOpts(type_='scroll',pos_left='80%',orient='vertical')
)
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
#添加js代码
.add_js_funcs(
"""
var img=new Image();
img.src='https://ts3.cn.mm.bing.net/th?id=OSK.HEROAswkkmw5w3FXgT4DZB0RybGod_LpTISZPIztKg7SV0Q&w=472&h=280&c=13&rs=2&o=6&oif=webp&dpr=1.3&pid=SANGAM'
"""
)
)
bar.render()
堆叠柱形图
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
from pyecharts.commons.utils import JsCode
bar=(
Bar(
)
.add_xaxis(Faker.choose())
.add_yaxis('商家A',Faker.values(),stack='abc')
.add_yaxis('商家B',Faker.values(),stack='abc')
.set_global_opts(
title_opts=opts.TitleOpts(title='堆叠柱形图'),
legend_opts=opts.LegendOpts(type_='scroll',pos_left='80%',orient='vertical'),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)),
datazoom_opts=[
opts.DataZoomOpts(),#x轴缩放
opts.DataZoomOpts(type_='inside')#鼠标缩放
]
)
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
)
bar.render()
条形图
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
from pyecharts.commons.utils import JsCode
bar=(
Bar()
.add_xaxis(Faker.choose())
.add_yaxis('商家A',Faker.values())
.add_yaxis('商家B',Faker.values())
.reversal_axis()#翻转轴,条形图
.set_global_opts(
title_opts=opts.TitleOpts(title='条形图'),
)
.set_series_opts(
label_opts=opts.LabelOpts(position='right')
)
)
bar.render()
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
from pyecharts.commons.utils import JsCode
bar=(
Bar()
.add_xaxis(Faker.choose())
.add_yaxis('商家A',Faker.values(),gap='0%')
.add_yaxis('商家B',Faker.values(),gap='0%')
.set_global_opts(
title_opts=opts.TitleOpts(title='不同系列柱子之间的距离'),
)
.set_series_opts(
label_opts=opts.LabelOpts(position='right')
)
)
bar.render()
直方图
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
from pyecharts.commons.utils import JsCode
bar=(
Bar()
.add_xaxis(Faker.choose())
.add_yaxis('商家A',Faker.values(),category_gap=0)
.set_global_opts(
title_opts=opts.TitleOpts(title='单系列柱子之间的距离,直方图'),
)
)
bar.render()
JsCode自定义柱状颜色
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType
from pyecharts.commons.utils import JsCode
color_fun="""
function(params){
if(params.value >0 && params.value<50){
return 'red';
}
else if (params.value >50 && params.value<100){
return 'green';
}
else{
return 'blue';
}
}
"""
bar=(
Bar()
.add_xaxis(Faker.choose())
.add_yaxis('商家A',Faker.values(),itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_fun)))
.set_global_opts(
title_opts=opts.TitleOpts(title='JsCode自定义柱状颜色'),
)
)
bar.render()
象形柱状图
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
bar=(
PictorialBar()
.add_xaxis(Faker.choose())
.add_yaxis('商家A',
Faker.values(),
label_opts=opts.LabelOpts(is_show=False),
symbol_size=10,#符号大小
is_symbol_clip=True,#符号裁剪
symbol_repeat='fixed',#重复方式
symbol=SymbolType.ROUND_RECT,#符号类型
)
.reversal_axis()
.set_global_opts(
title_opts=opts.TitleOpts(title='象形柱状图'),
xaxis_opts=opts.AxisOpts(is_show=False),#不显示X轴
yaxis_opts=opts.AxisOpts(
axistick_opts=opts.AxisOpts(is_show=False),#不显示y轴刻度
axisline_opts=opts.AxisOpts(is_show=False)#不显示y轴的线
)
)
)
bar.render()
雷达图
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,Radar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
v1=[[4300,10000,28000,35000,50000,19000]]
v2=[[5000,14000,28000,31000,50000,19000]]
bar=(
Radar()
.add_schema(
schema=[
opts.RadarIndicatorItem(name='项目1',max_=6000),
opts.RadarIndicatorItem(name='项目2',max_=16000),
opts.RadarIndicatorItem(name='项目3',max_=30000),
opts.RadarIndicatorItem(name='项目4',max_=38000),
opts.RadarIndicatorItem(name='项目5',max_=60000),
opts.RadarIndicatorItem(name='项目6',max_=22000),
]
)
.set_global_opts(title_opts=opts.TitleOpts(title='雷达图'))
.add('数据1',v1,color='blue')
.add('数据2',v2)
.set_series_opts(label_opts=opts.LabelOpts(is_show=True))
)
bar.render()
拆线图
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,Radar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
line=(
Line(
init_opts=opts.InitOpts(width='1000px',height='500px')
)
.add_xaxis(Faker.week)
.add_yaxis('商家A',
Faker.values(),
symbol='triangle',#点符号类型:triangle三角形
symbol_size=20,#点的大小
linestyle_opts=opts.LineStyleOpts(
color='green',width=2,type_='dashed'
),#线条样式
label_opts=opts.LabelOpts(
is_show=False
),#标签
itemstyle_opts=opts.ItemStyleOpts(
border_color='green',border_width=2,color='yellow'
),#点的属性
markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(type_='max'),#最大值
opts.MarkPointItem(type_='mih'),#最小值
]
),#标注点
markline_opts=opts.MarkLineOpts(
data=[
opts.MarkLineItem(type_='average'),#平均值
]
),#标注线
)
.add_yaxis('商家A',Faker.values(),is_smooth=True)#平滑曲线
.set_global_opts(
title_opts=opts.TitleOpts(title='拆线图'),
tooltip_opts=opts.TooltipOpts(trigger='axis'),#提示线
yaxis_opts=opts.AxisOpts(
type_='value',
splitline_opts=opts.SplitAreaOpts(is_show=True),#显示分隔线
)
)
)
line.render()
面积图
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,Radar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
line=(
Line()
.add_xaxis(Faker.week)
.add_yaxis('',y_axis=[1,23,34,56,34,78,33],
areastyle_opts=opts.AreaStyleOpts(opacity=0.5),#面积图
)
.set_global_opts(
title_opts=opts.TitleOpts(
title='面积图'
),
tooltip_opts=opts.TooltipOpts(trigger='axis'),#提示线
xaxis_opts=opts.AxisOpts(type_='category',boundary_gap=False),#boundary_gap:没有间隙
)
)
line.render()
堆叠面积图
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,Radar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
line=(
Line()
.add_xaxis(Faker.week)
.add_yaxis('广告',y_axis=[1,23,34,56,34,78,33],
areastyle_opts=opts.AreaStyleOpts(opacity=0.5),#面积图
stack='堆叠',
label_opts=opts.LabelOpts(is_show=False)
)
.add_yaxis('流量',y_axis=[1,23,34,56,34,78,33],
areastyle_opts=opts.AreaStyleOpts(opacity=0.5),#面积图
stack='堆叠',
label_opts=opts.LabelOpts(is_show=False)
)
.set_global_opts(
title_opts=opts.TitleOpts(
title='堆叠面积图'
),
tooltip_opts=opts.TooltipOpts(trigger='axis'),#提示线
xaxis_opts=opts.AxisOpts(type_='category',boundary_gap=False),#boundary_gap:没有间隙
)
)
line.render()
散点图
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,Radar,Scatter
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
data=[
[1,3],
[1,5],
[4,5],
[4,5.8],
[4.9,5.8],
[8,5.8],
[8.9,5.8],
[2,5.8],
]
data.sort(key=lambda x:x[0])
x_data=[i[0] for i in data]
y_data=[y[0] for y in data]
scatter=(
Scatter(
init_opts=opts.InitOpts(width='800px',height='400px')
)
.add_xaxis(xaxis_data=x_data)
.add_yaxis('',y_axis=y_data,symbol_size=20,label_opts=opts.LabelOpts(is_show=True))
.set_global_opts(
title_opts=opts.TitleOpts(
title='散点图'
),
xaxis_opts=opts.AxisOpts(
type_='value',
splitline_opts=opts.SplitLineOpts(is_show=True)
),
yaxis_opts=opts.AxisOpts(
type_='value',
splitline_opts=opts.SplitLineOpts(is_show=True)
)
)
)
scatter.render()
涟漪散点图
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,Radar,Scatter,EffectScatter
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
effectscatter=(
EffectScatter()
.add_xaxis(Faker.choose())
.add_yaxis('',Faker.values(),symbol=SymbolType.ARROW)
.set_global_opts(
title_opts=opts.TitleOpts(
title='涟漪散点图'
)
)
)
effectscatter.render()
热力图
import random
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,\
Radar,Scatter,EffectScatter,HeatMap
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
value=[[i,j,random.randint(0,50)]for i in range(24) for j in range(7)]
effectscatter=(
HeatMap()
.add_xaxis(Faker.clock)#clock:时钟
.add_yaxis('热力图',Faker.week,value,label_opts=opts.LabelOpts(is_show=True,position='inside'))
.set_global_opts(
title_opts=opts.TitleOpts(
title='热力图'
)
)
)
effectscatter.render()
日历图
import random,datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,\
Radar,Scatter,EffectScatter,HeatMap,Calendar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
begin=datetime.date(2024,1,1)
end=datetime.date(2024,12,31)
data=[[str(begin+datetime.timedelta(days=i)),random.randint(1000,25000)]
for i in range((end-begin).days)]
calendar=(
Calendar()
.add('',
data,
calendar_opts=opts.CalendarOpts(
range_='2024',
daylabel_opts=opts.CalendarDayLabelOpts(name_map='cn'),#中文
monthlabel_opts=opts.CalendarMonthLabelOpts(name_map='cn'),#中文
))
.set_global_opts(
title_opts=opts.TitleOpts(
title='日历图'
),
visualmap_opts=opts.VisualMapOpts(
max_=25000,
min_=1000,
orient='horizontal',#水平方向
is_piecewise=True,#显示方式
pos_left='100px',
pos_top='230px'
)
)
)
calendar.render()
箱型图
import random,datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,\
Radar,Scatter,EffectScatter,HeatMap,Calendar,Boxplot
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
v1=[[23,234,4353,23432,654,89,98798],[23,2342,435,2332,6534,89,9898]]
boxplot=Boxplot()
boxplot.add_xaxis(['demo1','demo2'])
boxplot.add_yaxis('A',boxplot.prepare_data(v1))
boxplot.render()
词云图
import random,datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,\
Radar,Scatter,EffectScatter,HeatMap,Calendar,Boxplot,WordCloud
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
data=[(i,random.randint(1,1000)) for i in Faker.choose() ]
wordcloud=(
WordCloud()
.add(
'热点分析',
data_pair=data,
word_size_range=[6,60],#字体大小范围
textstyle_opts=opts.TextStyleOpts(
font_family='cursive'#字体
)
)
.set_global_opts(
title_opts=opts.TitleOpts(title='词云图')
))
wordcloud.render()
漏斗图
import random,datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,\
Radar,Scatter,EffectScatter,HeatMap,Calendar,Boxplot,WordCloud,\
Funnel
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
data=[(i,random.randint(1,1000)) for i in Faker.choose() ]
funnel=(
Funnel()
.add(
'商品',
[list(i) for i in zip(Faker.choose(),Faker.values())],
gap=2,#间隙
tooltip_opts=opts.TooltipOpts(
trigger='item',
formatter='{a}<br/> {b}:{c}'
)
)
.set_global_opts(
title_opts=opts.TitleOpts(title='漏斗图')
))
funnel.render()
极坐标图
import random,datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,\
Radar,Scatter,EffectScatter,HeatMap,Calendar,Boxplot,WordCloud,\
Funnel,Polar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
polar=(
Polar()
.add_schema(
radiusaxis_opts=opts.RadiusAxisOpts(
data=Faker.week,
type_='category'
)
)
.add('商品A',[1,24,34,45,76,12,90],type_='bar',stack='abc')
.add('商品B',[1,30,34,45,76,13,90],type_='bar',stack='abc')
.add('商品C',[1,24,34,56,76,12,90],type_='bar',stack='abc')
.set_global_opts(
title_opts=opts.TitleOpts(title='极坐标图+堆叠柱形图')
))
polar.render()
import random,datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,\
Radar,Scatter,EffectScatter,HeatMap,Calendar,Boxplot,WordCloud,\
Funnel,Polar
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
data=[(i,random.randint(1,100))for i in range(101)]
polar=(
Polar()
.add(
'极坐标',
data,
type_='scatter',#散点图
label_opts=opts.LabelOpts(is_show=False)
)
.set_global_opts(
title_opts=opts.TitleOpts(title='极坐标图')
))
polar.render()
水球图
import random,datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar,Line,Pie,PictorialBar,\
Radar,Scatter,EffectScatter,HeatMap,Calendar,Boxplot,WordCloud,\
Funnel,Polar,Liquid
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType,SymbolType
from pyecharts.commons.utils import JsCode
liquid=(
Liquid()
.add('',[0.2,0.7])
.set_global_opts(
title_opts=opts.TitleOpts(title='水球图')
))
liquid.render()
桑基图
import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
Funnel, Polar, Liquid, Sankey
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode
nodes = [
{'name': 'c1'},
{'name': 'c2'},
{'name': 'c3'},
{'name': 'c4'},
{'name': 'c5'},
{'name': 'c6'},
{'name': 'c7'},
]
links=[
{'source':'c1','target':'c2','value':10},
{'source':'c2','target':'c3','value':20},
{'source':'c3','target':'c4','value':30},
{'source':'c4','target':'c5','value':40},
{'source':'c6','target':'c7','value':50},
]
sankey = (
Sankey()
.add('',
nodes,#所有节点
links,#节点之间的链接关系
linestyle_opt=opts.LineStyleOpts(
opacity=0.2,#透明度
curve=0.6,#曲线幅度0~1
color='red'
)
)
.set_global_opts(
title_opts=opts.TitleOpts(title='桑基图')
))
sankey.render()
旭日图
import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
Funnel, Polar, Liquid, Sankey,Sunburst
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode
data=[
opts.SunburstItem(
name='Grandpa',
children=[
opts.SunburstItem(
name='Uncle',
value=15,
children=[
opts.SunburstItem(name='Jack',value=2),
opts.SunburstItem(
name='Mary',
value=5,
children=[opts.SunburstItem(name='Jackson',value=2)]
)
]
)
],
),
opts.SunburstItem(
name='Father',
value=10,
children=[
opts.SunburstItem(name='Me',value=5),
opts.SunburstItem(name='Peter',value=1),
]
),
opts.SunburstItem(
name='Nancy',
children=[
opts.SunburstItem(
name='Nike',
children=[
opts.SunburstItem(name='Betty',value=1),
opts.SunburstItem(name='Jenny',value=2)
]
)
]
)
]
sunburst = (
Sunburst(
init_opts=opts.InitOpts(
width='800px',height='400px'
)
)
.add('',data_pair=data)
.set_global_opts(
title_opts=opts.TitleOpts(title='旭日图')
))
sunburst.render()
仪表盘
import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
Funnel, Polar, Liquid, Sankey,Sunburst,Gauge
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode
gauge = (
Gauge()
.add('指标',data_pair=[('','66')],radius='60%')
.set_global_opts(
title_opts=opts.TitleOpts(title='仪表盘')
))
gauge.render()
树图
import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
Funnel, Polar, Liquid, Sankey,Sunburst,Gauge,Tree
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode
data=[
{
'children':[
{'name':'叔叔'},
{'children':[{'name':'小华'}],'name':'伯伯'},
{'name':'父亲',
'children':[
{'name':'我'},
{'name':'姐姐'}
]}
],
'name':'祖父母'
}
]
tree = (
Tree()
.add('指标',data)
.set_global_opts(
title_opts=opts.TitleOpts(title='树图')
))
tree.render()
矩形树图
import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
Funnel, Polar, Liquid, Sankey,Sunburst,Gauge,Tree,TreeMap
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode
data=[
{'value':40,'name':'伯伯'},
{
'value':180,
'name':'父亲',
'children':[
{'value':70,
'name':'我',
'children':[
{'value':12,'name':'大儿子'},
{'value':28,'name':'二儿子'},
{'value':18,'name':'三女儿'},
{'value':16,'name':'四女儿'},
]}
]
}
]
treemap = (
TreeMap()
.add('',data)
.set_global_opts(
title_opts=opts.TitleOpts(title='矩形树图')
))
treemap.render()
关系图
import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
Funnel, Polar, Liquid, Sankey,Sunburst,Gauge,Tree,TreeMap,Graph
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode
nodes=[
{'name':'node1','symbolSize':10},
{'name':'node2','symbolSize':20},
{'name':'node3','symbolSize':30},
{'name':'node4','symbolSize':40},
{'name':'node5','symbolSize':50},
{'name':'node6','symbolSize':60},
{'name':'node7','symbolSize':10},
]
links=[]
for i in nodes:
for j in nodes:
links.append({'source':i.get('name'),'target':j.get('name')})
graph = (
Graph()
.add('',
nodes,
links,
repulsion=8000#排斥力:越大分的越开
)
.set_global_opts(
title_opts=opts.TitleOpts(title='关系图')
))
graph.render()
import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
Funnel, Polar, Liquid, Sankey,Sunburst,Gauge,Tree,TreeMap,Graph
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode
nodes_data=[
opts.GraphNode(name='node1',symbol_size=10),
opts.GraphNode(name='node2',symbol_size=20),
opts.GraphNode(name='node3',symbol_size=30),
opts.GraphNode(name='node4',symbol_size=40),
opts.GraphNode(name='node5',symbol_size=50),
opts.GraphNode(name='node6',symbol_size=60),
]
link_data=[
opts.GraphLink(source='node1',target='node2',value=1),
opts.GraphLink(source='node2',target='node3',value=2),
opts.GraphLink(source='node3',target='node4',value=3),
opts.GraphLink(source='node4',target='node5',value=4),
opts.GraphLink(source='node5',target='node6',value=5),
opts.GraphLink(source='node6',target='node1',value=6),
]
graph = (
Graph()
.add('',
nodes_data,
link_data,
repulsion=3000,#排斥力:越大分的越开
edge_label=opts.LabelOpts(
is_show=True,
position='middle',
formatter='{b}的数据:{c}'
)
)
.set_global_opts(
title_opts=opts.TitleOpts(title='关系图2')
))
graph.render()
K线图
import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, Kline
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode
data = [
# 开盘价,收盘价,最低价,最高价
[2320.20, 2320.26, 2287, 2363],
[2320.21, 2320.26, 2287, 2363],
[2320.22, 2320.27, 2284, 2362],
[2320.20, 2320.27, 2283, 2363],
[2320.23, 2320.20, 2282, 2364],
[2320.24, 2320.22, 2281, 2365],
]
kline = (
Kline()
.add_xaxis(['2024/1/{}'.format(i + 1) for i in range(31)])
.add_yaxis('K线图', data)
.set_global_opts(
title_opts=opts.TitleOpts(title='K线图')
))
kline.render()
地图
import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
Kline,Map
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode
provinces=[p+"市" if p=='上海'else p+'省' for p in Faker.provinces]
map = (
Map()
.add(
'',
[list(i) for i in zip(provinces,Faker.values())],
'china'#地图类型
)
.set_global_opts(
title_opts=opts.TitleOpts(title='地图'),
visualmap_opts=opts.VisualMapOpts(max_=200)
))
map.render()
import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
Kline,Map
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode
map = (
Map()
.add(
'',
[list(i) for i in zip(Faker.guangdong_city,Faker.values())],
'广东'#地图类型
)
.set_global_opts(
title_opts=opts.TitleOpts(title='广东地图'),
visualmap_opts=opts.VisualMapOpts(max_=200,is_piecewise=True)
))
map.render()
import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
Kline,Map
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode
map = (
Map()
.add(
'',
[list(i) for i in zip(Faker.country,Faker.values())],
'world'#地图类型
)
.set_global_opts(
title_opts=opts.TitleOpts(title='世界地图'),
visualmap_opts=opts.VisualMapOpts(max_=200,is_piecewise=True)
))
map.render()
import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
Kline,Map,MapGlobe
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType
from pyecharts.commons.utils import JsCode
POPULATION=[
['china',1322343243],
['India',132343243],
['Indonesia',1322343243],
['Mexico',1022343243],
['Japan',122343243],
['Ethiopia',1122343243],
]
data=[i for _,i in POPULATION]
low,high=min(data),max(data)
mapglobe = (
MapGlobe()
.add_schema()
.add(
maptype='world',
series_name='世界人口',
data_pair=POPULATION,#人口数据
is_map_symbol_show=False,#地图中各个国家的符号是否显示
label_opts=opts.LabelOpts(is_show=True)#显示国家名称
)
.set_global_opts(
title_opts=opts.TitleOpts(title='地球'),
visualmap_opts=opts.VisualMapOpts(max_=high,min_=low,range_text=['Max','Min'],is_piecewise=True)
))
mapglobe.render()
地理坐标图
import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
Kline, Map, MapGlobe, Geo
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType, ChartType
from pyecharts.commons.utils import JsCode
geo = (
Geo()
.add_schema(maptype='china')
.add('',
[('广州', 50), ('北京', 60), ('杭州', 70), ('重庆', 80)],
type_=ChartType.EFFECT_SCATTER, color='red')
.add(
'',
[('广州', '上海'), ('广州', '北京'), ('广州', '杭州'), ('广州', '重庆')],
type_=ChartType.LINES, # 线
#箭头
effect_opts=opts.EffectOpts(
symbol_size=6,
symbol=SymbolType.ARROW,
color='blue'
),
linestyle_opts=opts.LineStyleOpts(curve=0.2)
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title='地理坐标图'),
))
geo.render()
import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
Kline, Map, MapGlobe, Geo
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType, ChartType
from pyecharts.commons.utils import JsCode
geo = (
Geo()
.add_schema(maptype='china')
.add('geo',
[list(i) for i in zip(Faker.provinces,Faker.values())],
type_=ChartType.HEATMAP#热力图
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title='地理坐标图+热力图'),
visualmap_opts=opts.VisualMapOpts()
))
geo.render()
import random, datetime
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
Kline, Map, MapGlobe, Geo
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType, ChartType
from pyecharts.commons.utils import JsCode
geo = (
Geo()
.add_schema(maptype='china')
.add('geo',
[list(i) for i in zip(Faker.provinces,Faker.values())],
type_=ChartType.EFFECT_SCATTER#涟漪散点图
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title='地理坐标图+涟漪散点图'),
visualmap_opts=opts.VisualMapOpts()
))
geo.render()
3D拆线图
import random, datetime,math
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
Kline, Map, MapGlobe, Geo,Line3D
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType, ChartType
from pyecharts.commons.utils import JsCode
data=[]
for i in range(0,25000):
_i=i/1000
x=(1+0.25*math.cos(75*_i))*math.cos(_i)
y=(1+0.25*math.cos(75*_i))*math.sin(_i)
z=_i+2.0*math.sin(75*_i)
data.append([x,y,z])
line3d = (
Line3D()
.add('',
data,
xaxis3d_opts=opts.Axis3DOpts(Faker.clock,type_='value'),
yaxis3d_opts=opts.Axis3DOpts(Faker.week,type_='value'),
grid3d_opts=opts.Grid3DOpts(
width=100,
depth=100,
rotate_speed=150,
is_rotate=True
)
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title='3D拆线图'),
visualmap_opts=opts.VisualMapOpts(
min_=0,
max_=30,
range_color=Faker.visual_color
)
))
line3d.render()
3D柱状图
import random, datetime,math
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
Kline, Map, MapGlobe, Geo,Line3D,Bar3D
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType, ChartType
from pyecharts.commons.utils import JsCode
data=[(i,j,random.randint(0,12)) for i in range(6) for j in range(24)]
bar3d = (
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,type_='category'),
zaxis3d_opts=opts.Axis3DOpts(type_='value')
)
.set_global_opts(
title_opts=opts.TitleOpts(title='3D柱状图'),
visualmap_opts=opts.VisualMapOpts(
max_=15,
)
))
bar3d.render()
3D堆叠柱状图
import random, datetime, math
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
Kline, Map, MapGlobe, Geo, Line3D, Bar3D
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType, ChartType
from pyecharts.commons.utils import JsCode
x_data = y_data = list(range(10))
def generate_data():
data = []
for i in range(10):
for j in range(10):
value = random.randint(0, 9)
data.append([i, j, value * 2 + 4])
return data
bar3d = Bar3D()
for _ in range(10):
bar3d.add('',
generate_data(),
xaxis3d_opts=opts.Axis3DOpts(x_data, type_='value'),
yaxis3d_opts=opts.Axis3DOpts(y_data, type_='value'),
zaxis3d_opts=opts.Axis3DOpts(type_='value'),
shading='lambert' # 清晰
)
bar3d.set_global_opts(
title_opts=opts.TitleOpts(title='3D堆叠柱状图'),
visualmap_opts=opts.VisualMapOpts(
max_=15,
)
)
bar3d.set_series_opts(stack='abc')
bar3d.render()
时间轮播图
import random, datetime, math
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
Kline, Map, MapGlobe, Geo, Line3D, Bar3D,Timeline
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType, ChartType
from pyecharts.commons.utils import JsCode
x=Faker.choose()
tl=Timeline()
for i in range(2020,2025):
bar=(
Bar()
.add_xaxis(x)
.add_yaxis('A',Faker.values())
.add_yaxis('B',Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title='时间轮播图')
)
)
tl.add(bar,'{}年'.format(i))
tl.render()
并行布局
import random, datetime, math
from pyecharts.faker import Faker
from pyecharts.charts import Bar, Line, Pie, PictorialBar, \
Radar, Scatter, EffectScatter, HeatMap, Calendar, Boxplot, WordCloud, \
Funnel, Polar, Liquid, Sankey, Sunburst, Gauge, Tree, TreeMap, Graph, \
Kline, Map, MapGlobe, Geo, Line3D, Bar3D,Timeline,Grid
from pyecharts import options as opts
from pyecharts.globals import ThemeType, RenderType, SymbolType, ChartType
from pyecharts.commons.utils import JsCode
line=(
Line()
.add_xaxis(Faker.choose())
.add_yaxis('A',Faker.values())
.add_yaxis('B',Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title='拆线图',pos_left='5%'),
legend_opts=opts.LegendOpts(pos_left='20%')
)
)
scatter=(
Scatter()
.add_xaxis(Faker.choose())
.add_yaxis('C',Faker.values())
.add_yaxis('D',Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title='散点图',pos_right='40%'),
legend_opts=opts.LegendOpts(pos_right='20%')
)
)
grid=(
Grid()
.add(line,grid_opts=opts.GridOpts(pos_right='55%'))
.add(scatter,grid_opts=opts.GridOpts(pos_left='55%'))
)
grid.render()
标签:pyecharts,title,Faker,add,可视化,import,数据,opts
From: https://www.cnblogs.com/ytmjcmn/p/18113008