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上显示图
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()
bar.reversal_axis()#变成条形图
bar.render_notebook()
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()
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()
更多参数可参考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()
#构建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}
{b}:{c} ({d}%)"))
pie.render_notebook()
#生成数据
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()
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()
导入文本
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!'"""
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()