简介
pyecharts是一个由百度开源的数据可视化,凭借着良好的互交性,精巧的图表设计,得到了众多开发者的认可,而python是一门富有表达力的语言,很适合用于数据处理。当数据分析遇上数据可视化时,pyecharts诞生了。
安装
pip3 install pyecharts
import pyecharts
pyecharts.__version__
>>>'1.5.1'
柱状图、条形图
import pyecharts.charts as pyec
x = ['甲','乙','丙']
y = [300,800,600]
bar = pyec.Bar()
bar.add_xaxis(x)
bar.add_yaxis(series_name='公司A',yaxis_data=y)
bar.render_notebook()
画出来的图为动态图
- 把鼠标放在柱状区域会出现相关信息
- 点击图正上的小方框,公司A的信息会隐藏起来
可以把图生成html文件
bar.render("F:\\pyec.html")
>>>'F:\\pyec.html'
添加标题
import pyecharts.options as opts
bar.set_global_opts(title_opts=opts.TitleOpts(title='比较图'))
bar.render_notebook()
加另一组数据
y1 = [1200,500,200]
bar.add_yaxis(series_name='公司B',yaxis_data=y1)
bar.render_notebook()
- 可以只看公司A的对比图
- 可以只看公司B的对比图
转成条形图
bar.reversal_axis()
bar.render_notebook()
折线图
x = ['甲','乙','丙']
y = [300,800,600]
line = pyec.Line()
line.add_xaxis(x)
line.add_yaxis(series_name='A',y_axis=y)
line.render_notebook()
再加一条折线
y2 = [1300,400,700]
line.add_yaxis(series_name='B',y_axis=y2)
line.render_notebook()
同样的,也可以只显示一条折现
在图中增加提示项
数据提示
bar.set_global_opts(
tooltip_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='cross')
)
bar.render_notebook()
工具箱
bar.set_global_opts(
tooltip_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='cross'),
toolbox_opts=opts.ToolboxOpts(is_show=True,orient='horizontal'),
)
bar.render_notebook()
工具箱竖置
bar.set_global_opts(
tooltip_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='cross'),
toolbox_opts=opts.ToolboxOpts(is_show=True,orient='vertical'),
)
bar.render_notebook()
增加缩放功能
bar.set_global_opts(
tooltip_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='cross'),
toolbox_opts=opts.ToolboxOpts(is_show=True,orient='vertical'),
datazoom_opts=opts.DataZoomOpts(type_='slider',range_start=0,range_end=2500),
)
bar.render_notebook()
饼图
Pie需要的数据格式:
[[x1,y1],[x2,y2],[x3,y3]]或[(x1,y1),(x2,y2)]
x_data = ['直接访问','营销推广','博客推广','搜索引擎']
y_data = [830,214,300,1100]
data_pair = list(zip(x_data,y_data))
print(data_pair)
>>>[('直接访问', 830), ('营销推广', 214), ('博客推广', 300), ('搜索引擎', 1100)]
pie = pyec.Pie()
pie.add(series_name="推广渠道",data_pair=data_pair)
pie.render_notebook()
环形图
pie = pyec.Pie()
pie.add(series_name="推广渠道",data_pair=data_pair,radius=['50%','75%'])
pie.render_notebook()
散点图
准备工作
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
import numpy as np
import pandas as pd
plt.rcParams['font.sans-serif'] = ['SimHei']#用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False#用来正常显示负号
sns.set_style('darkgrid',{'font.sans-serif':['SimHei','Arial']})
import warnings#去除部分警告信息
warnings.filterwarnings('ignore')
import numpy as np
x = np.linspace(0,10,30)
y1 = np.sin(x)
y2 = np.cos(x)
用其他包画散点图
plt.scatter(x,y1)
sns.scatterplot(x,y1)
用pyecharts 画散点图
scatter = pyec.Scatter()
scatter.add_xaxis(xaxis_data=x)
scatter.add_yaxis(series_name='y=sin(x) 散点图',y_axis=y1)
scatter.render_notebook()
设置数据点不显示
scatter = pyec.Scatter()
scatter.add_xaxis(xaxis_data=x)
scatter.add_yaxis(series_name='y=sin(x) 散点图',
y_axis=y1,
label_opts=opts.LabelOpts(is_show=False))
scatter.render_notebook()
加数据(点的大小设置)
scatter.add_yaxis(
series_name='y=cos(x)',
y_axis = y2,
label_opts=opts.LabelOpts(is_show=False),
symbol_size=20#点大小设置
)
scatter.render_notebook()
scatter.add_yaxis(series_name='y=sin(x) 散点图',
y_axis=y1,
label_opts=opts.LabelOpts(is_show=False),
symbol_size=15)
scatter.add_yaxis(
series_name='y=cos(x)',
y_axis = y2,
label_opts=opts.LabelOpts(is_show=False),
symbol_size=20
)
scatter.render_notebook()
控制散点形状
scatter = pyec.Scatter()
scatter.add_xaxis(xaxis_data=x)
scatter.add_yaxis(series_name='y=sin(x) 散点图',
y_axis=y1,
label_opts=opts.LabelOpts(is_show=False),
symbol='circle')
scatter.render_notebook()
scatter = pyec.Scatter()
scatter.add_xaxis(xaxis_data=x)
scatter.add_yaxis(series_name='y=sin(x) 散点图',
y_axis=y1,
label_opts=opts.LabelOpts(is_show=False),
symbol='rect')
scatter.render_notebook()
scatter = pyec.Scatter()
scatter.add_xaxis(xaxis_data=x)
scatter.add_yaxis(series_name='y=sin(x) 散点图',
y_axis=y1,
label_opts=opts.LabelOpts(is_show=False),
symbol='roundRect')
scatter.render_notebook()
- triangle
- diamond
- pin
- arrow
- none