柱状图
import pyecharts
.charts
as pyec
x
= ['甲','乙','丙']
y
= [300,800,600]
bar
= pyec
.Bar
()
bar
.add_xaxis
(x
)
bar
.add_yaxis
(series_name
='公司A',yaxis_data
=y
)
bar
.render_notebook
()
import pyecharts
.options
as opts
bar
.set_global_opts
(title_opts
=opts
.TitleOpts
(title
="比较图"))
bar
.render_notebook
()
import pyecharts
.options
as opts
bar
.set_global_opts
(title_opts
=opts
.TitleOpts
(title
="比较图"))
y1
= [1200,500,200]
bar
.add_yaxis
(series_name
='公司B',yaxis_data
=y1
)
bar
.render_notebook
()
bar
.reversal_axis
()
bar
.render_notebook
()
折线图
x1
= ['2017','2018','2019']
y1
= [300,900,600]
line
= pyec
.Line
()
line
.add_xaxis
(x1
)
line
.add_yaxis
(series_name
='A',y_axis
=y1
)
line
.render_notebook
()
y2
= [1300,500,900]
line
.add_yaxis
(series_name
='B',y_axis
=y2
)
bar
.set_global_opts
(title_opts
=opts
.TitleOpts
(title
="比较图"))
line
.render_notebook
()
工具箱设置
line
.set_global_opts
(
tooltip_opts
= opts
.TooltipOpts
(trigger
='axis',axis_pointer_type
='cross'),
toolbox_opts
= opts
.ToolboxOpts
(is_show
=True,orient
='horizontal'))
line
.render_notebook
()
设置图表的大小
x1
= ['2017','2018','2019']
y1
= [300,900,600]
line
= pyec
.Line
(init_opts
= opts
.InitOpts
(width
= '500px',height
= '300px'))
line
.add_xaxis
(x1
)
line
.add_yaxis
(series_name
='A',y_axis
=y1
)
line
.render_notebook
()
设置区域铺放功能
bar
.set_global_opts
(
tooltip_opts
= opts
.TooltipOpts
(trigger
='axis',axis_pointer_type
='cross'),
toolbox_opts
= opts
.ToolboxOpts
(is_show
=True,orient
='horizontal'),
datazoom_opts
=opts
.DataZoomOpts
(type_
= 'slider',range_start
=(),range_end
=2500))
bar
.render_notebook
()
饼图
x_data
= ['直接访问','营销推广','博客推广','搜索引擎']
y_data
= [830,214,399,1199]
data_pair
= list(zip(x_data
,y_data
))
print(data_pair
)
[('直接访问', 830), ('营销推广', 214), ('博客推广', 399), ('搜索引擎', 1199)]
pie
= pyec
.Pie
()
pie
.add
(series_name
='推广渠道',data_pair
=data_pair
)
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
,label_opts
=opts
.LabelOpts
(is_show
=False))
scatter
.add_yaxis
(series_name
='y=cos(x)',y_axis
=y2
,label_opts
=opts
.LabelOpts
(is_show
=False))
scatter
.set_global_opts
(
xaxis_opts
=opts
.AxisOpts
(splitline_opts
=opts
.SplitLineOpts
(is_show
=True)),
yaxis_opts
=opts
.AxisOpts
(splitline_opts
=opts
.SplitLineOpts
(is_show
=True)),
visualmap_opts
=opts
.VisualMapOpts
(min_
=-1,max_
=1)
)
scatter
.render_notebook
()
词云
import this
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!
s
= """
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!
"""
s
= s
.lower
().split
()
result
={}
for i
in s
:
result
[i
]=s
.count
(i
)
print(result
)
{'the': 6, 'zen': 1, 'of': 3, 'python,': 1, 'by': 1, 'tim': 1, 'peters': 1, 'beautiful': 1, 'is': 10, 'better': 8, 'than': 8, 'ugly.': 1, 'explicit': 1, 'implicit.': 1, 'simple': 1, 'complex.': 1, 'complex': 1, 'complicated.': 1, 'flat': 1, 'nested.': 1, 'sparse': 1, 'dense.': 1, 'readability': 1, 'counts.': 1, 'special': 2, 'cases': 1, "aren't": 1, 'enough': 1, 'to': 5, 'break': 1, 'rules.': 1, 'although': 3, 'practicality': 1, 'beats': 1, 'purity.': 1, 'errors': 1, 'should': 2, 'never': 2, 'pass': 1, 'silently.': 1, 'unless': 2, 'explicitly': 1, 'silenced.': 1, 'in': 1, 'face': 1, 'ambiguity,': 1, 'refuse': 1, 'temptation': 1, 'guess.': 1, 'there': 1, 'be': 3, 'one--': 1, 'and': 1, 'preferably': 1, 'only': 1, 'one': 2, '--obvious': 1, 'way': 2, 'do': 2, 'it.': 1, 'that': 1, 'may': 2, 'not': 1, 'obvious': 1, 'at': 1, 'first': 1, "you're": 1, 'dutch.': 1, 'now': 1, 'never.': 1, 'often': 1, '*right*': 1, 'now.': 1, 'if': 2, 'implementation': 2, 'hard': 1, 'explain,': 2, "it's": 1, 'a': 2, 'bad': 1, 'idea.': 2, 'easy': 1, 'it': 1, 'good': 1, 'namespaces': 1, 'are': 1, 'honking': 1, 'great': 1, 'idea': 1, '--': 1, "let's": 1, 'more': 1, 'those!': 1}
d
= list(result
.items
())
d
[('the', 6),
('zen', 1),
('of', 3),
('python,', 1),
('by', 1),
('tim', 1),
('peters', 1),
('beautiful', 1),
('is', 10),
('better', 8),
('than', 8),
('ugly.', 1),
('explicit', 1),
('implicit.', 1),
('simple', 1),
('complex.', 1),
('complex', 1),
('complicated.', 1),
('flat', 1),
('nested.', 1),
('sparse', 1),
('dense.', 1),
('readability', 1),
('counts.', 1),
('special', 2),
('cases', 1),
("aren't", 1),
('enough', 1),
('to', 5),
('break', 1),
('rules.', 1),
('although', 3),
('practicality', 1),
('beats', 1),
('purity.', 1),
('errors', 1),
('should', 2),
('never', 2),
('pass', 1),
('silently.', 1),
('unless', 2),
('explicitly', 1),
('silenced.', 1),
('in', 1),
('face', 1),
('ambiguity,', 1),
('refuse', 1),
('temptation', 1),
('guess.', 1),
('there', 1),
('be', 3),
('one--', 1),
('and', 1),
('preferably', 1),
('only', 1),
('one', 2),
('--obvious', 1),
('way', 2),
('do', 2),
('it.', 1),
('that', 1),
('may', 2),
('not', 1),
('obvious', 1),
('at', 1),
('first', 1),
("you're", 1),
('dutch.', 1),
('now', 1),
('never.', 1),
('often', 1),
('*right*', 1),
('now.', 1),
('if', 2),
('implementation', 2),
('hard', 1),
('explain,', 2),
("it's", 1),
('a', 2),
('bad', 1),
('idea.', 2),
('easy', 1),
('it', 1),
('good', 1),
('namespaces', 1),
('are', 1),
('honking', 1),
('great', 1),
('idea', 1),
('--', 1),
("let's", 1),
('more', 1),
('those!', 1)]
wordcloud
= pyec
.WordCloud
()
wordcloud
.add
(series_name
='',data_pair
=d
)
wordcloud
.render_notebook
()
x
= ['衬衫','羊毛衫','雪纺衫','裤子','高跟鞋','袜子']
y1
= [14,16,53,25,15,61]
y2
= [42,11,52,67,72,14]
y3
= [21,51,75,35,73,15]
y4
= [61,71,13,84,34,89]
bar
= pyec
.Bar
()
bar
.add_xaxis
(x
)
bar
.add_yaxis
(series_name
='公司A',yaxis_data
=y1
)
bar
.add_yaxis
(series_name
='公司B',yaxis_data
=y2
)
bar
.add_yaxis
(series_name
='公司C',yaxis_data
=y3
)
bar
.add_yaxis
(series_name
='公司D',yaxis_data
=y4
)
bar
.render_notebook
()
多种主题类型
bar
= pyec
.Bar
(init_opts
=opts
.InitOpts
(theme
=ThemeType
.LIGHT
))
bar
.add_xaxis
(x
)
bar
.add_yaxis
(series_name
='公司A',yaxis_data
=y1
)
bar
.add_yaxis
(series_name
='公司B',yaxis_data
=y2
)
bar
.add_yaxis
(series_name
='公司C',yaxis_data
=y3
)
bar
.add_yaxis
(series_name
='公司D',yaxis_data
=y4
)
bar
.render_notebook
()
bar
= pyec
.Bar
(init_opts
=opts
.InitOpts
(theme
=ThemeType
.DARK
))
bar
.add_xaxis
(x
)
bar
.add_yaxis
(series_name
='公司A',yaxis_data
=y1
)
bar
.add_yaxis
(series_name
='公司B',yaxis_data
=y2
)
bar
.add_yaxis
(series_name
='公司C',yaxis_data
=y3
)
bar
.add_yaxis
(series_name
='公司D',yaxis_data
=y4
)
bar
.render_notebook
()
bar
= pyec
.Bar
(init_opts
=opts
.InitOpts
(theme
=ThemeType
.CHALK
))
bar
.add_xaxis
(x
)
bar
.add_yaxis
(series_name
='公司A',yaxis_data
=y1
)
bar
.add_yaxis
(series_name
='公司B',yaxis_data
=y2
)
bar
.add_yaxis
(series_name
='公司C',yaxis_data
=y3
)
bar
.add_yaxis
(series_name
='公司D',yaxis_data
=y4
)
bar
.render_notebook
()
bar
= pyec
.Bar
(init_opts
=opts
.InitOpts
(theme
=ThemeType
.ESSOS
))
bar
.add_xaxis
(x
)
bar
.add_yaxis
(series_name
='公司A',yaxis_data
=y1
)
bar
.add_yaxis
(series_name
='公司B',yaxis_data
=y2
)
bar
.add_yaxis
(series_name
='公司C',yaxis_data
=y3
)
bar
.add_yaxis
(series_name
='公司D',yaxis_data
=y4
)
bar
.render_notebook
()
bar
= pyec
.Bar
(init_opts
=opts
.InitOpts
(theme
=ThemeType
.INFOGRAPHIC
))
bar
.add_xaxis
(x
)
bar
.add_yaxis
(series_name
='公司A',yaxis_data
=y1
)
bar
.add_yaxis
(series_name
='公司B',yaxis_data
=y2
)
bar
.add_yaxis
(series_name
='公司C',yaxis_data
=y3
)
bar
.add_yaxis
(series_name
='公司D',yaxis_data
=y4
)
bar
.render_notebook
()
bar
= pyec
.Bar
(init_opts
=opts
.InitOpts
(theme
=ThemeType
.MACARONS
))
bar
.add_xaxis
(x
)
bar
.add_yaxis
(series_name
='公司A',yaxis_data
=y1
)
bar
.add_yaxis
(series_name
='公司B',yaxis_data
=y2
)
bar
.add_yaxis
(series_name
='公司C',yaxis_data
=y3
)
bar
.add_yaxis
(series_name
='公司D',yaxis_data
=y4
)
bar
.render_notebook
()
bar
= pyec
.Bar
(init_opts
=opts
.InitOpts
(theme
=ThemeType
.PURPLE_PASSION
))
bar
.add_xaxis
(x
)
bar
.add_yaxis
(series_name
='公司A',yaxis_data
=y1
)
bar
.add_yaxis
(series_name
='公司B',yaxis_data
=y2
)
bar
.add_yaxis
(series_name
='公司C',yaxis_data
=y3
)
bar
.add_yaxis
(series_name
='公司D',yaxis_data
=y4
)
bar
.render_notebook
()
bar
= pyec
.Bar
(init_opts
=opts
.InitOpts
(theme
=ThemeType
.ROMA
))
bar
.add_xaxis
(x
)
bar
.add_yaxis
(series_name
='公司A',yaxis_data
=y1
)
bar
.add_yaxis
(series_name
='公司B',yaxis_data
=y2
)
bar
.add_yaxis
(series_name
='公司C',yaxis_data
=y3
)
bar
.add_yaxis
(series_name
='公司D',yaxis_data
=y4
)
bar
.render_notebook
()
bar
= pyec
.Bar
(init_opts
=opts
.InitOpts
(theme
=ThemeType
.ROMANTIC
))
bar
.add_xaxis
(x
)
bar
.add_yaxis
(series_name
='公司A',yaxis_data
=y1
)
bar
.add_yaxis
(series_name
='公司B',yaxis_data
=y2
)
bar
.add_yaxis
(series_name
='公司C',yaxis_data
=y3
)
bar
.add_yaxis
(series_name
='公司D',yaxis_data
=y4
)
bar
.render_notebook
()
bar
= pyec
.Bar
(init_opts
=opts
.InitOpts
(theme
=ThemeType
.SHINE
))
bar
.add_xaxis
(x
)
bar
.add_yaxis
(series_name
='公司A',yaxis_data
=y1
)
bar
.add_yaxis
(series_name
='公司B',yaxis_data
=y2
)
bar
.add_yaxis
(series_name
='公司C',yaxis_data
=y3
)
bar
.add_yaxis
(series_name
='公司D',yaxis_data
=y4
)
bar
.render_notebook
()
bar
= pyec
.Bar
(init_opts
=opts
.InitOpts
(theme
=ThemeType
.VINTAGE
))
bar
.add_xaxis
(x
)
bar
.add_yaxis
(series_name
='公司A',yaxis_data
=y1
)
bar
.add_yaxis
(series_name
='公司B',yaxis_data
=y2
)
bar
.add_yaxis
(series_name
='公司C',yaxis_data
=y3
)
bar
.add_yaxis
(series_name
='公司D',yaxis_data
=y4
)
bar
.render_notebook
()
bar
= pyec
.Bar
(init_opts
=opts
.InitOpts
(theme
=ThemeType
.WALDEN
))
bar
.add_xaxis
(x
)
bar
.add_yaxis
(series_name
='公司A',yaxis_data
=y1
)
bar
.add_yaxis
(series_name
='公司B',yaxis_data
=y2
)
bar
.add_yaxis
(series_name
='公司C',yaxis_data
=y3
)
bar
.add_yaxis
(series_name
='公司D',yaxis_data
=y4
)
bar
.render_notebook
()
bar
= pyec
.Bar
(init_opts
=opts
.InitOpts
(theme
=ThemeType
.WESTEROS
))
bar
.add_xaxis
(x
)
bar
.add_yaxis
(series_name
='公司A',yaxis_data
=y1
)
bar
.add_yaxis
(series_name
='公司B',yaxis_data
=y2
)
bar
.add_yaxis
(series_name
='公司C',yaxis_data
=y3
)
bar
.add_yaxis
(series_name
='公司D',yaxis_data
=y4
)
bar
.render_notebook
()
bar
= pyec
.Bar
(init_opts
=opts
.InitOpts
(theme
=ThemeType
.WONDERLAND
))
bar
.add_xaxis
(x
)
bar
.add_yaxis
(series_name
='公司A',yaxis_data
=y1
)
bar
.add_yaxis
(series_name
='公司B',yaxis_data
=y2
)
bar
.add_yaxis
(series_name
='公司C',yaxis_data
=y3
)
bar
.add_yaxis
(series_name
='公司D',yaxis_data
=y4
)
bar
.render_notebook
()
import datetime
import random
from pyecharts
import options
as opts
from pyecharts
.charts
import Calendar
def calendar_base() -> Calendar
:
begin
= datetime
.date
(2017, 1, 1)
end
= datetime
.date
(2017, 12, 31)
data
= [
[str(begin
+ datetime
.timedelta
(days
=i
)), random
.randint
(1000, 25000)]
for i
in range((end
- begin
).days
+ 1)
]
c
= (
Calendar
()
.add
("", data
, calendar_opts
=opts
.CalendarOpts
(range_
="2017"))
.set_global_opts
(
title_opts
=opts
.TitleOpts
(title
="Calendar-2017年微信步数情况"),
visualmap_opts
=opts
.VisualMapOpts
(
max_
=20000,
min_
=500,
orient
="horizontal",
is_piecewise
=True,
pos_top
="230px",
pos_left
="100px",
),
)
)
return c
calendar_base
().render_notebook
()
import pyecharts
from pyecharts
.globals import ThemeType
funnel
= pyecharts
.charts
.Funnel
()
data
= [('a',12),('b',21),('c',15),('d',16),('e',23)]
funnel
.add
('',data
)
funnel
.set_global_opts
(title_opts
=opts
.TitleOpts
(title
="Funnel-Label(inside)"))
funnel
.render_notebook
()
from pyecharts
.charts
import Gauge
gauge
= Gauge
()
gauge
.add
('',[('aaa',33.3)])
gauge
.render_notebook
()