Day13-pyecharts的使用
1. 柱状图
from pyecharts.charts import Bar
from pyecharts import options
bar = Bar()
bar.add_xaxis(['1季度', '2季度', '3季度', '4季度'])
bar.add_yaxis('销售额', [890, 1023, 509, 1500],
color='red',
bar_max_width=100,
bar_min_width=40
)
bar.add_yaxis('销量', [208, 300, 301, 400])
bar.set_global_opts(
title_opts=options.TitleOpts(
title='全年xxx手机的销售信息',
title_link='https://www.baidu.com',
subtitle='销售额和销量信息',
pos_right='350',
text_align='center'
),
legend_opts=options.LegendOpts(
is_show=False,
pos_right='90',
item_width=20,
item_height=10
),
toolbox_opts=options.ToolboxOpts(
is_show=True
),
datazoom_opts=options.DataZoomOpts(
is_show=True
),
xaxis_opts=options.AxisOpts(
name='时间'
),
yaxis_opts=options.AxisOpts(
name='销售额(万元)/销量'
)
)
bar.set_series_opts(
label_opts=options.LabelOpts(
is_show=True,
position='top',
),
markpoint_opts=options.MarkPointOpts(
data=[options.MarkPointItem(name='最大值', type_='max'), options.MarkPointItem(name='最小值', type_='min')]
)
)
bar.render('files/01柱状图.html')
2. 折线图
from pyecharts.charts import Line
from pyecharts import options
line = Line()
line.add_xaxis(['1季度', '2季度', '3季度', '4季度'])
line.add_yaxis('服装', [189, 340, 299, 412], is_smooth=True)
line.add_yaxis('电子产品', [89, 109, 77, 56])
line.set_global_opts(
title_opts=options.TitleOpts(
title='xxx商城各大商品的销售情况',
pos_left='350'
),
legend_opts=options.LegendOpts(
pos_right=80
),
xaxis_opts=options.AxisOpts(
name='时间'
),
yaxis_opts=options.AxisOpts(
name='销售额(万元)'
)
)
line.set_series_opts(
label_opts=options.LabelOpts(
is_show=True
),
markline_opts=options.MarkLineOpts(
data=[options.MarkLineItem(type_='average', name='平均销售额')]
)
)
line.render('files/02折线图.html')
3. 饼图
from pyecharts.charts import Pie
from pyecharts import options
pie = Pie()
pie.add('手机销量',
[('Apple', 890), ('华为', 1002), ('小米', 409), ('OPPO', 102), ('魅族', 98)],
radius=(50, 100),
rosetype='radius'
)
pie.set_global_opts(
legend_opts=options.LegendOpts(
is_show=False
),
title_opts=options.TitleOpts(
title='xxx商城6月手机销售情况',
pos_left='350'
)
)
pie.set_series_opts(
label_opts=options.LabelOpts(
formatter='{b}({d}%)'
)
)
pie.render('files/03饼图.html')
4. 地图
rom pyecharts.charts import Map
from pyecharts import options
map1 = Map()
map1.add(
'房价',
[('四川省', 8000), ('重庆市', 7923), ('河南省', 5672), ('北京市', 53994),
('上海市', 47789), ('新疆维吾尔自治区', 3982), ('广东省', 6000), ('黑龙江省', 4632)],
zoom=1.5,
min_scale_limit=1,
max_scale_limit=2
)
map1.set_global_opts(
visualmap_opts=options.VisualMapOpts(
is_show=True,
min_=3000,
max_=65000,
is_piecewise=True,
pieces=[
{'min': 3000, 'max': 4999},
{'min': 5000, 'max': 9999},
{'min': 10000, 'max': 29999},
{'min': 30000, 'max': 59999},
{'min': 60000, 'max': 65000}
]
),
legend_opts=options.LegendOpts(
is_show=False
),
title_opts=options.TitleOpts(
title='中国各省平均房价',
pos_right='400'
)
)
map1.render('files/04中国地图.html')
map2 = Map()
map2.add('房价',
[('成都市', 12000), ('乐山市', 7000)],
maptype='四川')
map2.render('files/05四川地图.html')
map3 = Map()
map3.add('房价', [('China', 2000)], maptype='world')
map3.set_series_opts(
label_opts=options.LabelOpts(
is_show=False
)
)
map3.render('files/06世界地图.html')
map4 = Map()
map4.add('房价', [('青羊区', 17000)], maptype='成都')
map4.render('files/07成都地图.html')
5. 词云
from pyecharts.charts import WordCloud
from pyecharts import options
wc = WordCloud()
wc.add(
'游戏',
[('王者荣耀', 82933), ('英雄联盟', 109283), ('阴阳师', 7282),
('原神', 1928), ('开心消消乐', 8192), ('和平精英', 76788),
('飞机大战', 109), ('蛋仔排队', 678), ('烈梦者', 302),
('红色警戒', 19), ('魔兽', 68922), ('刀塔', 3192), ('QQ农场', 87),
('植物大战僵尸', 67), ('地平线', 33), ('神庙逃亡',10), ('节奏大师', 2033),
('找你妹', 9), ('大富翁', 300), ('保卫萝卜', 9012)],
shape='star'
)
wc.render('files/08词云.html')