说明
pyecharts 是一个用于生成 Echarts 图表的类库。Echarts 是百度开源的一个数据可视化 JS 库。用 Echarts 生成的图可视化效果很是棒,pyecharts 是为了与 Python 进行对接,方便在 Python 中直接使用数据生成图。python
今天咱们就用jupyter notebook(工具)和pyecharts(库)绘制图形web
1、Pyecharts绘制散点图
label_opts=opts.LabelOpts(is_show=False) #是(True)否(False)显示散点图对应的数据echarts
from pyecharts.charts import Scatter
import pyecharts.options as opts
import numpy as np
x = np.linspace(0,2*np.pi,100)
y = np.sin(x)
y2 = np.cos(x)
(
Scatter()
.add_xaxis(xaxis_data=x)
.add_yaxis(series_name='sin',y_axis=y)
.add_yaxis(series_name='cos',y_axis=y2,label_opts=opts.LabelOpts(is_show=False))#是否显示散点图对应的数据
).render_notebook()
2、Pyecharts绘制线图
title_opts=opts.TitleOpts(title=‘曲线’) #设置标题svg
from pyecharts.charts import Line
import pyecharts.options as opts
import numpy as np
x = np.linspace(0,2*np.pi,100
y = np.sin(x)
(
Line()
.add_xaxis(xaxis_data=x)
.add_yaxis(series_name='sin',y_axis=y,label_opts=opts.LabelOpts(is_show=False))
.add_yaxis(series_name='cos',y_axis=np.cos(x),label_opts=opts.LabelOpts(is_show=False))
#设置全局配置
.set_global_opts(title_opts=opts.TitleOpts(title='曲线'),#设置标题
tooltip_opts=opts.TooltipOpts(axis_pointer_type='cross')#设置交互
).render_notebook()
3、Pyecharts绘制饼图
.add(series_name=’’, data_pair=[(j, i) for i, j in zip(num, lab)])#饼图
.add(series_name=’’,data_pair=[(j,i) for i,j in zip(num,lab)],radius=[‘40%’,‘75%’])#环图
.add(series_name=’’, data_pair=[(j, i) for i, j in zip(num, lab)], rosetype=‘radius’)#南丁格尔图工具
from pyecharts.charts import Pie
import pyecharts.options as opts
import numpy as np
num = [110,136,108,111,112,103]
lab = ['哈士奇','萨摩耶','泰迪','金毛','牧羊犬','吉娃娃','柯基']
(
Pie(init_opts=opts.InitOpts(width='720px',height='320px'))#默认900,600
.add(series_name='', data_pair=[(j, i) for i, j in zip(num, lab)])#饼图
#.add(series_name='',data_pair=[(j,i) for i,j in zip(num,lab)],radius=['40%','75%'])#环图
#.add(series_name='', data_pair=[(j, i) for i, j in zip(num, lab)], rosetype='radius')#南丁格尔图
).render_notebook()
4、Pyecharts绘制柱状图
画布大小默认900x600,
修改画布大小:init_opts=opts.InitOpts(width=‘720px’,height=‘320px’))#默认900,600ui
from pyecharts.charts import Bar
import pyecharts.options as opts
num = [110,136,108,48,111,112,103]
lab = ['哈士奇','萨摩耶','泰迪','金毛','牧羊犬','吉娃娃','柯基']
(
Bar(init_opts=opts.InitOpts(width='720px',height='320px'))
.add_xaxis(xaxis_data=lab)
.add_yaxis(series_name='',yaxis_data=num)
).render_notebook()
5、Pyecharts绘制柱状图和折线图组合
from pyecharts.charts import Bar,Line
import pyecharts.options as opts
num = [110,136,108,48,111,112,103]
lab = ['哈士奇','萨摩耶','泰迪','金毛','牧羊犬','吉娃娃','柯基']
bar =(
Bar(init_opts=opts.InitOpts(width='720px',height='320px'))
.add_xaxis(xaxis_data=lab)
.add_yaxis(series_name='',yaxis_data=num)
)
lines = (
Line(init_opts=opts.InitOpts(width='720px',height='320px'))
.add_xaxis(xaxis_data=lab)
.add_yaxis(series_name='',y_axis=num,label_opts=opts.LabelOpts(is_show=False))
)
bar.overlap(lines).render_notebook()
6、Pyecharts绘制散点图和曲线组合
from pyecharts.charts import Grid, Line, Scatter
import pyecharts.options as opts
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
lines = (
Line()
.add_xaxis(xaxis_data=x)
.add_yaxis(series_name='', y_axis=y, label_opts=opts.LabelOpts(is_show=False))
)
points = (
Scatter()
.add_xaxis(xaxis_data=x)
.add_yaxis(series_name='', y_axis=y, label_opts=opts.LabelOpts(is_show=False))
)
(
Grid(init_opts=opts.InitOpts(width='720px', height='320px'))
.add(points, grid_opts=opts.GridOpts(pos_right='60%'))#在右边显示
.add(lines, grid_opts=opts.GridOpts(pos_left='60%'))#在左边显示
).render_notebook()
7、Pyecharts绘制地理迁移图
from pyecharts.charts import Geo,Map
import pyecharts.options as opts
from pyecharts.globals import ChartType,SymbolType
city_num = [('广州',105),('成都',105),('北京',105),('西安',105)]
start_end = [('广州','成都'),('广州','北京'),('广州','西安')]
(
Geo()
.add_schema(maptype='china',#设置地图类型
itemstyle_opts = opts.ItemStyleOpts(color='#323c48',#设置背景颜色
border_color='#111'))#边界颜色
.add('',data_pair = city_num,color = 'white')#添加数据,默认是点
.add('',data_pair=start_end,type_=ChartType.LINES,#把start_end用线描绘
effect_opts=opts.EffectOpts(symbol=SymbolType.ARROW, #把特效的线改为箭头
color='blue',
symbol_size=8 #箭头的大小
)
)
).render_notebook()
8、Pyecharts绘制中国地图的特效散点图
from pyecharts.charts import Geo
import pyecharts.options as opts
from commons import Faker
from pyecharts.globals import ChartType
(
Geo()
.add_schema(maptype='china')
.add(series_name='',data_pair=[(i,j) for i,j in zip(Faker.provinces,Faker.values())],
type_=ChartType.EFFECT_SCATTER)#特效散点图效果
.set_global_opts(
title_opts=opts.TitleOpts(title='中国地图'),
visualmap_opts = opts.VisualMapOpts(
is_piecewise=True)) #不是连续型的区域
).render_notebook()
9、Pyecharts绘制广东地图
from pyecharts.charts import Map
import pyecharts.options as opts
from pyecharts.globals import ChartType
from commons import Faker
(
Map()
.add( '',[(i,j) for i,j in zip(Faker.guangdong_city,Faker.values())],'广东' )
.set_global_opts(visualmap_opts=opts.VisualMapOpts())
).render_notebook()
注意 8、九须要commons.py文件spa