数据可视化11.3

一、pyecharts介绍

ECharts,一个使用JavaScript实现的开源可视化库,可以流畅的运行在PC和移动设备上,兼容当前绝大部分浏览器( IE8/9/10/11 , Chrome,Firefox,Safar等), 底层依赖轻量级的矢量图形库ZRender,提供直观,交互丰富,可高度个性化定制的数据可视化图表。

二、柱状图

import pyecharts.charts as pyec #导入pyecharts库
#设置数据
x=['甲','乙','丙']
y=[300,400,600]

bar=pyec.Bar()#实例化
bar.add_xaxis(x)#添加x数据为x轴
bar.add_yaxis(series_name=‘公司A’,yaxis_data=y)#添加y数据为y轴,设置图例名称,默认显示图例
bar.render_notebook()#在jupyter上显示图

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在这里插入图片描述

import pyecharts.options as opts
bar.set_global_opts(title_opts=opts.TitleOpts(title='比较图'))#添加一些设置,设置图片名称

y1=[530,210,460]
bar.add_yaxis(series_name=‘公司B’,yaxis_data=y1)#添加数据
bar.render_notebook()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在这里插入图片描述

bar.reversal_axis()#变成条形图
bar.render_notebook()

 
 
  • 1
  • 2

在这里插入图片描述

三、折线图

import pyecharts.charts as pyec
import pyecharts.options as opts
#设置数据
x1 = ['2017','2018','2019']
y1 = [300,800,600]
y2=[530,210,460]

line = pyec.Line()#实例化
line.add_xaxis(x1)
line.add_yaxis(series_name=‘公司A’,y_axis=y1)#添加y1数据为y轴,设置图例名称
line.add_yaxis(series_name=‘公司B’,y_axis=y2)#添加y2数据为y轴,设置图例名称,默认显示图例
line.set_global_opts(title_opts=opts.TitleOpts(title=‘折线图’))#添加标题
line.render_notebook()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在这里插入图片描述

line.set_global_opts(
        title_opts=opts.TitleOpts(title='我的第一幅折线图'),#设置标题
        tooltip_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='cross'),#折线图添加提示项,trigger可选参数为item和axis。item数据项图形触发,主要用于散点图,饼图;axis坐标轴触发,主要用于柱状图,折线图
        toolbox_opts=opts.ToolboxOpts(is_show=True,orient='horizontal'),#折线图工具箱(图片右上角所显示)设置,可在工具箱上直接修改数据,orient可选参数horizontal(水平显示)和vertical(垂直显示)
)
line.render_notebook()

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在这里插入图片描述
更多参数可参考help(line.set_global_opts())
在这里插入图片描述
设置图表的大小
可以通过init_opts = opts.InitOpts(width = '1000px',height = '600px'进行显示

四、饼图

Pie需要的数据格式:
[x1,y1],[x2,y2]]或[(x1,y1),(x2,y2)]
绘制饼图的操作步骤:
●构建Pie的数据
●为Pie示例对象添加数据
●设置标题
●设置每一项占比

#构建pie的数据
x_data = ["apples","bananas","pear","peaches"]
y_data = [830,500,425,988]

#pie设置指定的格式
data_pair = list(zip(x_data,y_data))
print(data_pair)

pie = pyec.Pie()#实例化
pie.add(series_name = “fruit”,data_pair = data_pair)
pie.render_notebook()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

在这里插入图片描述
环形图

#构建pie的数据
x_data = ["apples","bananas","pear","peaches"]
y_data = [830,500,425,988]

#pie设置指定的格式
data_pair = list(zip(x_data,y_data))
print(data_pair)

pie = pyec.Pie()#实例化
pie.add(series_name = “fruit”,data_pair = data_pair,radius = [‘40%’,‘75%’])
pie.set_global_opts(title_opts = opts.TitleOpts(title = ‘环形图’))
#设置每项占比
pie.set_series_opts(tooltip_opts=opts.TooltipOpts(trigger = ‘item’,formatter = “{a} <br/> {b}:{c} ({d}%)”))
pie.render_notebook()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

在这里插入图片描述

五、散点图

#生成数据
import numpy as np
x = np.linspace(0,10,30)
y1 = np.sin(x)
y2 = np.cos(x)

scatter = pyec.Scatter()
scatter.add_xaxis(xaxis_data = x)
scatter.add_yaxis(series_name = ‘y = sin(x)函数散点图’,y_axis = y1)

scatter.render_notebook()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

在这里插入图片描述

scatter1 = pyec.Scatter()
scatter1.add_xaxis(xaxis_data = x)
scatter1.add_yaxis(
series_name = 'y = sin(x)函数散点图',#图例名称
y_axis = y1,#数据
label_opts = opts.LabelOpts(is_show = False),#设置数据点是否显示
symbol_size = 15,#控制散点图点的大小
symbol = 'triangle'#设置点的形状,可选参数为circle、rect、roundRect、diamond、pin、arrow、none
)
scatter1.add_yaxis(
series_name = 'y = cos(x)函数散点图',
y_axis = y2,
label_opts = opts.LabelOpts(is_show = False))

scatter1.render_notebook()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

在这里插入图片描述

六、词云图

导入文本
txt = """The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea – let’s do more of those!’"""

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
txt = txt.lower()
sig = '~!@#$%^&*_=|?<>:"\]\'[{}-.,]'
for i in sig:
    txt = txt.replace(i,"")

words = txt.split()
counts = {}
for i in words:
counts[i] = counts.get(i,0) + 1

items = list(counts.items())

import pyecharts.options as opt
from pyecharts.charts import WordCloud
c = WordCloud()
c.add(series_name = ‘’,data_pair = items,
shape = ‘cardioid’#可选参数circle、cardioid、diamond、triangle-forward、pentagon)
c.render_notebook()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

在这里插入图片描述

                                </div>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值