admin 管理员组

文章数量: 887021


2024年1月11日发(作者:apple tv国内能用吗)

一、 使用pyecharts绘制图表

1、 概述

进行web绘图,使用百度的echarts组件生成网页显示的图表。使用javascript在网页中呈现图表要显示的数据。

2、 pyecharts 使用python语言调用百度的图形组件库进行web图形的绘制。

3、 最终生成的文件都是网页文件,使用浏览器打开就能查看。

二、pyecharts的使用

1、安装pyecharts组件

pip install pyechrats

2、pyecharts常用的方法

(1)add(...)方法,用于加载要绘制的图表数据和配置。

(2)show_config();打印配置项信息

(3)render(“”):用于绘制生成html文件,当未指定要绘制的文件名称,默认

3、绘制图表的基本步骤

(1)初始化图表类型

Xxx=图表类型()

例如,绘制柱图

mybar=Bar()

(2)添加配置项

(.....)

(3)生成html文件

(“”)

三、pyecharts常用图的绘制

1、柱状图的绘制(使用pyecharts0.5.11版本)

from pyecharts import Bar

pnames=['方便面','饼干','火腿肠','卤蛋','花生米','榨菜']

pnums=[10,20,40,20,5,10]

mybar=Bar("商品销售图","商品季度销售统计")#第一个参数是主标题,第二个参数是副标题

('商品信息',pnames,pnums)

_config()

("product_")

使用pyecharts1.9版本呈现柱状对比图

1、安装pyecharts1.9版本

(2)示例如下

from import Bar #适用于pyecharts 1.9版本

from pyecharts import options as opts

#创建柱状图对象

bar=Bar()

pnames=['方便面','饼干','火腿肠','卤蛋','花生米','榨菜']

pnum1=[10,20,40,20,5,10]

pnum2=[15,21,50,45,10,20]

#在x轴设置商品名称

_xaxis(pnames)

#在y轴设置商品的销售数量

_yaxis("淘宝店铺",pnum1)

_yaxis("京东商铺",pnum2)

#设置全局的标题信息

_global_opts(title_opts=pts(title="商品销售数量",subtitle="淘宝和京东的销售对比图"),toolbox_opts=xOpts(is_show=True))

#设置商品标题显示的位置,在顶部显示

_series_opts(label_opts=pts(position="top"))

#生成html文档

("")

轴交换的效果

from import Bar #适用于pyecharts 1.9版本

from pyecharts import options as opts

#创建柱状图对象

bar=Bar()

pnames=['方便面','饼干','火腿肠','卤蛋','花生米','榨菜']

pnum1=[10,20,40,20,5,10]

pnum2=[15,21,50,45,10,20]

#在x轴设置商品名称

_xaxis(pnames)

#在y轴设置商品的销售数量

_yaxis("淘宝店铺",pnum1)

_yaxis("京东商铺",pnum2)

#设置全局的标题信息

_global_opts(title_opts=pts(title="商品销售数量",subtitle="淘宝和京东的销售对比图"),toolbox_opts=xOpts(is_show=True))

#设置商品标题显示的位置,在顶部显示

_series_opts(label_opts=pts(position="right"))

al_axis()#坐标旋转90°

#生成html文档

("")

2、使用pyecharts1.9版本生成饼图效果

from import Pie #引入饼图组件,适用于pyecharts1.9版本

from pyecharts import options as opts

#定义集合数据

pnames=['方便面','饼干','火腿肠','卤蛋','花生米','榨菜']

pnums=[10,20,40,20,5,10]

#创建饼图对象

pie=Pie()

#添加饼图数据

("",[list(z) for z in zip(pnames,pnums)])

#设置全局的标题信息

_global_opts(title_opts=pts(title="商品销售数量",subtitle="数量图例"),toolbox_opts=xOpts(is_show=True))

#设置商品标题显示的位置,在顶部显示

_series_opts(label_opts = pts(formatter="{b}:{pie}"))

("product_")

环形图

from import Pie #引入饼图组件,适用于pyecharts1.9版本

from pyecharts import options as opts

#定义集合数据

pnames=['方便面','饼干','火腿肠','卤蛋','花生米','榨菜']

pnums=[10,20,40,20,5,10]

#创建饼图对象

pie=Pie()

#添加环形图数据

("",[list(z) for z in zip(pnames,pnums)],radius=["40%","75%"])

#设置全局的标题信息

_global_opts(title_opts=pts(title="商品销售数量"))

#设置商品标题显示的位置,在顶部显示

_series_opts(label_opts = pts(formatter="{b}:{pie}"))

("product_")

绘制玫瑰图

from import Pie #引入饼图组件,适用于pyecharts1.9版本

from pyecharts import options as opts

#定义集合数据

pnames=['方便面','饼干','火腿肠','卤蛋','花生米','榨菜']

pnums=[10,20,40,20,5,10]

#创建饼图对象

pie=Pie()

#添加环形图数据

#("",[list(z) for z in zip(pnames,pnums)],radius=["40%","75%"])

#绘制玫瑰图

("",[list(z) for z in zip(pnames,pnums)],radius=["40%","75%"],rosetype="area")

#设置全局的标题信息

_global_opts(title_opts=pts(title="商品销售数量"))

#设置商品标题显示的位置,在顶部显示

_series_opts(label_opts = pts(formatter="{b}:{pie}"))

("product_")

散点图

from import Scatter

from pyecharts import options as opts

pnames=['方便面','饼干','火腿肠','卤蛋','花生米','榨菜']

pnums1=[10,20,40,20,5,10] #京东的商品销售数据

pnums2=[30,10,20,50,15,30] #淘宝的商品销售数量

#定义散点图对象

s=Scatter()

#添加x轴坐标的数据

_xaxis(pnames)

#添加y轴的商评数量数据

_yaxis('京东',pnums1)

_yaxis('淘宝',pnums2)

_global_opts(title_opts=pts(title='散点图'),

toolbox_opts = xOpts(is_show=True),

legend_opts

Opts(orient='vertical',pos_top='5%',pos_left="2%"))

_series_opts(label_opts=pts(position='top'))

("")

=

多图绘制

在一个页面呈现多图,需要使用网格对象grid添加绘制的图形

from import Bar,Line,Pie,Grid

from pyecharts import options as opts

A = ["小米","三星","华为","苹果","魅族","VIVO","OPPO"]

CA = [100,125,87,90,78,98,118]

B = ["草莓","芒果","葡萄","雪梨","西瓜","柠檬","车厘子"]

CB = [78,95,120,102,88,108,98]

bar = Bar()

_xaxis(A)

_yaxis("商家A",CA)

_yaxis("商家B",CB)

_global_opts(title_opts=pts(title="多图绘制"))

()

line = Line()

_xaxis(B)

_yaxis("商家A",CA)

_yaxis("商家B",CB)

_global_opts(title_opts=pts(title="2图",pos_top="48%"),

legend_opts=Opts(pos_top="48%"))

()

#绘制饼图

#定义集合数据

pnames=['方便面','饼干','火腿肠','卤蛋','花生米','榨菜']

pnums=[10,20,40,20,5,10]

#创建饼图对象

pie=Pie()

#添加环形图数据

#("",[list(z) for z in zip(pnames,pnums)],radius=["40%","75%"])

#绘制玫瑰图

("",[list(z) for z in zip(pnames,pnums)],radius=["40%","75%"],rosetype="area")

#设置全局的标题信息

_global_opts(title_opts=pts(title="商品销售数量"))

#设置商品标题显示的位置,在顶部显示

_series_opts(label_opts = pts(formatter="{b}:{pie}"))

#===================================================

grid = Grid()#网格视图对象

(bar,grid_opts=ts(pos_bottom="60%"))#添加柱图,设置位置

(line,grid_opts=ts(pos_top="60%"))#添加折线图并设置位置

#(pie,grid_opts=ts(pos_right="10%"))

("")

雷达图

from import Radar

from pyecharts import options as opts #用以设置

radar = Radar()

#由于雷达图传入的数据得为多维数据,所以这里需要做一下处理

#2个系列的5个维度的数据

value1 = [[0.79,0.90,0.46,0.57,-0.50]]

value2 = [[0.11,0.34,0.31,-0.11,0.21]]

#用于调整雷达各维度的范围大小

c_schema= [{"name": "O", "max": 1, "min": -1},

{"name": "C", "max": 1, "min": -1},

{"name": "E", "max": 1, "min": -1},

{"name": "A", "max": 1, "min": -1},

{"name": "N", "max": 1, "min": -1}]

#画图

radar = Radar() #创建雷达图对象

_schema(schema=c_schema)

("Alen", value1)

("Bella", value2)

("")

柱状图和折线图合并图使用pyecharts0.5.11版本

from pyecharts import Bar

from pyecharts import Line

from pyecharts import Style

from pyecharts import Page

from pyecharts import Overlap

def create_charts():

page = Page()

x = ['{}年'.format(i)for i in range(1,12)]

y = [3,5,3,5,3,4,5,3,5,2,4]

y1=[1,2,3,4,5,6,7,8,9,10,11]

style = Style(height=600,width=1400)

bar = Bar('柱形图',**_style,background_color=['pink'])

line = Line()

('',x,y,effect_scale=8)

('',x,y1,effect_scale=10)

('商家A',x,y,mark_line=['average'],mark_point=['min','max'])

('商家B',x,y1,mark_line=['average'],mark_point=['min','max'],is_legend_show=True)

overlap = Overlap(height=450,width=1200)

(bar)

(line)

(overlap)

return page

create_charts().render('')

词云图

from pyecharts import WordCloud #适用于pyecharts 0.5.11版本

name =['Sam S Club', 'Macys', 'Amy Schumer', 'Jurassic World', 'Charter Communications', 'Chick

Fil A', 'Planet Fitness', 'Pitch Perfect', 'Express', 'Home', 'Johnny Depp', 'Lena Dunham', 'Lewis

Hamilton', 'KXAN', 'Mary Ellen Mark', 'Farrah Abraham', 'Rita Ora', 'Serena Williams', 'NCAA

baseball tournament', 'Point Break']

value =[10000, 6181, 4386, 4055, 2467, 2244, 1898, 1484, 1112, 965, 847, 582, 555, 550, 462,

366, 360, 282, 273, 265]

wordcloud =WordCloud(width=1300, height=620)

("", name, value, word_size_range=[20, 100])

_config()

("")

折线图-面积图示例

from pyecharts import Line

attr =["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]

v1 =[5, 20, 36, 10, 10, 100]

v2 =[55, 60, 16, 20, 15, 80]

line =Line("折线图-面积图示例")

("商家A", attr, v1, is_fill=True, line_opacity=0.2, area_opacity=0.4, symbol=None)

("商家B", attr, v2, is_fill=True, area_color='#000', area_opacity=0.3, is_smooth=True)

_config()

("")

3d柱状图

from pyecharts import Bar3D

bar3d = Bar3D("3D 柱状图示例", width=1200, height=600)

x_axis = [

"12a", "1a", "2a", "3a", "4a", "5a", "6a", "7a", "8a", "9a", "10a", "11a",

"12p", "1p", "2p", "3p", "4p", "5p", "6p", "7p", "8p", "9p", "10p", "11p"

]

y_axis = [

"Saturday", "Friday", "Thursday", "Wednesday", "Tuesday", "Monday", "Sunday"

]

data = [

[0, 0, 5], [0, 1, 1], [0, 2, 0], [0, 3, 0], [0, 4, 0], [0, 5, 0],

[0, 6, 0], [0, 7, 0], [0, 8, 0], [0, 9, 0], [0, 10, 0], [0, 11, 2],

[0, 12, 4], [0, 13, 1], [0, 14, 1], [0, 15, 3], [0, 16, 4], [0, 17, 6],

[0, 18, 4], [0, 19, 4], [0, 20, 3], [0, 21, 3], [0, 22, 2], [0, 23, 5],

[1, 0, 7], [1, 1, 0], [1, 2, 0], [1, 3, 0], [1, 4, 0], [1, 5, 0],

[1, 6, 0], [1, 7, 0], [1, 8, 0], [1, 9, 0], [1, 10, 5], [1, 11, 2],

[1, 12, 2], [1, 13, 6], [1, 14, 9], [1, 15, 11], [1, 16, 6], [1, 17, 7],

[1, 18, 8], [1, 19, 12], [1, 20, 5], [1, 21, 5], [1, 22, 7], [1, 23, 2],

[2, 0, 1], [2, 1, 1], [2, 2, 0], [2, 3, 0], [2, 4, 0], [2, 5, 0],

[2, 6, 0], [2, 7, 0], [2, 8, 0], [2, 9, 0], [2, 10, 3], [2, 11, 2],

[2, 12, 1], [2, 13, 9], [2, 14, 8], [2, 15, 10], [2, 16, 6], [2, 17, 5],

[2, 18, 5], [2, 19, 5], [2, 20, 7], [2, 21, 4], [2, 22, 2], [2, 23, 4],

[3, 0, 7], [3, 1, 3], [3, 2, 0], [3, 3, 0], [3, 4, 0], [3, 5, 0],

[3, 6, 0], [3, 7, 0], [3, 8, 1], [3, 9, 0], [3, 10, 5], [3, 11, 4],

[3, 12, 7], [3, 13, 14], [3, 14, 13], [3, 15, 12], [3, 16, 9], [3, 17, 5],

[3, 18, 5], [3, 19, 10], [3, 20, 6], [3, 21, 4], [3, 22, 4], [3, 23, 1],

[4, 0, 1], [4, 1, 3], [4, 2, 0], [4, 3, 0], [4, 4, 0], [4, 5, 1],

[4, 6, 0], [4, 7, 0], [4, 8, 0], [4, 9, 2], [4, 10, 4], [4, 11, 4],

[4, 12, 2], [4, 13, 4], [4, 14, 4], [4, 15, 14], [4, 16, 12], [4, 17, 1],

[4, 18, 8], [4, 19, 5], [4, 20, 3], [4, 21, 7], [4, 22, 3], [4, 23, 0],

[5, 0, 2], [5, 1, 1], [5, 2, 0], [5, 3, 3], [5, 4, 0], [5, 5, 0],

[5, 6, 0], [5, 7, 0], [5, 8, 2], [5, 9, 0], [5, 10, 4], [5, 11, 1],

[5, 12, 5], [5, 13, 10], [5, 14, 5], [5, 15, 7], [5, 16, 11], [5, 17, 6],

[5, 18, 0], [5, 19, 5], [5, 20, 3], [5, 21, 4], [5, 22, 2], [5, 23, 0],

[6, 0, 1], [6, 1, 0], [6, 2, 0], [6, 3, 0], [6, 4, 0], [6, 5, 0],

[6, 6, 0], [6, 7, 0], [6, 8, 0], [6, 9, 0], [6, 10, 1], [6, 11, 0],

[6, 12, 2], [6, 13, 1], [6, 14, 3], [6, 15, 4], [6, 16, 0], [6, 17, 0],

[6, 18, 0], [6, 19, 0], [6, 20, 1], [6, 21, 2], [6, 22, 2], [6, 23, 6]

]

range_color = ['#313695', '#4575b4', '#74add1', '#abd9e9', '#e0f3f8', '#ffffbf',

'#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026']

(

"",

x_axis,

y_axis,

[[d[1], d[0], d[2]] for d in data],

is_visualmap=True,

visual_range=[0, 20],

visual_range_color=range_color,

grid3d_width=200,

grid3d_depth=80,

)

("")


本文标签: 数据 设置 绘制 使用