赞
踩
目录
pyecharts:python 调用echarts库
echarts 是开源的可视化工具 http://echarts.baidu.com
- from pyechart.chart import Bar #从pyechrt 中导入柱状图
- from pyechrts import options as opts #全局参数设置
-
-
- x = ['张三','李四','王五']
- y1 = [1140,550,270]
- y2 = [570,1340,1370]
-
- bar = Bar() # 示例对象
- bar.add_xaxis(xaxis_data=x) # x 轴坐标的数据
- bar.add_yaxis(series_name='平台-A',yaxis_data=y1) # 图例名称+y轴数据
- bar.add_yaxis(series_name='平台-B',yaxis_data=y2)
- bar.set_global_opts(title_opts=opts.TitleOpts(title='测试一下'))#全局变量设置
- # 生成HTML 文件
- bar.render(path='bar_test.html')
- from pyecharts.charts import Bar
- from pyecharts import options as opts
- # Bar 参数数据格式
- x = ['张三','李四','王五']
- y1 = [1140,550,270]
- y2 = [570,1340,1370]
-
- bar2 = Bar()
- bar2.add_xaxis(x)
- bar2.add_yaxis(series_name='平台-A',yaxis_data=y1) # 图例名称+y轴数据
- bar2.add_yaxis(series_name='平台-B',yaxis_data=y2)
-
- bar2.set_global_opts(title_opts=opts.TitleOpts(title='销售数量'))
-
- bar2.reversal_axis() # x 轴和y轴进行坐标轴的数据转化。 使用这个代码之后横向图表会展示成纵向图表
- bar2.render_notebook()
使用这种方法生成图片需要进行环境变量的设置,以下是windows中的环境变量设置链接。
https://www.jianshu.com/p/dc0336a0bf50https://www.jianshu.com/p/dc0336a0bf50
- #导入相关库
- import pyecharts.options as opts #全局变量设置
- from pyecharts.charts import Line #折线图
-
- #数据
- x = ['seaborn','matplotlib','plotly','pyecharts','python']
- y1 = [440,550,770,450,800]
- y2 = [570,1340,1370,1111,2222]
-
- #用函数来绘制图形
- c = Line()
- c.add_xaxis(xaxis_data=x) #x轴数值
- c.add_yaxis(series_name='',y_axis=y1) #y轴数值与标签设置
- c.add_yaxis(series_name='',y_axis=y2)
-
- #对工具箱中名称修改
- data_zoom = {
- "show":True,
- "title":{"zoom":"data zoom","back":"data zoom restore"}
-
- }
-
- #set_global_opts全局变量设置
- c.set_global_opts(
- title_opts=opts.TitleOpts(title='测试数据'), #图表标题
- legend_opts=opts.LegendOpts(is_show=True), #图例为True时展示
- tooltip_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='cross'), #触发类型,就是鼠标悬停在图表上时显示的内容
- toolbox_opts=opts.ToolboxOpts(is_show=True,orient='horizontal',
- feature=opts.ToolBoxFeatureOpts(data_zoom=data_zoom))#显示工具箱(下载图片、转换图形为柱状图、刷新等)
- )
-
-
- c.render_notebook() #在jupyter notebook中生成图形
- #导库
- from pyecharts import options as opts
- from pyecharts.charts import Bar
-
- #数据
- x = ['Python 数据可视化 seaborn','Python 数据可视化 plotly','Python 数据可视化 pyecharts']
- y1 = [1140,559,270]
- y2 = [570,1340,1370]
-
- # 创建Bar 示例对象,同时x,y轴数据填充
- bar = Bar(init_opts=opts.InitOpts(width='1000px',height='600px')) #设置图表的大小 init_opts
-
- bar.add_xaxis(xaxis_data=x)
- bar.add_yaxis(series_name='',yaxis_data=y1)
-
-
- # bar 设置坐标轴的反转操作
- bar.reversal_axis()
-
-
- # 设置全局参数
- bar.set_global_opts(
- title_opts=opts.TitleOpts(title='测试数据'),
- yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=65)), #文字倾斜度
- legend_opts=opts.LegendOpts(is_show=True), # 展示图例默认为 True
- datazoom_opts=opts.DataZoomOpts(type_='slider',range_start=0,range_end=1500) #设置区域缩放配置
- )
-
- # 展示图表
- bar.render_notebook()
- bar.render_notebook()
- from pyecharts import options as opts
- from pyecharts.charts import Bar,Line
-
- x = ['Python','Seaborn','Plotly','pyecharts']
- # 绘制柱状图方法
-
- y1 = [1140, 559, 270,1200]# 数据1
- y2 = [570,1340,1370,900] # 数据2
- bar = Bar(init_opts=opts.InitOpts(width='1000px',height='600px'))
- bar.add_xaxis(xaxis_data=x)
- bar.add_yaxis(series_name='数据1',yaxis_data=y1,label_opts=opts.LabelOpts(is_show=True))
- bar.add_yaxis(series_name='数据2',yaxis_data=y2,label_opts=opts.LabelOpts(is_show=False))
-
- bar.set_global_opts(title_opts=opts.TitleOpts(title='测试数据'))
-
- # bar 扩展
- bar.extend_axis(
- yaxis=opts.AxisOpts(
- name='价格',
- type_='value',
- min_=0,
- max_=200,
- interval=10,
- axislabel_opts=opts.LabelOpts(formatter='{value} 吨')) # value
- )
-
-
-
- # 绘制Line 方法
-
- y = [159,29,49,79]
- c = Line()
- c.add_xaxis(xaxis_data=x)
- c.add_yaxis(series_name='重量',yaxis_index=1,y_axis= y,label_opts=opts.LabelOpts(is_show=False))
-
-
-
- # Bar + Line
- bar = bar
- line = c
-
- bar.overlap(line).render_notebook() #柱状体和折线图的组合
-
- #bar.render_notebook()
-
- #bar.overlap(line).render()
- # 导入库
-
- from pyecharts.charts import Pie
- from pyecharts import options as opts
-
- #数据
- x_data = ['张三','李四','王五','赵六']
- y_data = [830,214,300,1100]
-
- # Pie 设置指定的格式
- data_pair = [list(z) for z in list(zip(x_data,y_data))] # [['张三', 830], ['李四', 214], ['王五', 300], ['赵六', 1100]]
-
- c = Pie(init_opts=opts.InitOpts(width='500px',height='500px'))
- c.add(series_name='访问来源',data_pair=data_pair,radius=['40%','75%']) #radius 讲饼图展示位环形图
-
- # 设置全局项
- c.set_global_opts(title_opts=opts.TitleOpts(title='测试数据',pos_left='center',pos_top=20),
- legend_opts=opts.LegendOpts(orient='vertical',pos_top='15%',pos_left='2%')) #图例
-
-
- # 设置每项数据占比
- c.set_series_opts(tooltip_opts=opts.TooltipOpts(trigger='item',formatter="{a} <br/> {b}:{c} ({d}%)")) #formatter 图表悬停时数据展示格式化
- c.render_notebook()
- #导入库
- from pyecharts.charts import Scatter
- from pyecharts import options as opts
- import numpy as np
-
- #导入数据
- x_data = np.linspace(0,10,30)
- y1 = np.sin(x_data)
- y2 = np.cos(x_data)
-
-
- figsize = opts.InitOpts(width='800px',height='300px')
- scatter = Scatter(init_opts=figsize)
- scatter.add_xaxis(xaxis_data=x_data)
-
- scatter.add_yaxis(
- series_name='y = sin(x) 函数散点图', # 图例名称
- y_axis = y1,#数据
- label_opts=opts.LabelOpts(is_show=False),
- symbol_size=20 # 控制散点图 点的大小
- )# 设置 数据点是否展示
-
- scatter.add_yaxis(
- series_name='y = cos(x) 函数散点图',
- y_axis = y2,
- label_opts=opts.LabelOpts(is_show=False)
- )
-
- scatter.set_global_opts(title_opts=opts.TitleOpts(title='散点图',pos_top='20px',pos_left='center'))
- scatter.render_notebook()
- #导入库
- import pyecharts.options as opts
- from pyecharts.charts import WordCloud
-
- #数据
-
- data = [
- ('python',4583345),
- ('excel',2324539),
- ('人工智能',2296099),
- ('机器学习',1376545),
- ('深度学习',1337607),
- ('mysql',4583345),
- ('数据',5324539),
- ('数据分析',3296099),
- ('数据挖掘',1376545)
- ]
-
- c = WordCloud()
- c.add(series_name='图表',data_pair=data)
- c.set_global_opts(title_opts=opts.TitleOpts(title='热词展示'))
- c.render_notebook()
- #在开始使用之前需要先安装对应的地图文件
- pip3 install pyecharts
- pip3 install echarts-countries-pypkg
- pip3 install echarts-china-provinces-pypkg
- pip3 install echarts-china-cities-pypkg
- #导入库 pyecharts: 1.x
- from pyecharts import options as opts
- from pyecharts.charts import Geo
- from pyecharts.globals import ChartType
-
- import pyecharts
- import warnings
- warnings.filterwarnings('ignore')
-
-
- #绘制地图
- data = [['广东',104320459],['山东',95792719],['河南',94029939]]
-
-
- c = Geo()
- c.add_schema(maptype='china',is_roam=False,label_opts=opts.LabelOpts(is_show=True))
- c.add('geo',data,type_=ChartType.EFFECT_SCATTER,symbol_size=12,symbol='pin')
- c.set_global_opts(title_opts=opts.TitleOpts(title='测试数据'),legend_opts=opts.LegendOpts(is_show=True))
- c.render_notebook()
- c.render_notebook()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。