题目一:绘制桑基图,展示某商铺新老客服群体的商品喜好
编写程序。根据第9.3.7,绘制桑基图,展示某商铺新老客服群体的商品喜好。
运行代码:
#绘制桑基图,展示某商铺新老客服群体的商品喜好
from pyecharts import options as opts
from pyecharts.charts import Sankey
nodes =[{"name":"消费者"},{" name":"老客户"},{"name":"新客户"},{"name":"运动鞋"},{"name":"衬衫"},{"name":"连衣裙"},{"name":"高跟鞋"}
]
links =[{"source":"消费者","target":"老客户","value":30},{"source":"消费者","target":"新客户","value":20},{"source":"老客户","target":"运动鞋","value":10},{"source":"老客户","target":"衬衫","value":20},{"source":"新客户","target":"连衣裙","value":10},{"source":"新客户","target":"高跟鞋","value":10}
]
sankey_demo =(Sankey().add("", nodes = nodes , links = links , linestyle_opt = opts.LineStyleOpts(opacity =0.2,curve =0.5, color ="source"),label_opts = opts.LabelOpts (position ="right")).set_global_opts(title_opts = opts.TitleOpts(title ="桑基图示例"))
)
#sankey_demo.render_notebook()
sankey_demo.render('sankey_demo_chart.html')
运行结果:
题目二:绘制绘制由柱形图和折线图组成的、采用上下布局方式的和图表
编写程序。根据第9.4.1,绘制绘制由柱形图和折线图组成的、采用上下布局方式的和图表。
运行代码:
#绘制由柱形图和折线图组成的、采用上下布局方式的组合图表
from pyecharts import options as opts
from pyecharts.charts import Bar , Line , Grid
x_data =['小米','三星','华为','苹果','魅族','vivo','OPPO']
y_a=[107,36,102,91,51,113,45]
y_b=[104,60,33,138,105,111,91]
bar = (Bar().add_xaxis(x_data).add_yaxis("商家A",y_a).add_yaxis ("商家B",y_b).set_global_opts(title_opts = opts.TitleOpts( title ="组合图表-柱形图"),yaxis_opts = opts.AxisOpts(name="销售额(万元)", name_location ='center',name_gap =30))
)
Line=(Line().add_xaxis(x_data).add_yaxis("商家A", y_a).add_yaxis("商家B", y_b).set_global_opts(title_opts = opts.TitleOpts(title="组合图表﹣折线图", pos_top ="48%"), legend_opts = opts.LegendOpts(pos_top ="48%"), yaxis_opts= opts.AxisOpts (name ="销售额(万元)", name_location ="center", name_gap =30))
)
#创建组合图表,并以上下布局的方式显示柱形图和折线图
grid =(Grid().add(bar , grid_opts = opts.GridOpts(pos_bottom ="60%")).add(Line , grid_opts = opts.GridOpts(pos_top ="60%"))
)
#grid.render_notebook()
grid.render('grid_chart.html')
运行结果:
题目三:绘制由柱形图和折线图组成的、按顺序显示的组合图表
编写程序。根据第9.4.2,绘制由柱形图和折线图组成的、按顺序显示的组合图表。
。
运行代码:
#绘制由柱形图和折线图组成的、按顺序显示的组合图表
from pyecharts import options as opts
from pyecharts.charts import Bar , Line , Page
x_data =['小米','三星','华为','苹果','魅族','vivo','OPPO']
y_a=[107,36,102,91,51,113,45]
y_b=[104,60,33,138,105,111,91]
bar = (Bar().add_xaxis(x_data).add_yaxis("商家A",y_a).add_yaxis ("商家B",y_b).set_global_opts(title_opts = opts.TitleOpts( title ="组合图表-柱形图"),yaxis_opts = opts.AxisOpts(name="销售额(万元)", name_location ='center',name_gap =30))
)
Line=(Line().add_xaxis(x_data).add_yaxis("商家A", y_a).add_yaxis("商家B", y_b).set_global_opts(title_opts = opts.TitleOpts(title="组合图表﹣折线图", pos_top ="48%"), legend_opts = opts.LegendOpts(pos_top ="48%"), yaxis_opts= opts.AxisOpts (name ="销售额(万元)", name_location ="center", name_gap =30))
)
#绘制由柱形图和折线图组成的、按顺序显示的组合图表
page = Page()
page.add(bar, Line)
#grid.render_notebook()
page.render('page_chart.html')
运行结果:
题目四:绘制由柱形图和折线图组成的选项卡形式的组合图表
编写程序。根据第9.4.3,绘制由柱形图和折线图组成的选项卡形式的组合图表。
运行代码:
#绘制由柱形图和折线图组成的选项卡形式的组合图表
from pyecharts import options as opts
from pyecharts.charts import Bar , Line , Tab
x_data =['小米','三星','华为','苹果','魅族','vivo','OPPO']
y_a=[107,36,102,91,51,113,45]
y_b=[104,60,33,138,105,111,91]
bar = (Bar().add_xaxis(x_data).add_yaxis("商家A",y_a).add_yaxis ("商家B",y_b).set_global_opts(title_opts = opts.TitleOpts( title ="组合图表-柱形图"),yaxis_opts = opts.AxisOpts(name="销售额(万元)", name_location ='center',name_gap =30))
)
Line=(Line().add_xaxis(x_data).add_yaxis("商家A", y_a).add_yaxis("商家B", y_b).set_global_opts(title_opts = opts.TitleOpts(title="组合图表﹣折线图", pos_top ="48%"), legend_opts = opts.LegendOpts(pos_top ="48%"), yaxis_opts= opts.AxisOpts (name ="销售额(万元)", name_location ="center", name_gap =30))
)
#绘制由柱形图和折线图组成的选项卡形式的组合图表
tab = Tab()
tab.add(bar, "柱形图")
tab.add(Line, "折线图")
#grid.render_notebook()
tab.render('tab_chart.html')
运行结果:
题目五:绘制由多个柱形图组成的带时间线的组合图表
编写程序。根据第9.4.4,绘制由多个柱形图组成的带时间线的组合图表。
运行代码:
#绘制由多个柱形图组成的带时间线的组合图表
#导入 pyecharts 官方的测试数据
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar , Page , Pie , Timeline
#随机获取一组测试数据
x = Faker.choose()
tl = Timeline()
for i in range (2015,2020):bar=(Bar().add_xaxis(x)# Faker . values ()生成一个包含?个随机整数的列表.add_yaxis("商家A", Faker.values ()).add_yaxis("商家B", Faker.values ()).set_global_opts(title_opts = opts.TitleOpts("时间线轮播柱形图示例"),yaxis_opts = opts.AxisOpts (name ="销售额(万元)", name_location ="center", name_gap =30)))tl.add(bar ,"{}年".format(i))
#tl.render_notebook()
tl.render('tl_chart.html')
运行结果:
题目六:绘制由柱形图和折线图组成的、采用上下布局方式的和图表
编写程序。9.5根据9.4.1(绘制由柱形图和折线图组成的、采用上下布局方式的和图表),主题风格改为ROMA
运行代码:
#根据9.4.1(绘制由柱形图和折线图组成的、采用上下布局方式的和图表),主题风格改为ROMA
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.globals import ThemeTypex_data =['小米','三星','华为','苹果','魅族','vivo','OPPO']
y_a=[107,36,102,91,51,113,45]
y_b=[104,60,33,138,105,111,91]
bar = (Bar(init_opts=opts.InitOpts(theme=ThemeType.ROMA)).add_xaxis(x_data).add_yaxis("商家A",y_a).add_yaxis ("商家B",y_b).set_global_opts(title_opts = opts.TitleOpts( title ="柱形图-ROMA主题"),yaxis_opts = opts.AxisOpts(name="销售额(万元)", name_location ='center',name_gap =30))
)
#grid.render_notebook()
bar.render('bar_chart.html')