赞
踩
针对数据分析团队需求,简单整理了一些相关内容分享同步大家学习,旨在帮助机器学习人员、数据分析人员、NLP算法人员快速了解业务数据,针对性解决工程中面临的问题。
ECharts,一个使用 JavaScript 实现的开源可视化库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Firefox,Safari等),底层依赖轻量级的矢量图形库 ZRender,提供直观,交互丰富,可高度个性化定制的数据可视化图表。
安装库 pip3 install pyecharts
如果需要绘制地理图相关内容,需要一并安装如下内容:
安装地图文件
全球国家地图: echarts-countries-pypkg
中国省级地图: echarts-china-provinces-pypkg
中国市级地图: echarts-china-cities-pypkg
全部直接使用python的pip安装,安装好之后,就可以使用了
pip3 install pyecharts
pip3 install echarts-countries-pypkg
pip3 install echarts-china-provinces-pypkg
pip3 install echarts-china-cities-pypkg
使用版本说明:
import pyecharts
pyecharts.__version__
'1.2.1'
# 导入库
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]
# 定义Line Charts 的函数
def line_charts()->Line:
c = Line()
c.add_xaxis(xaxis_data=x)
c.add_yaxis(series_name='',y_axis=y1)
c.add_yaxis(series_name='',y_axis=y2)
return c
# 绘制图表
c = line_charts()
c.render_notebook()
# 导入库 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] # 定义Line Charts 的函数 def line_charts()->Line: c = Line() c.add_xaxis(xaxis_data=x) c.add_yaxis(series_name='A',y_axis=y1) c.add_yaxis(series_name='B',y_axis=y2) # 数据项设置 c.set_global_opts( title_opts=opts.TitleOpts(title='数量统计'), legend_opts=opts.LegendOpts(is_show=True)# is_show = True 默认-展示图例 ) return c # 绘制图表 c = line_charts() c.render_notebook()
pyecharts 中提供累TooltipOpts
trigger 触发类型。可选:
item: 数据项图形触发,主要用于散点图,饼图
axis:坐标轴触发,主要用在状图,折线图
none: 什么都不做
# 导入库 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] # 定义Line Charts 的函数 def line_charts()->Line: c = Line() c.add_xaxis(xaxis_data=x) c.add_yaxis(series_name='A',y_axis=y1) c.add_yaxis(series_name='B',y_axis=y2) # 数据项设置 ,全局设置一次 c.set_global_opts( title_opts=opts.TitleOpts(title='数量统计'), legend_opts=opts.LegendOpts(is_show=True),# is_show = True 默认-展示图例 tooltip_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='cross') ) #c.set_global_opts(tooltip_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='cross')) return c # 绘制图表 c = line_charts() c.render_notebook()
pyecharts 全局参数设置
jupyter notebook 和 pycharm 基本图表绘制
如何绘制一个简单Bar 图
坐标轴文本倾斜设置
设置图表大小
封装一些绘制图表的函数
设置多组的柱状图
设置图例
区域缩放的配置项
from pyecharts import options as opts from pyecharts.charts import Bar def bar_charts()->Bar(): ''' 定义一个返回pyecharts Bar 的函数 :return: ''' x = ['seaborn', 'plotly', 'pyecharts'] y1 = [1140, 559, 270] y2 = [570,1340,1370] c = Bar(init_opts=opts.InitOpts(width='1000px',height='600px')) c.add_xaxis(xaxis_data=x) c.add_yaxis(series_name='',yaxis_data=y1) c.add_yaxis(series_name='',yaxis_data=y2) c.reversal_axis() c.set_global_opts( title_opts=opts.TitleOpts(title=''), yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=60)) ) return c c = bar_charts() c.render_notebook()
柱状图: y 轴显示 指标1
折线图: y 轴显示 指标2
from pyecharts import options as opts from pyecharts.charts import Bar,Line x = ['Python','Seaborn','Plotly','pyecharts'] # 绘制柱状图方法 def bar_charts()->Bar(): y1 = [1140, 559, 270,1200] y2 = [570,1340,1370,900] bar = Bar(init_opts=opts.InitOpts(width='1000px',height='600px')) bar.add_xaxis(xaxis_data=x) bar.add_yaxis(series_name='A',yaxis_data=y1,label_opts=opts.LabelOpts(is_show=False)) bar.add_yaxis(series_name='B',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 ) return bar # 绘制Line 方法 def line_charts()->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)) return c # Bar + Line bar = bar_charts() line = line_charts() bar.overlap(line).render_notebook()
Pie 需要的数据格式:
[[x1,y1],[x2,y2]]
绘制饼图的操作步骤:
我们分析一些我的一些课程不同的来源销售的占比
from pyecharts.charts import Pie
from pyecharts import options as opts
# 构建Pie的数据
x_data = ['直接访问','营销推广','博客推广','搜索引擎']
y_data = [830,214,300,1100]
# Pie 设置指定的格式
data_pair = [list(z) for z in list(zip(x_data,y_data))]
print(data_pair)
[['直接访问', 830], ['营销推广', 214], ['博客推广', 300], ['搜索引擎', 1100]]
def pie_charts()->Pie:
c = Pie(init_opts=opts.InitOpts(width='500px',height='500px'))
c.add(series_name='访问来源',data_pair=data_pair)
# 设置全局项
c.set_global_opts(title_opts=opts.TitleOpts(title='不同的来源的销售分析',pos_left='center',pos_top=20))
# 设置每项数据占比
c.set_series_opts(tooltip_opts=opts.TooltipOpts(trigger='item',formatter="{a} <br/> {b}:{c} ({d}%)"))
return c
c = pie_charts()
c.render_notebook()
from pyecharts.charts import Pie
from pyecharts import options as opts
# 构建Pie的数据
x_data = ['直接访问','营销推广','博客推广','搜索引擎']
y_data = [830,214,300,1100]
# Pie 设置指定的格式
data_pair = [list(z) for z in list(zip(x_data,y_data))]
print(data_pair)
[['直接访问', 830], ['营销推广', 214], ['博客推广', 300], ['搜索引擎', 1100]]
def pie_radius_charts()->Pie: c = Pie() c.add(series_name='访问来源',data_pair=data_pair,radius=['40%','75%']) c.set_global_opts( title_opts=opts.TitleOpts(title='不同销售来源'), 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}%)")) return c c = pie_radius_charts() c.render_notebook()
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))# 设置 数据点是否展示 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 = [
('宁泽涛女友',4583345),
('日学者发现侵华日军使用毒气铁证',2324539),
('伦敦眼惊现摩斯密码',2296099),
('网友请愿追责丛林法则节目组',1376545),
('寒门女孩清华毕业典礼上发言',1337607)
]
c = WordCloud()
c.add(series_name='',data_pair=data)
c.set_global_opts(title_opts=opts.TitleOpts(title='热词分析'))
c.render_notebook()
如果需要绘制地理图相关内容,需要一并安装如下内容:
安装地图文件
全球国家地图: echarts-countries-pypkg
中国省级地图: echarts-china-provinces-pypkg
中国市级地图: echarts-china-cities-pypkg
全部直接使用python的pip安装,安装好之后,就可以使用了
pip3 install pyecharts
pip3 install echarts-countries-pypkg
pip3 install echarts-china-provinces-pypkg
pip3 install echarts-china-cities-pypkg
本课程使用版本说明:
Python 版本: 3.7.x
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')
print('pyecharts version=',pyecharts.__version__)
pyecharts version= 1.2.1
def geo_charts()->Geo:
data = [['广东',104320459],['山东',95792719],['河南',94029939]]
print(data)
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='geo-案例'),legend_opts=opts.LegendOpts(is_show=True))
c.render_notebook()
return c
c = geo_charts()
c.render_notebook()
彻底掌握对业务的数据分析,你需要掌握的工具:hadoop,spark ,python,numpy ,pandas ,seaborn ,pyecharts ,matplotlib 。具体更多内容,大家可以相互交流。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。