pyecharts生成各种图表
一、柱状图
from pyecharts.charts import Bar
from pyecharts import options
province = ['湖北', '北京', '新疆', '山东', '安徽', '云南', '上海']
data = [100000, 50000, 55000, 51000, 60000, 43210, 50334]
data2 = [12000, 5000, 3000, 5400, 2000, 3000, 10000]
bar = Bar()
bar.add_xaxis(province)
bar.add_yaxis('确诊人数', data)
bar.add_yaxis('死亡人数', data2)
bar.set_global_opts(
title_opts=options.TitleOpts(title='全国重点城市疫情统计', subtitle='确诊人数和死亡人数')
, toolbox_opts=options.ToolboxOpts()
)
bar.set_series_opts(
label_opts=options.LabelOpts(is_show=False),
markpoint_opts=options.MarkPointOpts(
data=[
options.MarkPointItem(type_='min', name='最小值'),
options.MarkPointItem(type_='max', name='最大值')
]
)
)
bar.render('templates/柱状图.html')
二、饼状图
from pyecharts.charts import Pie
from pyecharts import options
data = [('小米', 200), ('华为', 300), ('oppo', 250), ('vivo', 400), ('1+', 100)]
pie = Pie()
pie.add(
data_pair=data,
series_name='国产手机销量',
radius=[30,200],
rosetype='radius',
center=[300,300]
)
pie.set_global_opts(
title_opts=options.TitleOpts(title='国产手机销量')
)
pie.set_series_opts(
label_opts=options.LabelOpts(formatter='{b}:{d}%')
)
pie.render('templates/饼图.html')
饼状图简化版
from pyecharts.charts import Pie
from pyecharts import options
pie=(
Pie()
.add(
series_name='国产手机',
data_pair=[('小米', 200), ('华为', 300), ('oppo', 250), ('vivo', 400), ('1+', 100)],
rosetype='radius',
radius=['1%','90%']
)
.set_global_opts(title_opts=options.TitleOpts('国产手机销量'))
.set_series_opts(
label_opts=options.LabelOpts(formatter='{b}:{d}%')
)
)
pie.render('templates/饼图2.html')
三、折线图
from pyecharts.charts import Line
from pyecharts import options
cuntry = ['美国', '巴西', '印度', '俄罗斯']
data1 = [10000000, 600000, 5000000, 300000]
line = Line()
line.add_xaxis(cuntry)
line.add_yaxis(series_name='感染人数', y_axis=data1, is_smooth=True)
line.set_global_opts(
title_opts=options.TitleOpts(title='世界严重感染国家'),
toolbox_opts=options.ToolboxOpts()
)
line.set_series_opts(
markline_opts=options.MarkLineOpts(
data=[
options.MarkLineItem(name='平均值', type_='average'),
]
)
)
line.render('templates/折线图.html')
四、组合图
from pyecharts.charts import Line, Bar, Grid
from pyecharts import options
cate = ['重庆', '黑龙江', '香港', '台湾', '上海']
data = [579, 925, 1035, 427, 640]
data2 = [470, 487, 699, 253, 533]
bar = Bar()
line = Line()
bar.add_xaxis(cate)
bar.add_yaxis('确诊人数', data)
bar.add_yaxis('治愈人数', data2)
bar.set_global_opts(
title_opts=options.TitleOpts(title='疫情信息')
)
line.add_xaxis(cate)
line.add_yaxis('确诊人数', data)
line.add_yaxis('治愈人数', data2)
grid = Grid(init_opts=options.InitOpts(width='1200px', height='800px'))
grid.add(bar, grid_opts=options.GridOpts(
pos_top='50',
pos_left='60',
width='550',
height='300'
))
grid.add(line, grid_opts=options.GridOpts(
pos_top='50',
pos_right='0',
width='550',
height='300'
))
grid.render('templates/组合.html')
五、地图
from pyecharts import options
from pyecharts.charts import Map
data = [('湖北', 68149), ('内蒙古', 333), ('四川', 808), ('上海', 1350), ('陕西', 501),('台湾',12000)]
map = Map()
map.add(series_name='国内主要城市感染人数', data_pair=data)
map.set_global_opts(
title_opts=options.TitleOpts(title='感染人数')
, legend_opts=options.LegendOpts(is_show=True)
, visualmap_opts=options.VisualMapOpts(
max_=70000,
min_=0,
is_piecewise=True,
pieces=[
{'min': 0, 'max': 100, 'label': '0-100', 'color': 'pink'},
{'min': 100, 'max': 200, 'label': '100-200', 'color': 'yellow'},
{'min': 200, 'max': 1000, 'label': '200-1000', 'color': 'blue'},
{'min': 1000, 'max': 2000, 'label': '1000-2000', 'color': 'orange'},
{'min': 1000, 'label': '>=2000', 'color': 'red'}
]
)
)
map.render('templates/地图.html')