赞
踩
总共22个完整的pyecharts例子,包含常用的配置方法,每个小例子都包含完整代码,为避免混淆,每个例子都差不多只包含单一配置的代码,更多有趣的源码分享可以在评论区回复。
不同系列的数据使用相同的stack值会堆叠在一起;
from pyecharts.charts import \* from pyecharts import options as opts from pyecharts.faker import Faker def bar\_stack(): bar = Bar(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) bar.add_xaxis(Faker.choose()) # stack值一样的系列会堆叠在一起 bar.add_yaxis('A', Faker.values(), stack='stack1') bar.add_yaxis('B', Faker.values(), stack='stack1') bar.add_yaxis('C', Faker.values(), stack='stack2') return bar chart = bar_stack() chart.render_notebook()
碰上类目标签过长的时候,可以选择关闭坐标轴,将数据&标签直接显示在图形中
from pyecharts.charts import \* from pyecharts import options as opts from pyecharts.faker import Faker def bar\_with\_axis\_off(): bar = Bar(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) bar.add_xaxis(Faker.choose()) bar.add_yaxis('', Faker.values()) # 碰上类目标签过长的时候,可以选择关闭坐标轴,直接显示在图形中 bar.set_series_opts(label_opts=opts.LabelOpts(position='insideLeft', formatter='{b}:{c}')) bar.set_global_opts(xaxis_opts=opts.AxisOpts(is_show=False), yaxis_opts=opts.AxisOpts(is_show=False)) bar.reversal_axis() return bar chart = bar_with_axis_off() chart.render_notebook()
x轴默认数据类型是使用离散型,在使用散点图的时候可调整为数值型
from pyecharts.charts import \* from pyecharts import options as opts import random x_data = [random.randint(0, 20) for _ in range(100)] y_data = [random.randint(0, 50) for _ in range(100)] def scatter\_with\_value\_xaxis(): scatter = Scatter(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) scatter.add_xaxis(x_data) scatter.add_yaxis('', y_data) # X轴默认数据类型为离散数据,设置为数值型 scatter.set_global_opts(xaxis_opts=opts.AxisOpts(type_="value")) return scatter chart = scatter_with_value_xaxis() chart.render_notebook()
实际是Bar和Line两个图表共用同一套坐标体系,将Bar和Line分别指向不同的Y轴
from pyecharts.charts import \* from pyecharts import options as opts import random x_data = ['香蕉', '梨子', '水蜜桃', '核桃', '西瓜', '苹果'] y_data_1 = [random.randint(10, 50) for _ in range(len(x_data))] y_data_2 = [random.randint(100, 500) for _ in range(len(x_data))] def bar\_line\_combine\_with\_two\_axis(): bar = Bar(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) bar.add_xaxis(x_data) # 添加一个Y轴 bar.extend_axis(yaxis=opts.AxisOpts()) bar.add_yaxis('左边Y轴', y_data_1, yaxis_index=0) line = Line(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) line.add_xaxis(x_data) # 将line数据通过yaxis\_index指向后添加的Y轴 line.add_yaxis('右边Y轴', y_data_2, yaxis_index=1) bar.overlap(line) return bar chart = bar_line_combine_with_two_axis() chart.render_notebook()
from pyecharts.charts import \* from pyecharts import options as opts import random x_data = ['香蕉', '梨子', '水蜜桃', '核桃', '西瓜', '苹果'] y_data_1 = [random.randint(10, 50) for _ in range(len(x_data))] y_data_2 = [random.randint(100, 500) for _ in range(len(x_data))] def bar\_with\_multiple\_axis(): bar = Bar(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) bar.add_xaxis(x_data) # 添加一个Y轴 bar.extend_axis(yaxis=opts.AxisOpts()) # 分别指定使用的Y轴 bar.add_yaxis('左边Y轴', y_data_1, yaxis_index=0) bar.add_yaxis('右边Y轴', y_data_2, yaxis_index=1) return bar chart = bar_with_multiple_axis() chart.render_notebook()
from pyecharts.charts import \* from pyecharts import options as opts import random x_data_1 = ["2020/10/{}".format(i + 1) for i in range(30)] x_data_2 = ["2019/10/{}".format(i + 1) for i in range(30)] y_data_1 = [random.randint(10, 50) for _ in range(30)] y_data_2 = [random.randint(20, 60) for _ in range(30)] def line\_with\_two\_xaxis(): line = Line(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) line.add_xaxis(x_data_1) # 添加一个x轴 line.extend_axis(xaxis_data=x_data_2, xaxis=opts.AxisOpts()) line.add_yaxis('下面X轴', y_data_1) line.add_yaxis('上面X轴', y_data_2) return line chart = line_with_two_xaxis() chart.render_notebook()
from pyecharts.charts import \* from pyecharts import options as opts import random x_data = ["2020/10/{}".format(i + 1) for i in range(30)] # 随机生成点数据 y_data = [random.randint(10, 20) for i in range(len(x_data))] def bar\_datazoom\_inside(): bar = Bar(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) bar.add_xaxis(x_data) bar.add_yaxis('', y_data) bar.set_global_opts(datazoom_opts=opts.DataZoomOpts(type_='inside', range_start=50, # 设置起止位置,50%-100% range_end=100)) return bar chart = bar_datazoom_inside() chart.render_notebook()
from pyecharts.charts import \* from pyecharts import options as opts import random x_data = ["2020/10/{}".format(i + 1) for i in range(30)] # 随机生成点数据 y_data = [random.randint(10, 20) for i in range(len(x_data))] def bar\_with\_datazoom\_slider(): bar = Bar(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) bar.add_xaxis(x_data) bar.add_yaxis('', y_data) bar.set_global_opts(datazoom_opts=opts.DataZoomOpts(range_start=50, # 设置起止位置,50%-100% range_end=100)) return bar chart = bar_with_datazoom_slider() chart.render_notebook()
from pyecharts.charts import \* from pyecharts import options as opts from pyecharts.faker import Faker def bar\_reverse\_axis(): bar = Bar(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) bar.add_xaxis(Faker.choose()) bar.add_yaxis('A', Faker.values()) bar.add_yaxis('B', Faker.values()) # x,y轴翻转 bar.reversal_axis() return bar chart = bar_reverse_axis() chart.render_notebook()
在图表加载前会有一段动画效果
from pyecharts.charts import \* from pyecharts import options as opts from pyecharts.faker import Faker """ 更多动画效果可参见:https://echarts.apache.org/examples/zh/editor.html?c=line-easing """ def bar\_with\_animation(): bar = Bar(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px', animation_opts=opts.AnimationOpts(animation_delay=1000, # 动画延时 animation_easing='bounceIn') ) ) bar.add_xaxis(Faker.choose()) bar.add_yaxis('A', Faker.values()) bar.add_yaxis('B', Faker.values()) return bar chart = bar_with_animation() chart.render_notebook()
from pyecharts.charts import \* from pyecharts import options as opts from pyecharts.faker import Faker def bar\_with\_visualmap\_color(): bar = Bar(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) bar.add_xaxis(Faker.choose()) bar.add_yaxis('A', Faker.values()) bar.add_yaxis('B', Faker.values()) # 设置视觉组件,默认通过颜色映射数据,数值范围为0-100,可通过min\_和max\_进行自定义 bar.set_global_opts(visualmap_opts=opts.VisualMapOpts()) return bar chart = bar_with_visualmap_color() chart.render_notebook()
from pyecharts.charts import \* from pyecharts import options as opts from pyecharts.faker import Faker from pyecharts.commons.utils import JsCode color_js = """ new echarts.graphic.LinearGradient( 0, 1, 0, 0, [{offset: 0, color: '#008B8B'}, {offset: 1, color: '#FF6347'}], false) """ def bar\_with\_linear\_gradient\_color(): bar = Bar(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) bar.add_xaxis(Faker.choose()) bar.add_yaxis('', Faker.values(), # 使用JsCode执行渐变色代码 itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_js))) return bar chart = bar_with_linear_gradient_color() chart.render_notebook()
from pyecharts.charts import \* from pyecharts import options as opts from pyecharts.faker import Faker def bar\_with\_custom\_splitline(): bar = Bar(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) bar.add_xaxis(Faker.choose()) bar.add_yaxis('A', Faker.values()) bar.add_yaxis('B', Faker.values()) # 设置分割线 bar.set_global_opts(yaxis_opts=opts.AxisOpts( splitline_opts=opts.SplitLineOpts(is_show=True, linestyle_opts=opts.LineStyleOpts(type_='dotted')))) return bar chart = bar_with_custom_splitline() chart.render_notebook()
from pyecharts.charts import \* from pyecharts import options as opts from pyecharts.faker import Faker def bar\_with\_dict\_config(): bar = Bar(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) bar.add_xaxis(Faker.choose()) bar.add_yaxis('', Faker.values()) # 通过字典配置itemstyle bar.set_series_opts(itemstyle_opts=dict(color='#008B8B', opacity=0.8)) return bar chart = bar_with_dict_config() chart.render_notebook()
from pyecharts.charts import \* from pyecharts import options as opts def geo\_effect\_scatter(): geo = Geo(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) geo.add_schema(maptype="china") geo.add("", [("广州", 150), ("北京", 70), ("长沙", 64), ("上海", 74), ("厦门", 63)], # 涟漪效果散点图 type_='effectScatter') return geo chart = geo_effect_scatter() chart.render_notebook()
from pyecharts.charts import \* from pyecharts import options as opts def geo\_heatmap(): geo = Geo(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) geo.add_schema(maptype="china") geo.add("", [("广州", 150), ("北京", 70), ("深圳", 64), ("上海", 74), ("杭州", 63)], type_='heatmap') # 热点图必须配置visualmap\_opts geo.set_global_opts(visualmap_opts=opts.VisualMapOpts()) return geo chart = geo_heatmap() chart.render_notebook()
from pyecharts.charts import \* from pyecharts import options as opts data = [('广东', 10430), ('山东', 9579), ('河南', 9402), ('四川', 8041), ('江苏', 7866), ('河北', 7185), ('湖南', 6568), ('安徽', 5950), ('湖北', 5724), ('浙江', 5442), ('广西', 4603), ('云南', 4597), ('江西', 4457), ('辽宁', 4375), ('黑龙江', 3831), ('陕西', 3733), ('山西', 3571), ('福建', 3552), ('贵州', 3477), ('重庆', 2884), ('吉林', 2746), ('甘肃省', 2557), ('内蒙古', 2471), ('台湾', 2316), ('上海', 2301), ('新疆', 2181), ('北京', 1961), ('天津', 1294), ('海南', 867), ('香港', 710), ('宁夏', 630), ('青海', 562), ('西藏', 300), ('澳门', 55)] def map\_with\_viusalmap(): map_chart = Map(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) map_chart.add('人口(万人)', data_pair=data, maptype='china', # 关闭symbol的显示 is_map_symbol_show=False) map_chart.set_global_opts(visualmap_opts=opts.VisualMapOpts( max_=10430, # visualmap默认映射数据范围是【0,100】,需调整 is_piecewise=True, range_color=["#CCD3D9", "#E6B6C2", "#D4587A", "#DC364C"] )) return map_chart chart = map_with_viusalmap() chart.render_notebook()
实现方式就是在折线图先填充区域
from pyecharts.charts import \* from pyecharts import options as opts import random x_data = ["2020/10/{}".format(i + 1) for i in range(30)] # 随机生成点数据 y_data = [i + random.randint(10, 20) for i in range(len(x_data))] def line\_with\_area(): line = Line(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) line.add_xaxis(x_data) line.add_yaxis('', y_data) # opacity 默认为0,即透明 line.set_series_opts(areastyle_opts=opts.AreaStyleOpts(opacity=0.5)) return line chart = line_with_area() chart.render_notebook()
from pyecharts.charts import \* from pyecharts import options as opts from pyecharts.faker import Faker def line\_stack\_area(): line = Line(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) line.add_xaxis(Faker.choose()) line.add_yaxis('A', Faker.values(), stack='stack') line.add_yaxis('B', Faker.values(), stack='stack') line.add_yaxis('C', Faker.values(), stack='stack') # opacity 默认为0,即透明 line.set_series_opts(areastyle_opts=opts.AreaStyleOpts(opacity=0.5)) return line chart = line_stack_area() chart.render_notebook()
from pyecharts.charts import \* from pyecharts import options as opts from pyecharts.faker import Faker def pie\_custom\_radius(): pie = Pie(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) pie.add("", [list(z) for z in zip(Faker.choose(), Faker.values())], # 设置半径范围,0%-100% radius=["40%", "75%"]) return pie chart = pie_custom_radius() chart.render_notebook()
from pyecharts.charts import \* from pyecharts import options as opts from pyecharts.faker import Faker def pie\_with\_custom\_label(): pie = Pie(init_opts=opts.InitOpts(theme='light', width='1000px', height='600px')) pie.add("", [list(z) for z in zip(Faker.choose(), Faker.values())]) pie.set_series_opts( # 自定义数据标签 label_opts=opts.LabelOpts(position='top', color='red', font_family='Arial', font_size=12, font_style='italic', interval=1, formatter='{b}:{d}%' ) ) return pie chart = pie_with_custom_label() chart.render_notebook()
from pyecharts.charts import \* from pyecharts import options as opts from pyecharts.faker import Faker def pie\_multiple(): pie = Pie(init_opts=opts.InitOpts(theme='light', width='1000px', height='800px')) pie.add("", [list(z) for z in zip(Faker.choose(), Faker.values())], radius=["20%", "50%"], center=["25%", "50%"]) # 添加多个饼图 pie.add("", [list(z) for z in zip(Faker.choose(), Faker.values())], radius=["20%", "50%"], center=["75%", "50%"]) return pie chart = pie_multiple() chart.render_notebook()
分享到这里结束了,感谢观看,更多Python精彩内容关注我云芸,看我主页。
(1)Python所有方向的学习路线(新版)
这是我花了几天的时间去把Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
最近我才对这些路线做了一下新的更新,知识体系更全面了。
(2)Python学习视频
包含了Python入门、爬虫、数据分析和web开发的学习视频,总共100多个,虽然没有那么全面,但是对于入门来说是没问题的,学完这些之后,你可以按照我上面的学习路线去网上找其他的知识资源进行进阶。
(3)100多个练手项目
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了,只是里面的项目比较多,水平也是参差不齐,大家可以挑自己能做的项目去练练。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。