pyecharts 库的基本使用用法
渲染图表
安装pyecharts
# !pip install pyecharts
# 导入pyecharts
import pyecharts
# 检查pyecharts版本
pyecharts.__version__
'0.5.5'
print(dir(pyecharts))
['Bar', 'Bar3D', 'Boxplot', 'Candlestick', 'EffectScatter', 'Funnel', 'Gauge', 'Geo', 'GeoLines', 'Graph', 'Grid', 'HeatMap', 'Kline', 'Line', 'Line3D', 'Liquid', 'Map', 'Overlap', 'Page', 'Parallel', 'Pie', 'Polar', 'Radar', 'Sankey', 'Scatter', 'Scatter3D', 'Style', 'ThemeRiver', 'Timeline', 'TreeMap', 'WordCloud', '__author__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_version', 'base', 'chart', 'charts', 'conf', 'configure', 'constants', 'custom', 'datasets', 'echarts', 'enable_nteract', 'engine', 'exceptions', 'js_extensions', 'jupyter_image', 'online', 'utils']
第一个示例
# 从pyecharts库中导入Bar类, 图类
from pyecharts import Bar
bar = Bar("我的第一个图表", "这里是副标题")
bar.add("服装", ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"], [5, 20, 36, 10, 75, 90])
# 该行只为了打印配置项,方便调试时使用
# bar.print_echarts_options()
# 生成本地 HTML 文件,默认名为“render.html”
bar.render()
# # 生成本地 HTML 文件,命名为“我的第一个图表.html”
bar.render(path="我的第一个图表.html")
# 在jupyter notebook中打印图
bar
<div id="34647f877fae4d96abaf8ef72e4d6829" style="width:800px;height:400px;"></div>
- add()
主要方法,用于添加图表的数据和设置各种配置项
- print_echarts_options()
打印输出图表的所有配置项
- render()
默认将会在根目录下生成一个 render.html 的文件,支持 path 参数,设置文件保存位置,如 render(r"e:\my_first_chart.html"),文件用浏览器打开。
Note: 可以按右边的下载按钮将图片下载到本地,如果想要提供更多实用工具按钮,请在 add() 中设置 is_more_utils 为 True
from pyecharts import Bar
bar = Bar("我的第一个图表", "这里是副标题")
# 自 0.5.2+ 起,pyecharts 支持更换主题。echarts 自带 dark 主题, pyecharts 也就自带了 dark,默认主题是shine
bar.use_theme('dark')
bar.add("服装",
["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
[5, 20, 36, 10, 75, 90],
is_toolbox_show=True,
is_more_utils=True)
# bar.render()
bar
<div id="6fa046543a8a41349d444c4ac52e1d70" style="width:800px;height:400px;"></div>
自定义主题:扩展主题插件,多样化图表配色
自 0.5.2+ 起,pyecharts 支持更换主体色系。
如何获得更多主题
echarts-themes-pypkg 提供了 vintage, macarons, infographic, shine 和 roma 主题。
安装主题插件
# !pip install echarts-themes-pypkg
from pyecharts import Bar