Pyecharts是由Echarts而来,Echarts是百度开源的数据可视化的库,适合用来做图表设计开发,当使用Python与Echarts结合时就产生了Pyecharts。可使用pip安装,默认是最新版本的Pyecharts,查看安装的版本号可以使用pycharts.__version__查看。
安装
现在安装的v1版本与以前的0.5版本是不兼容的,使用方法上存在较大的差异,并且v0.5版本对Python的支持在Python2.7和3.4+的版本上,v1版本支持最新的Python版本。所以网上的很多关于Pyecharts的代码在新版本上并不适用,安装命令:pin install pyecharts
链式调用
可以使用链式调用的方法来创建一个图表
from pyecharts.charts import Bar
from pyecharts import options as opts
bar= (
Bar()
.add_xaxis(["裤子", "高跟鞋", "袜子"])
.add_yaxis(["销售额"],[300,509,300])
.set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题"))
)
bar.render()
上述代码中我们可以加入主题:from pyecharts.globals import ThemeType
Bar(init_opts.IninOpts(theme=ThemeType.LTGHT))
图形绘制
日历图
Calendar可以用来显示日历图,timedelta用来设置日期间的间隔。具体代码如下
import datetime
import random
from pyecharts import options as opts
from pyecharts.charts import Calendar
def calendar_base() -> Calendar:
begin = datetime.date(2018, 1, 1) #设置起始日期
end = datetime.date(2019, 12, 31) #设置终止日期
data =[
[str(begin + datetime.timedelta(days=i)), random.randint(1000, 25000)] #设置日期间隔