本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,,版权归原作者所有,如有问题请及时联系我们以作处理
本篇文章来自腾讯云 作者:数据山谷
私信小编01即可获取大量Python学习资料
先给大家展现给不一样得东西看看。
![e01298b7c699079dda27ccf200f34f51.png](https://i-blog.csdnimg.cn/blog_migrate/b64c0682f15d09ba3c151fad35d0097f.jpeg)
![ea3ffdaf09091efccc2ee3e6d27e6e51.png](https://i-blog.csdnimg.cn/blog_migrate/fea261135de3f36214c86b99f7892cc7.jpeg)
导包
# 导入options配置项from pyecharts import options as opts# 导入柱状图,时间轴from pyecharts.charts import Bar,Timeline# 导入Faker随机生成标签from pyecharts.faker import Faker# 使用random随机生成数值import random
首先我们随机生成x轴数据
phone=Faker.phones
开头展示的柱状图熟悉么?
tl = Timeline()# 写一个时间轴函数def timeline_bar() -> Timeline: for i in range(2015, 2020): data1=random.sample(range(0, 100), 7) data2=random.sample(range(0, 100), 7) bar=Bar() bar.add_xaxis(phone) bar.add_yaxis('A店',data1) bar.add_yaxis('B店',data2) bar.set_global_opts(title_opts=opts.TitleOpts(.format(i),sub)) tl.add(bar, "{}年".format(i)) return tltimeline_bar().render_notebook()
添加数据标签和辅助线之后的时间轴展示
tl = Timeline()def timeline_bar() -> Timeline: for i in range(2015, 2020): data1=random.sample(range(10, 100), 7) data2=random.sample(range(10, 100), 7) bar=(Bar() .add_xaxis(phone) .add_yaxis('A店',data1) .add_yaxis('B店',data2) .set_global_opts(title_opts=opts.TitleOpts(.format(i),sub), legend_opts=opts.LegendOpts(is_show=True) ) .set_series_opts(label_opts=opts.LabelOpts(is_show=True), markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_='max',name='最大值')]), markline_opts=opts.MarkLineOpts(data=[opts.MarkPointItem(type_='min',name='最小值')]) ) ) tl.add(bar, "{}年".format(i)) return tl timeline_bar().render_notebook()
效果
![87d36b3bb2f452e3547919946e77e78b.png](https://i-blog.csdnimg.cn/blog_migrate/354f2d2e486c2d9e5fd6004c33a0e768.jpeg)
变换之后得效果展示
tl = Timeline()def timeline_bar() -> Timeline: for i in range(2015, 2020): data1=random.sample(range(10, 100), 7) data2=random.sample(range(10, 100), 7) bar=(Bar() .add_xaxis(phone) .add_yaxis('A店',data1,stack="stack1", category_gap="50%") .add_yaxis('B店',data2, stack="stack1", category_gap="50%") .set_global_opts(title_opts=opts.TitleOpts(.format(i),sub), legend_opts=opts.LegendOpts(is_show=True), ) .set_series_opts(label_opts=opts.LabelOpts(is_show=True), markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_='max',name='最大值')]), markline_opts=opts.MarkLineOpts(data=[opts.MarkPointItem(type_='min',name='最小值')]) ) .reversal_axis() ) tl.add(bar, "{}年".format(i)) return tl timeline_bar().render_notebook()
效果
![0e9c15858b7c38288c43bf6cb1e5babd.png](https://i-blog.csdnimg.cn/blog_migrate/269436415258b05ed657163b54609c8c.jpeg)
再来看看堆积柱状图的样子!
tl = Timeline()def timeline_bar() -> Timeline: for i in range(2015, 2020): data1=random.sample(range(10, 100), 7) data2=random.sample(range(10, 100), 7) bar=(Bar() .add_xaxis(phone) .add_yaxis('A店',data1,stack="stack1", category_gap="50%") .add_yaxis('B店',data2, stack="stack1", category_gap="50%") .set_global_opts(title_opts=opts.TitleOpts(.format(i),sub), legend_opts=opts.LegendOpts(is_show=True), ) .set_series_opts(label_opts=opts.LabelOpts(is_show=True), markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_='max',name='最大值')]), markline_opts=opts.MarkLineOpts(data=[opts.MarkPointItem(type_='min',name='最小值')]) ) ) tl.add(bar, "{}年".format(i)) return tl timeline_bar().render_notebook()
效果
![8abd0c724696df111f1f8ff1d2a1e40b.png](https://i-blog.csdnimg.cn/blog_migrate/77c16ea0dfa7b4624d00360c6b8f4835.jpeg)
给大家介绍一位新朋友!!!
# 导入3D柱状图的包from pyecharts.charts import Bar3D# 随机生成x.y.z轴数据phone=Faker.phonesdays =Faker.week_endata = [(i, j, random.randint(0,20)) for i in range(7) for j in range(7)]bar3d=Bar3D(init_opts=opts.InitOpts(, ))bar3d.add( series_name="", data=data, xaxis3d_opts=opts.Axis3DOpts(type_="category", data=phone), yaxis3d_opts=opts.Axis3DOpts(type_="category", data=days), zaxis3d_opts=opts.Axis3DOpts(type_="value"), )bar3d.set_global_opts( visualmap_opts=opts.VisualMapOpts( max_=25 ) )bar3d.render_notebook()
![cfa6d159bf9565134afab10a3465eb74.png](https://i-blog.csdnimg.cn/blog_migrate/47dfa759cc56b4610bce58e1d25c28be.jpeg)
细心地小伙伴今天可能已经发现了,我们的代码是用链式法写的,这样的简便写法大家尝试一下