# Faker
from pyecharts.faker import Faker
# 随机产生同属性的7个名词
Faker.choose()
Faker.values() # 随机的7个数
from pyecharts.charts import Pie
import pyecharts.options as opts
Faker.choose()
Faker.values()
list(zip(Faker.choose(),Faker.values()))
[list(x) for x in zip(Faker.choose(),Faker.values())]
c = Pie()
c.add('', [list(x) for x in zip(Faker.choose(),Faker.values())])
c.render_notebook()
c = (
Pie()
.add('', [list(x) for x 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}'))
)
c.render_notebook()
c = (
Pie()
.add(
'',
[list(i) for i in zip(v, Faker.values())],
radius=['30%','75%'],
center=['25%','50%'],
rosetype='radius',
label_opts=opts.LabelOpts(is_show=False) # 不显示标签
)
.add(
'',
[list(i) for i in zip(v, Faker.values())],
radius=['20%','55%'],
center=['75%','50%'],
rosetype='area',
label_opts=opts.LabelOpts(is_show=True) # 显示标签
)
.set_global_opts(title_opts=opts.TitleOpts(title='玫瑰图'))
)
c.render_notebook()
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.faker import Faker
from pyecharts.globals import ThemeType
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis('商家A', Faker.values())
.add_yaxis('商家B', Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(
title='柱状图',
subtitle='副标题'
)
)
)
c.render_notebook()
c = (
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='柱状图',
subtitle='副标题'
)
)
)
c.render_notebook()
from pyecharts.commons.utils import JsCode
c = (
Bar(
init_opts=opts.InitOpts(
bg_color={
'image':JsCode('img'),
'repeat':'no-repeat'
},
theme=ThemeType.CHALK,
)
)
.add_xaxis(Faker.choose())
.add_yaxis('商家A', Faker.values())
.add_yaxis('商家B', Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(
title='柱状图',
subtitle='副标题'
)
)
)
# 添加js代码
c.add_js_funcs(
"""
var img = new Image()
img.src = 'https://img.zcool.cn/community/[email protected]'
"""
)
c.render_notebook()
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis('商家A', Faker.values(),stack='abc')
.add_yaxis('商家B', Faker.values(),stack='abc')
.set_series_opts(
label_opts=opts.LabelOpts(is_show=False)
)
.set_global_opts(
title_opts=opts.TitleOpts(
title='堆叠柱状图',
subtitle='副标题'
),
# 旋转
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)),
# 缩放
datazoom_opts=[
opts.DataZoomOpts(), # X轴拖拉缩放
opts.DataZoomOpts(type_='inside') # 鼠标滚轮缩放
]
)
)
c.render_notebook()
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis('商家A', Faker.values())
.add_yaxis('商家B', Faker.values())
# 翻转轴,条形图
.reversal_axis()
.set_series_opts(
label_opts=opts.LabelOpts(position='right')
)
.set_global_opts(
title_opts=opts.TitleOpts(
title='堆叠柱状图',
),
)
)
c.render_notebook()
不同系列柱子之间的距离
c = (
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='不同系列柱间距离')
)
)
c.render_notebook()
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis('商家A', Faker.values(), category_gap=0)
.set_global_opts(
title_opts=opts.TitleOpts(title='直方图')
)
)
c.render_notebook()
color_func="""
function(params){
if(params.value > 0 && params.value < 50){
return 'red';
}
else if (params.value > 50 && params.value < 100){
return 'green';
}
return 'blue';
}
"""
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis(
'商家A',
Faker.values(),
itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_func))
)
.add_yaxis(
'商家B',
Faker.values(),
itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_func))
)
.add_yaxis(
'商家C',
Faker.values(),
itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_func))
)
)
c.render_notebook()
from pyecharts import options as opts
from pyecharts.charts import PictorialBar
from pyecharts.globals import SymbolType
location = ['山西','四川','西藏','北京','上海','内蒙古','云南','黑龙江','广东','福建']
values = [13, 42, 67, 81, 86, 94, 166, 220, 249, 262]
c = (
PictorialBar()
.add_xaxis(location)
.add_yaxis(
'',
values,
label_opts=opts.LabelOpts(is_show=False),
symbol=SymbolType.ROUND_RECT, # 符号类型 ARROW、DIAMOND、RECT、ROUND_RECT、TRIANGLE
symbol_repeat='fixed', # 重复方式
symbol_size=16, # 符号大小
is_symbol_clip=True, # 符号裁剪
)
.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.AxisTickOpts(is_show=False), # 不显示y轴刻度
axisline_opts=opts.AxisLineOpts(is_show=False), # 不显示y轴的线
)
)
)
c.render_notebook()
from pyecharts.charts import Radar
import pyecharts.options as opts
v1 = [[4300, 10000, 28000, 35000, 50000, 19000]]
v2 = [[5000, 14000, 28000, 31000, 42000, 21000]]
c = (
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),
]
)
.add('数据1',v1,color='blue')
.add('数据2',v2)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
)
c.render_notebook()
from pyecharts.charts import Line
import pyecharts.options as opts
from pyecharts.faker import Faker
c = (
Line(init_opts=opts.InitOpts(width='600px',height='300px'))
.add_xaxis(Faker.choose())
.add_yaxis('商家A',Faker.values())
.add_yaxis('商家B',Faker.values(),is_smooth=True) # 平滑
.set_global_opts(
title_opts=opts.TitleOpts(title='折线图'),
# 提示线
tooltip_opts=opts.TooltipOpts(trigger='axis')
)
)
c.render_notebook()
c = (
Line(
init_opts=opts.InitOpts(width='600px',height='400px')
)
.add_xaxis(xaxis_data=Faker.week)
.add_yaxis(
series_name='',
y_axis=[120, 200, 150, 80, 70, 110, 130],
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_width=2,border_color='yellow',color='blue'
),
# 标注点
markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(type_='max'), # 最大值
opts.MarkPointItem(type_='min'), # 最小值
]
),
# 标注线
markline_opts=opts.MarkLineOpts(
data=[
opts.MarkLineItem(type_='average'), # 平均值
]
),
)
.set_global_opts(
yaxis_opts=opts.AxisOpts(
type_='value',
splitline_opts=opts.SplitLineOpts(is_show=True),
),
tooltip_opts=opts.TooltipOpts(trigger='axis')
)
)
c.render_notebook()
from pyecharts.charts import Line
import pyecharts.options as opts
x_data = ['周一','周二','周三','周四','周五','周六','周日']
y_data = [820,932,901,934,1290,1330,1320]
c = (
Line()
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
'',
y_axis=[120,130,100,140,90,200,150],
areastyle_opts=opts.AreaStyleOpts(opacity=0.5), # 面积图
)
.set_global_opts(
title_opts=opts.TitleOpts(title='面积图'),
tooltip_opts=opts.TooltipOpts(trigger='axis'),
# boundary_gap=False:没有间隙
xaxis_opts=opts.AxisOpts(type_='category',boundary_gap=False)
)
)
c.render_notebook()
c = (
Line()
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
'广告',
stack='堆叠',
y_axis=[120,40,100,30,50,60,150],
areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
label_opts=opts.LabelOpts(is_show=False)
)
.add_yaxis(
'销售',
stack='堆叠',
y_axis=[120,40,100,30,50,60,150],
areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
label_opts=opts.LabelOpts(is_show=False)
)
.add_yaxis(
'浏量',
stack='堆叠',
y_axis=[120,40,100,30,50,60,150],
areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
label_opts=opts.LabelOpts(is_show=False)
)
.add_yaxis(
'浏览',
stack='堆叠',
y_axis=[120,40,100,30,50,60,150],
areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
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)
)
)
c.render_notebook()
import pyecharts.options as opts
from pyecharts.charts import Scatter
from pyecharts.faker import Faker
data = [
[10.0,8.04],
[8.0,6.95],
[13.0,7.58],
[9.0,8.81],
[11.0,8.33],
[14.0,9.96],
[6.0,7.24],
[4.0,4.26],
[12.0,10.84],
[7.0,4.82],
[5.0,5.68],
]
# 把data中第一列数据排序
data.sort(key=lambda x:x[0])
x_data = [d[0] for d in data]
y_data = [d[1] for d in data]
display(x_data,y_data)
c = (
Scatter(
init_opts=opts.InitOpts(width='800px',height='500px')
)
.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(
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)
),
)
)
c.render_notebook()
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='涟漪散点图'),
xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),
yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True))
)
)
c.render_notebook()
from pyecharts import options as opts
from pyecharts.charts import HeatMap
from pyecharts.faker import Faker
import random
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(
'热力图',
Faker.week,
value,
label_opts=opts.LabelOpts(is_show=True,position='inside')
)
.set_global_opts(
title_opts=opts.TitleOpts(title='热力图')
)
)
c.render_notebook()
from pyecharts import options as opts
from pyecharts.charts import Calendar
import datetime
import random
# 2023年
begin = datetime.date(2023,1,1)
end = datetime.date(2023,12,31)
data = [ [str(begin + datetime.timedelta(days=i)),random.randint(1000,25000)] for i in range((end-begin).days+1)]
c = (
Calendar()
.add(
'',
data,
calendar_opts=opts.CalendarOpts(
range_='2023',
daylabel_opts=opts.CalendarDayLabelOpts(name_map='cn'), # 中文
monthlabel_opts=opts.CalendarMonthLabelOpts(name_map='cn'), # 中文
)
)
.set_global_opts(
title_opts=opts.TitleOpts(title='2023年日历图'),
visualmap_opts=opts.VisualMapOpts(
max_=25000,
min_=1000,
orient='horizontal', # 水平方向显示
is_piecewise=True, # 显示方式
pos_left='100px',
pos_top='230px',
)
)
)
c.render_notebook()