文章目录
pyecharts是百度开源的一个数据可视化JS库图形功能十分丰富。
1.Pyecharts绘制交互散点图
from pyecharts.charts import Scatter#导入散点图包
import pyecharts.options as opts#不显示数字,全局配置包
import numpy as np
x=np.linspace(0,2*np.pi,100)
y1=np.sin(x)
y2=np.cos(x)
(
Scatter()
.add_xaxis(xaxis_data=x)
.add_yaxis(series_name='sin',y_axis=y1)
.add_yaxis(series_name='cos',y_axis=y2,label_opts=opts.LabelOpts(is_show=False))#cos函数图像不显示数字
).render_notebook()
2.Pyecharts绘制交互曲线图
from pyecharts.charts import Line
import pyecharts.options as opts#参数包,不显示数字
import numpy as np
x=np.linspace(0,2*np.pi,100)
y1=np.sin(x)
y2=np.cos(x)
(
Line()
.add_xaxis(xaxis_data=x)
.add_yaxis(series_name='sin',y_axis=y1,label_opts=opts.LabelOpts(is_show=False))
.add_yaxis(series_name='cos',y_axis=y2,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绘制交互饼图
from pyecharts.charts import Pie
import pyecharts.options as opts#全局配置包,不显示数字
num=[12,11,23,44,29]
lab=['A','B','C','D','E']
(
Pie(init_opts=opts.InitOpts(width='720px',height='320px'))#图片大小
.add(series_name='',data_pair=[(i,j)for i,j in zip(lab,num)])
).render_notebook()
4.Pyecharts绘制交互环图
from pyecharts.charts import Pie
import pyecharts.options as opts#参数包,不显示数字
num=[12,11,23,44,29]
lab=['nancy','jack','jone','simply','jasion']
(
Pie(init_opts=opts.InitOpts(width='720px',height='320px'))
.add(series_name='',data_pair=[(i,j)for i,j in zip(lab,num)],radius=['40%','75%'])
).render_notebook()
5.Pyecharts绘制交互柱状图
from pyecharts.charts import Bar
import pyecharts.options as opts#参数包,不显示数字
num1=[12,11,23,44,29]
num2=[11,34,56,22,18]
lab=['nancy','jack','joneon','dkej','ekjje']
(
Bar(init_opts=opts.InitOpts(width='720px',height='320px'))
.add_xaxis(xaxis_data=lab)
.add_yaxis(series_name='商家a',yaxis_data=num1)
.add_yaxis(series_name='商家b',yaxis_data=num2)
.set_global_opts(title_opts=opts.TitleOpts(title='各个商家拥有xx数量情况',
subtitle='副标题'))
).render_notebook()
6.Pyecharts交互图形组合(并排)
from pyecharts.charts import Grid,Line,Scatter#Grid组合图形用
import numpy as np
import pyecharts.options as opts#参数包,不显示数字
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))#不显示数字
.set_global_opts(title_opts=opts.TitleOpts(title='曲线A'))#标题
)
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_bottom='60%'))
.add(lines,grid_opts=opts.GridOpts(pos_top='60%'))
).render_notebook()
7.Pyecharts交互图形组合(重合上下覆盖)
from pyecharts.charts import Bar,Line
num=[11,34,56,22,18]
lab=['nancy','jack','jone','simply','jasion']
bar=(
Bar(init_opts=opts.InitOpts(width='720px',height='320px'))
.add_xaxis(xaxis_data=lab)
.add_yaxis(series_name='',yaxis_data=num)
)
lines=(
Line()
.add_xaxis(xaxis_data=lab)
.add_yaxis(series_name='',y_axis=num,label_opts=opts.LabelOpts(is_show=False))
)
bar.overlap(lines).render_notebook()
8.Pyecharts绘制交互中国地图
from pyecharts.charts import Geo#地理包
import pyecharts.options as opts
from commons import Faker#数据包
(
Geo()
.add_schema(maptype='china')
.add(series_name='',data_pair=[(i,j) for i,j in zip(Faker.provinces,Faker.values())])
.set_global_opts(
title_opts=opts.TitleOpts(title='中国地图'),
visualmap_opts=opts.VisualMapOpts()#视觉映射
)
).render_notebook()
9.Pyecharts绘制交互中国地图(特效点图波纹)
from pyecharts.charts import Geo
import pyecharts.options as opts
from pyecharts.globals import ChartType
from commons import Faker
(
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()
10.Pyecharts绘制全国各省xx数据分布点图
c = (
Geo()
.add_schema(maptype="china")
.add("jone", [list(z) for z in zip(['江苏','浙江','湖北','湖南','河南'], [22,34,27,53,42])])
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(),
title_opts=opts.TitleOpts(title="全国各省xx数据分布"),
)
)
c.render_notebook()
11.Pyecharts绘制江苏省各市区的数据分布图
c = (
Geo()
.add_schema(maptype="江苏")
.add( "geo",
[list(z) for z in zip(['盐城市','苏州市','盐都区','常州市','镇江市'], [150,100,300,200,500])],
type_=ChartType.EFFECT_SCATTER,
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(),
title_opts=opts.TitleOpts(title="热力图江苏"),
)
)
c.render_notebook()
12.Pyecharts绘制全国主要城市航班路线和数量
from pyecharts.globals import ChartType, SymbolType
c = (
Geo()
.add_schema(maptype="china")
.add( "",
[("深圳", 120), ("哈尔滨", 66), ("杭州", 77), ("重庆", 88), ("上海", 100), ("乌鲁木齐", 30),("北京", 30),("武汉",70)],
type_=ChartType.EFFECT_SCATTER,
color="green",
)
.add( "geo",
[("北京", "上海"), ("武汉", "深圳"),("重庆", "杭州"),("哈尔滨", "重庆"),("乌鲁木齐", "哈尔滨"),("深圳", "乌鲁木齐"),("武汉", "北京")],
type_=ChartType.LINES,
effect_opts=opts.EffectOpts(
symbol=SymbolType.ARROW, symbol_size=6, color="blue"
),
linestyle_opts=opts.LineStyleOpts(curve=0.2),
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="全国主要城市航班路线和数量"))
)
c.render_notebook()
13绘制交互地理迁徙图
from pyecharts.charts import Geo
from pyecharts.globals import ChartType,SymbolType#特效散点和带箭头
import pyecharts.options as opts#全局配置
city_num=[('广州',105),('成都',70),('上海',80),('江苏',130)]#颜色深浅和数据大小有关
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,
effect_opts=opts.EffectOpts(symbol=SymbolType.ARROW,#添加指向线段
color='blue',
symbol_size=8))
).render_notebook()