代码
from pyecharts.charts import Bar,Line,Grid
from pyecharts import options as opts
x = ['鸡','鸭','鱼','鹅','猪',"衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
y1 = [16,10,6,18,26,5, 20, 36, 10, 75, 90]
y2 = [12,18,12,8,20,5, 20, 36, 10, 75, 90]
bar = (
Bar(init_opts = opts.InitOpts(width='1550px',height='750px'))
.add_xaxis(x)
.add_yaxis('test1',y1)
.add_yaxis('test2',y2)
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='test',pos_left='center'),
#显示图例,右边
legend_opts=opts.LegendOpts(pos_right='1px'),
#横坐标
xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='动物名',name_location='middle',name_gap=40),
#纵坐标
yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40),
#滑动
datazoom_opts=opts.DataZoomOpts(is_show=True, type_="slider",range_start=20,range_end=60)) #图例位置
)
bar.render('test.html')
opts.InitOpts是设置图大小,set_global_opts设置全局变量。
from pyecharts.charts import Bar
from pyecharts import options as opts
x = ['鸡','鸭','鱼','鹅','猪',"衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
y1 = [16,10,6,18,26,5, 20, 36, 10, 75, 90]
y2 = [12,18,12,8,20,5, 20, 36, 10, 75, 90]
bar = (
Bar(init_opts = opts.InitOpts(width='1550px',height='750px'))
.add_xaxis(x)
.add_yaxis('test1',y1,stack="stack1")
.add_yaxis('test2',y2,stack="stack1")
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='test',pos_left='center'),
#显示图例,右边
legend_opts=opts.LegendOpts(pos_right='1px'),
#横坐标
xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='动物名',name_location='middle',name_gap=40),
#纵坐标
yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40),
#滑动
datazoom_opts=opts.DataZoomOpts(is_show=True, type_="slider",range_start=20,range_end=60)) #图例位置
)
bar.render('bar_stack.html')
主要在add_yaxis()中加入stack=“stack1”
from pyecharts.charts import Bar
from pyecharts import options as opts
x = ['鸡','鸭','鱼','鹅','猪',"衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
y1 = [16,10,6,18,26,5, 20, 36, 10, 75, 90]
y2 = [12,18,12,8,20,5, 20, 36, 10, 75, 90]
bar = (
Bar(init_opts = opts.InitOpts(width='1550px',height='750px'))
.add_xaxis(x)
.add_yaxis('test1',y1)
.add_yaxis('test2',y2)
#翻转
.reversal_axis()
#数字显示右边
.set_series_opts(label_opts=opts.LabelOpts(position="right"))
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='test',pos_left='center'),
#显示图例,右边
legend_opts=opts.LegendOpts(pos_right='1px'),
#横坐标
xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='sh',name_location='middle',name_gap=40),
#纵坐标
yaxis_opts=opts.AxisOpts( name='动物名',name_location='middle', name_gap=40),
))
bar.render('barh.html')
#翻转
.reversal_axis()
#数字显示右边
.set_series_opts(label_opts=opts.LabelOpts(position=“right”))
from pyecharts.charts import Bar,Line,Grid
from pyecharts import options as opts
x = ['鸡','鸭','鱼','鹅','猪',"衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
y1 = [16,10,6,18,26,5, 20, 36, 10, 75, 90]
y2 = [12,18,12,8,20,5, 20, 36, 10, 75, 90]
bar = (
Bar(init_opts = opts.InitOpts(width='1550px',height='750px'))
.add_xaxis(x)
.add_yaxis('test1',y1,category_gap=0)
#.add_yaxis('test2',y2,category_gap=0)
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='test',pos_left='center'),
#显示图例,右边
legend_opts=opts.LegendOpts(pos_right='1px'),
#横坐标
xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='动物名',name_location='middle',name_gap=40),
#纵坐标
yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40),
))
bar.render('zft.html')
将add_yaxis中加入category_gap=0或category_gap='0%'即可,category_gap是空值柱子的宽度。
折线图将Bar变为Line
from pyecharts.charts import Bar,Line,Grid
from pyecharts import options as opts
x = ['鸡','鸭','鱼','鹅','猪',"衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
y1 = [16,10,6,18,26,5, 20, 36, 10, 75, 90]
y2 = [12,18,12,8,20,5, 20, 36, 10, 75, 90]
line = (
Line(init_opts = opts.InitOpts(width='1550px',height='750px'))
.add_xaxis(x)
.add_yaxis('test1',y1)
.add_yaxis('test2',y2)
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='test',pos_left='center'),
#显示图例,右边
legend_opts=opts.LegendOpts(pos_right='1px'),
#横坐标
xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='动物名',name_location='middle',name_gap=40),
#纵坐标
yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40),
))
line.render('line.html')
from pyecharts.charts import Line
from pyecharts import options as opts
x_data = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
y_data = [820, 932, 901, 934, 1290, 1330, 1320]
l = (
Line(init_opts = opts.InitOpts(width='1550px',height='750px'))
.add_xaxis(x_data)
.add_yaxis("test", y_data,
#显示symbol
is_symbol_show=True,
#symbol大小
symbol_size = 10,
#显示symbol上的数字
label_opts=opts.LabelOpts(is_show=True))
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='test',pos_left='center'),
#显示图例,右边
legend_opts=opts.LegendOpts(pos_right='1px'),
#横坐标
xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":45},name='日期',name_location='middle',name_gap=40),
#纵坐标
yaxis_opts=opts.AxisOpts(
name='sh',name_location='middle', name_gap=40,
splitline_opts=opts.SplitLineOpts(is_show=True), #显示纵坐标横线
axisline_opts=opts.AxisLineOpts(is_show=False)) #不显示显示纵坐标竖线
)
#设置折线
.set_series_opts(
linestyle_opts=opts.LineStyleOpts(
# 是否显示
is_show= True,
#线宽。
width = 2,
#颜色
color='red'))
.render("ltest.html")
)
散点图将Bar变为Scatter
from pyecharts.charts import Bar,Line,Scatter
from pyecharts import options as opts
x = ['鸡','鸭','鱼','鹅','猪',"衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
y1 = [16,10,6,18,26,5, 20, 36, 10, 75, 90]
y2 = [12,18,12,8,20,5, 20, 36, 10, 75, 90]
scatter = (
Scatter(init_opts = opts.InitOpts(width='1550px',height='750px'))
.add_xaxis(x)
.add_yaxis('test1',y1)
.add_yaxis('test2',y2)
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='test',pos_left='center'),
#显示图例,右边
legend_opts=opts.LegendOpts(pos_right='1px'),
#横坐标
xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='动物名',name_location='middle',name_gap=40),
#纵坐标
yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40),
))
scatter.render('scatter.html')
from pyecharts import options as opts
from pyecharts.charts import Boxplot
x = ["expr1", "expr2"]
y1 = [
[850, 740, 900, 1070, 930, 850, 950, 980, 980, 880, 1000, 980],
[960, 940, 960, 940, 880, 800, 850, 880, 900, 840, 830, 790],
]
y2 = [
[890, 810, 810, 820, 800, 770, 760, 740, 750, 760, 910, 920],
[890, 840, 780, 810, 760, 810, 790, 810, 820, 850, 870, 870],
]
boxplot = (
Boxplot(init_opts = opts.InitOpts(width='1550px',height='750px'))
.add_xaxis(x)
.add_yaxis('test1',Boxplot.prepare_data(y1))
.add_yaxis('test2',Boxplot.prepare_data(y2))
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='test',pos_left='center'),
#显示图例,右边
legend_opts=opts.LegendOpts(pos_right='1px'),
#横坐标
xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='te',name_location='middle',name_gap=40),
#纵坐标
yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40),
))
boxplot.render('boxplot.html')
其中,Boxplot.prepare_data(y1),将y1中数据转换为嵌套的 [min, Q1, median (or Q2), Q3, max],其中Q1是下四分位数,Q3是上四分位数
from pyecharts.charts import Line
from pyecharts import options as opts
x = ['1月','2月','3月','4月','5月','6月']
a=[12,9,6,13,10,12]
b=[8,6,10,7,9,11]
c=[11,12,9,12,13,9]
area = (
Line()
.add_xaxis(x)
.add_yaxis('a', a, is_smooth=True, areastyle_opts=opts.AreaStyleOpts(opacity=0.5))
.add_yaxis('b', b, is_smooth=True, areastyle_opts=opts.AreaStyleOpts(opacity=0.5))
.add_yaxis('c', c, is_smooth=True, areastyle_opts=opts.AreaStyleOpts(opacity=0.5))
.set_global_opts(title_opts=opts.TitleOpts(title="面积图"))
)
area.render("面积图.html")
from pyecharts import options as opts
from pyecharts.charts import Gauge
gauge = (
Gauge(init_opts = opts.InitOpts(width='1550px',height='750px'))
.add("", [("完成率", 88.6)],detail_label_opts=opts.GaugeDetailOpts(offset_center=[0,"25%"]))
.set_global_opts(
title_opts=opts.TitleOpts(title="gg"),
)
)
gauge.render("gauge.html")
其中detail_label_opts=opts.GaugeDetailOpts(offset_center=[0,“25%”])是将轮盘内数据项标签移动
from pyecharts import options as opts
from pyecharts.charts import Pie
x = ['鸡','鸭','鱼','鹅','猪']
y1 = [16,10,6,18,26]
d = [(i,j) for i,j in zip(x,y1)]
pie = (
Pie(init_opts = opts.InitOpts(width='1550px',height='750px'))
.add("", d)
.set_global_opts(title_opts=opts.TitleOpts(title='test',pos_left='left'))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c} 占比{d}%"))
)
pie.render("pie1.html")
set_series_opts(label_opts=opts.LabelOpts(formatter=“{b}: {c} 占比{d}%”))中{b}是name,{c}数量,{d}%占比
from pyecharts import options as opts
from pyecharts.charts import Pie
x = ['鸡','鸭','鱼','鹅','猪']
y1 = [16,10,6,18,26]
d = [(i,j) for i,j in zip(x,y1)]
pie = (
Pie(init_opts = opts.InitOpts(width='1550px',height='750px'))
.add("",d,
radius=['50%','70%'],#第一个内半径,第二个外半径 圆环图的重要步骤!!!!
center=['50%','50%'], #第一个横坐标,第二个纵坐标
)
.set_global_opts(title_opts=opts.TitleOpts(title='test'))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c} 占比{d}%"))
)
pie.render("pie2.html")
radius=[‘50%’,‘70%’] 只要第一个不是0或0%,就说明有了内半径,就可以绘制圆环图了。
from pyecharts import options as opts
from pyecharts.charts import Pie
x = ['鸡','鸭','鱼','鹅','猪']
y1 = [16,10,6,18,26]
d = [(i,j) for i,j in zip(x,y1)]
pie = (
Pie(init_opts = opts.InitOpts(width='1550px',height='750px'))
.add("",d,
radius=['50%','70%'],#第一个内半径,第二个外半径 圆环图的重要步骤!!!!
center=['25%','50%'], #第一个横坐标,第二个纵坐标
rosetype='radius'
)
.add("",d,
radius=['50%','70%'],#第一个内半径,第二个外半径 圆环图的重要步骤!!!!
center=['75%','50%'], #第一个横坐标,第二个纵坐标
rosetype='area'
)
.set_global_opts(title_opts=opts.TitleOpts(title='test'))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}{d}%"))
)
pie.render("pie3.html")
center=[‘75%’,‘50%’], #第一个横坐标,第二个纵坐标,是饼图的中心坐标,rosetype两种形式。
需要用到层叠组件overlap
from pyecharts.charts import Bar,Line,Grid
from pyecharts import options as opts
x = ['鸡','鸭','鱼','鹅','猪',"衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
y1 = [16,10,6,18,26,5, 20, 36, 10, 75, 90]
y2 = [12,18,12,8,20,5, 20, 36, 10, 75, 90]
y3 = [i*2 for i in range(11)]
bar = (
Bar(init_opts = opts.InitOpts(width='1550px',height='750px'))
.add_xaxis(x)
.add_yaxis('test1',y1)
.add_yaxis('test2',y2)
#插入一个右边的坐标
.extend_axis(
yaxis=opts.AxisOpts(
axislabel_opts=opts.LabelOpts(formatter="{value} yuan"), interval=4
)
)
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='test',pos_left='center'),
#显示图例,右边
legend_opts=opts.LegendOpts(pos_right='1px'),
#横坐标
xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='动物名',name_location='middle',name_gap=40),
#纵坐标
yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40,axislabel_opts=opts.LabelOpts(formatter="{value} ge")),
#图例位置
))
#折线图
line = (
Line(init_opts = opts.InitOpts(width='1550px',height='750px'))
.add_xaxis(x)
.add_yaxis("jiage", y3, yaxis_index=1)) #yaxis_index=1是使用右边的纵坐标
bar.overlap(line)
bar.render('zh.html')
相当于画两个图,用overlap进行层叠
from pyecharts.charts import Bar,Line,Grid,Scatter
from pyecharts import options as opts
x = ['鸡','鸭','鱼','鹅','猪',"衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
y1 = [16,10,6,18,26,5, 20, 36, 10, 75, 90]
y2 = [12,18,12,8,20,5, 20, 36, 10, 75, 90]
bar = (
Bar()
.add_xaxis(x)
.add_yaxis('test1',y1)
.add_yaxis('test2',y2)
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='test1'),
#横坐标
xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='动物名',name_location='middle',name_gap=40),
#纵坐标
yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40),
))
line = (
Line()
.add_xaxis(x)
.add_yaxis('test1',y1)
.add_yaxis('test2',y2)
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='test2',pos_left='5%'),
#显示图例,右边
legend_opts=opts.LegendOpts(pos_right='1px'),
#横坐标
xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='动物名',name_location='middle',name_gap=40),
#纵坐标
yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40),
))
grid = (
Grid(init_opts = opts.InitOpts(width='1850px',height='500px'))
.add(line, grid_opts=opts.GridOpts(pos_left="55%"))
.add(bar, grid_opts=opts.GridOpts(pos_right="55%"))
)
grid.render('t.html')
from pyecharts.charts import Bar, Line, Grid, Scatter, Pie
from pyecharts import options as opts
x1 = ['鸡', '鸭', '鱼', '鹅', '猪']
y1 = [16, 10, 6, 18, 26]
y2 = [12, 18, 12, 8, 20]
bar = (
Bar(init_opts=opts.InitOpts(width='800px',height='400px'))
.add_xaxis(x1)
.add_yaxis('柱状图1', y1)
# 全局配置
.set_global_opts(
# 标题名
title_opts=opts.TitleOpts(title='柱状图',pos_top='20%', pos_left='40%'),
# 显示图例,
legend_opts=opts.LegendOpts(pos_top='20%', pos_left='60%'),
))
data = [(i, j) for i, j in zip(x1, y1)]
pie = (
Pie(init_opts=opts.InitOpts(width='5%', height='5%'))
.add("", data, center=['25%', '60%'], radius=['30%', '40%']) #center是调整饼图位置
# 全局配置
.set_global_opts(
# 标题名
title_opts=opts.TitleOpts(title='饼状图', pos_top='10%'),
# 显示图例,
legend_opts=opts.LegendOpts(pos_top='35%', pos_left='18%'))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c} 占比{d}%"))
)
grid = (
Grid(init_opts=opts.InitOpts(width='1550px',height='750px'))
.add(bar, grid_opts=opts.GridOpts(pos_left="55%",pos_top='30%'))
.add(pie, grid_opts=opts.GridOpts(pos_right="55%"))
)
grid.render('t11.html')
from pyecharts.charts import Bar,Line,Grid,Scatter
from pyecharts import options as opts
x = ['鸡','鸭','鱼','鹅','猪',"衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
y1 = [16,10,6,18,26,5, 20, 36, 10, 75, 90]
y2 = [12,18,12,8,20,5, 20, 36, 10, 75, 90]
bar = (
Bar()
.add_xaxis(x)
.add_yaxis('test1',y1)
.add_yaxis('test2',y2)
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='test1'),
#显示图例,
legend_opts=opts.LegendOpts(pos_top='1px'),
#横坐标
xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='动物名',name_location='middle',name_gap=40),
#纵坐标
yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40),
))
line = (
Line()
.add_xaxis(x)
.add_yaxis('test1',y1)
.add_yaxis('test2',y2)
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='test2',pos_left='5%'),
#显示图例,右边
legend_opts=opts.LegendOpts(pos_top="55%"),
#横坐标
xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='动物名',name_location='middle',name_gap=40),
#纵坐标
yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40),
))
grid = (
Grid(init_opts = opts.InitOpts(width='1100px',height='900px'))
.add(line, grid_opts=opts.GridOpts(pos_top="55%"))
.add(bar, grid_opts=opts.GridOpts(pos_bottom="55%"))
)
grid.render('t1.html')
from pyecharts.charts import Bar,Line,Grid,Scatter
from pyecharts import options as opts
x1 = ['鸡','鸭','鱼','鹅','猪']
y1 = [16,10,6,18,26]
y2 = [12,18,12,8,20]
bar = (
Bar()
.add_xaxis(x1)
.add_yaxis('柱状图1',y1)
.add_yaxis('柱状图2',y2)
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='柱状图'),
#显示图例,
legend_opts=opts.LegendOpts(pos_top='5%',pos_left='20%'),
))
line = (
Line()
.add_xaxis(x1)
.add_yaxis('折线图test',y1,
#显示symbol
is_symbol_show=True,
#symbol大小
symbol_size = 20,
#显示symbol上的数字
label_opts=opts.LabelOpts(is_show=True))
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='折线图',pos_left='50%'),
#显示图例,右边
legend_opts=opts.LegendOpts(pos_top='5%',pos_left='75%'))
#设置折线
.set_series_opts(
linestyle_opts=opts.LineStyleOpts(
# 是否显示
is_show= True,
#线宽。
width = 2,
#颜色
color='orange'))
)
scatter = (
Scatter()
.add_xaxis(x1)
.add_yaxis('散点图test',y1,symbol_size=40,color='red')
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='散点图',pos_left='50%',pos_top='50%'),
#显示图例,右边
legend_opts=opts.LegendOpts(pos_top='55%',pos_left='75%'),
))
bar1 = (
Bar()
.add_xaxis(x1)
.add_yaxis('堆积柱状图test1',y1,stack="stack1")
.add_yaxis('堆积柱状图test2',y2,stack="stack1")
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='堆积柱状图',pos_top='50%'),
#显示图例,
legend_opts=opts.LegendOpts(pos_top='55%',pos_left='18%'),
))
grid = (
Grid(init_opts = opts.InitOpts(width='1800px',height='920px'))
.add(line, grid_opts=opts.GridOpts(pos_bottom="60%", pos_left="60%"))
.add(bar, grid_opts=opts.GridOpts(pos_bottom="60%", pos_right="60%"))
.add(scatter, grid_opts=opts.GridOpts(pos_top="60%", pos_left="60%"))
.add(bar1, grid_opts=opts.GridOpts(pos_top="60%", pos_right="60%"))
)
grid.render('t1.html')
from pyecharts.charts import Bar,Line,Grid,Scatter,Pie
from pyecharts import options as opts
x1 = ['鸡','鸭','鱼','鹅','猪']
y1 = [16,10,6,18,26]
y2 = [12,18,12,8,20]
bar = (
Bar()
.add_xaxis(x1)
.add_yaxis('柱状图1',y1)
.add_yaxis('柱状图2',y2)
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='柱状图'),
#显示图例,
legend_opts=opts.LegendOpts(pos_top='5%',pos_left='20%'),
))
line = (
Line()
.add_xaxis(x1)
.add_yaxis('折线图test',y1,
#显示symbol
is_symbol_show=True,
#symbol大小
symbol_size = 20,
#显示symbol上的数字
label_opts=opts.LabelOpts(is_show=True))
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='折线图',pos_left='50%'),
#显示图例,右边
legend_opts=opts.LegendOpts(pos_top='5%',pos_left='75%'))
#设置折线
.set_series_opts(
linestyle_opts=opts.LineStyleOpts(
# 是否显示
is_show= True,
#线宽。
width = 2,
#颜色
color='orange'))
)
scatter = (
Scatter()
.add_xaxis(x1)
.add_yaxis('散点图test',y1,symbol_size=40,color='red')
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='散点图',pos_left='50%',pos_top='50%'),
#显示图例,右边
legend_opts=opts.LegendOpts(pos_top='55%',pos_left='75%'),
))
data = [(i,j) for i,j in zip(x1,y1)]
pie = (
Pie(init_opts = opts.InitOpts(width='5%',height='5%'))
.add("",data,center=['25%','80%'],radius=['0%','40%'])
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='饼状图',pos_top='50%'),
#显示图例,
legend_opts=opts.LegendOpts(pos_top='55%',pos_left='18%'))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c} 占比{d}%"))
)
grid = (
Grid(init_opts = opts.InitOpts(width='1800px',height='920px'))
.add(line, grid_opts=opts.GridOpts(pos_bottom="60%", pos_left="60%"))
.add(bar, grid_opts=opts.GridOpts(pos_bottom="60%", pos_right="60%"))
.add(scatter, grid_opts=opts.GridOpts(pos_top="60%", pos_left="60%"))
.add(pie, grid_opts=opts.GridOpts(pos_top="60%", pos_right="60%"))
)
grid.render('t11.html')
加入饼状图,需要在饼状图中加入center和radius参数,分别代表饼图所在的横纵坐标,和饼图的内外半径,最后布局的时候,饼图不能放在第一位,否则会报错。
from pyecharts.charts import Bar,Line,Scatter,Pie,Page
from pyecharts import options as opts
x1 = ['鸡','鸭','鱼','鹅','猪']
y1 = [16,10,6,18,26]
y2 = [12,18,12,8,20]
bar = (
Bar()
.add_xaxis(x1)
.add_yaxis('柱状图1',y1)
.add_yaxis('柱状图2',y2)
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='柱状图'),
))
line = (
Line()
.add_xaxis(x1)
.add_yaxis('折线图test',y1,
#显示symbol
is_symbol_show=True,
#symbol大小
symbol_size = 20,
#显示symbol上的数字
label_opts=opts.LabelOpts(is_show=True))
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='折线图'))
#设置折线
.set_series_opts(
linestyle_opts=opts.LineStyleOpts(
# 是否显示
is_show= True,
#线宽。
width = 2,
#颜色
color='orange'))
)
scatter = (
Scatter()
.add_xaxis(x1)
.add_yaxis('散点图test',y1,symbol_size=40,color='red')
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='散点图')
))
bar1 = (
Bar()
.add_xaxis(x1)
.add_yaxis('堆积柱状图test1',y1,stack="stack1")
.add_yaxis('堆积柱状图test2',y2,stack="stack1")
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='堆积柱状图'),
))
data = [(i,j) for i,j in zip(x1,y1)]
data1 = [(i,j) for i,j in zip(x1,y2)]
pie = (
Pie()
.add("",data)
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='饼状图'),
)
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c} 占比{d}%"))
)
pie1 = (
Pie()
.add("",data1,radius=['30%','70%'])
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='圆形图'),
)
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c} 占比{d}%"))
)
#Page.SimplePageLayout是将图形放在中间,缩小图形会排成一行
#Page.DraggablePageLayout是一种拖拽布局,可随意拖拽图形,
page = (
Page(layout=Page.SimplePageLayout)
.add(
bar,
line,
scatter,
bar1,
pie,
pie1)
)
page.render('t11.html')
#Page.SimplePageLayout是将图形放在中间,缩小图形会排成一行
缩小后:
from pyecharts.charts import Bar,Line,Scatter,Pie,Page
from pyecharts import options as opts
x1 = ['鸡','鸭','鱼','鹅','猪']
y1 = [16,10,6,18,26]
y2 = [12,18,12,8,20]
bar = (
Bar()
.add_xaxis(x1)
.add_yaxis('柱状图1',y1)
.add_yaxis('柱状图2',y2)
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='柱状图'),
))
line = (
Line()
.add_xaxis(x1)
.add_yaxis('折线图test',y1,
#显示symbol
is_symbol_show=True,
#symbol大小
symbol_size = 20,
#显示symbol上的数字
label_opts=opts.LabelOpts(is_show=True))
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='折线图'))
#设置折线
.set_series_opts(
linestyle_opts=opts.LineStyleOpts(
# 是否显示
is_show= True,
#线宽。
width = 2,
#颜色
color='orange'))
)
scatter = (
Scatter()
.add_xaxis(x1)
.add_yaxis('散点图test',y1,symbol_size=40,color='red')
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='散点图')
))
bar1 = (
Bar()
.add_xaxis(x1)
.add_yaxis('堆积柱状图test1',y1,stack="stack1")
.add_yaxis('堆积柱状图test2',y2,stack="stack1")
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='堆积柱状图'),
))
data = [(i,j) for i,j in zip(x1,y1)]
data1 = [(i,j) for i,j in zip(x1,y2)]
pie = (
Pie()
.add("",data)
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='饼状图'),
)
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c} 占比{d}%"))
)
pie1 = (
Pie()
.add("",data1,radius=['30%','70%'])
#全局配置
.set_global_opts(
#标题名
title_opts=opts.TitleOpts(title='圆形图'),
)
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c} 占比{d}%"))
)
#Page.SimplePageLayout是将图形放在中间,缩小图形会排成一行
#Page.DraggablePageLayout是一种拖拽布局,可随意拖拽图形,
page = (
Page(layout=Page.DraggablePageLayout)
.add(
bar,
line,
scatter,
bar1,
pie,
pie1)
)
page.render('t111.html')
点击save config,会生成一个chart_config.json文件,
再输入以下代码:
from pyecharts.charts import Page
Page.save_resize_html("t111.html", # 之前保存的HTML文件名称
cfg_file="chart_config.json", # 保存的json文件
dest="new_t111.html") # 新的HTML文件名称
生成new_t111.html
随意缩放大小都不会改变其布局。