pyecharts一直被誉为python可视化的神器,因为它只需较少的代码既可以绘制非常漂亮的图形
今天主要分享一下pyecharts的常见绘图方法,并把官方中文文档.pdf送给大家
1.利用Bar绘制柱状图
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = ( Bar()
.add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣",
"高跟鞋", "袜子"])
.add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
.set_global_opts(title_opts = opts.TitleOpts(title = "某商场销售情况")) )
bar.render_notebook()
2.并列柱状图绘制
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = Bar()
bar.add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
bar.add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
bar.set_global_opts(title_opts = opts.TitleOpts(title = "货品销售情况",subtitle = "A和B公司"))
bar.render_notebook()
3.绘制水平直方图
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = Bar()
bar.add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
bar.add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
bar.set_global_opts(title_opts=opts.TitleOpts(title="货品销售情况",subtitle = "A和B公司"),toolbox_opts = opts.ToolboxOpts(is_show = True))
bar.set_series_opts(label_opts=opts.LabelOpts(position = "right"))
bar.reversal_axis()
bar.render_notebook()
4.饼图绘制
# from example.commons import Faker
from pyecharts import options as opts
from pyecharts.charts import Page, Pie
L1=['教授','副教授','讲师','助教','其他']
num = [20,30,10,12,8]
c = Pie()
c.add("", [list(z) for z in zip(L1,num)])
c.set_global_opts(title_opts=opts.TitleOpts(title="Pie-职称类别比例"))
c.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
c.render_notebook()
5.圆形饼图绘制
from pyecharts import options as opts
from pyecharts.charts import Page, Pie
wd = ['教授','副教授','讲师','助教','其他']
num = [20,30,10,12,8]
c = Pie()
c.add("",[list(z) for z in zip(wd, num)],radius = ["40%", "75%"])
c.set_global_opts( title_opts=opts.TitleOpts(title="Pie-Radius"),legend_opts=opts.LegendOpts( orient="vertical", pos_top="5%", pos_left="2%" ))
c .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
c.render_notebook()
6.圆形饼图中的rich参数应用
from pyecharts import options as opts
from pyecharts.charts import Page, Pie
wd = ['教授','副教授','讲师','助教','其他']
num = [20,30,10,12,8]
c = Pie()
c.add("",[list(z) for z in zip(wd, num)],radius=["40%", "55%"],
label_opts=opts.LabelOpts(position="outside",
formatter="{a|{a}}{abg|}\n{hr|}\n{b|{b}:}{c} {per|{d}%} ",
background_color="#eee",border_color="#aaa",
border_width=1,border_radius=4,
rich={"a": {"color": "#999", "lineHeight": 22, "align":
"center"},"abg": {"backgroundColor": "#e3e3e3","width": "100%", "align": "right", "height": 22,"borderRadius": [4, 4, 0, 0],}, "hr": {"borderColor": "#aaa", "width": "100%",
"borderWidth": 0.5, "height": 0,}, "b": {"fontSize": 16, "lineHeight": 33},"per": {"color": "#eee",
"backgroundColor": "#334455", "padding": [2, 4],"borderRadius": 2,} } ))
c.set_global_opts(title_opts=opts.TitleOpts(title="Pie-富文本示例"))
c.render_notebook()
7.玫瑰图绘制
from pyecharts import options as opts
from pyecharts.charts import Page, Pie
data1 = [45,86,39,52,68]
data2 = [67,36,64,89,123]
labels = ['电脑','手机','彩电','冰箱','洗衣机']
c = Pie()
c.add("",[list(z) for z in zip(labels, data1)],radius=["35%", "70%"],center=[180,220],rosetype='radius')
c.add("",[list(z) for z in zip(labels, data2)],radius=["35%", "70%"],center=[550,220],rosetype='area')
c.set_global_opts(title_opts=opts.TitleOpts(title="玫瑰图"))
c.render_notebook()
8.漏斗图绘制
from pyecharts.charts import Funnel
from pyecharts import options as opts
%matplotlib inline
data = [45,86,39,52,68]
labels = ['电脑','手机','彩电','冰箱','洗衣机']
wf = Funnel()
wf.add('电器销量图',[list(z) for z in zip(labels, data)], is_selected= True)
wf.render_notebook()
9.散点图绘制
from pyecharts import options as opts
from pyecharts.charts import Scatter
week = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
c = Scatter()
c.add_xaxis(week)
c.add_yaxis("商家A", [81,65,48,32,68,92,87])
c.set_global_opts(title_opts=opts.TitleOpts(title="Scatter-一周的销售额(万元)"))
c.render_notebook()
10.K线图绘制
from pyecharts import options as opts
from pyecharts.charts import Kline
data = [[2320.26, 2320.26, 2287.3, 2362.94],
[2300, 2291.3, 2288.26, 2308.38],
[2295.35, 2346.5, 2295.35, 2345.92],
[2347.22, 2358.98, 2337.35, 2363.8],
[2360.75, 2382.48, 2347.89, 2383.76],
[2383.43, 2385.42, 2371.23, 2391.82],
[2377.41, 2419.02, 2369.57, 2421.15],
[2425.92, 2428.15, 2417.58, 2440.38],
[2411, 2433.13, 2403.3, 2437.42],
[2432.68, 2334.48, 2427.7, 2441.73],
[2430.69, 2418.53, 2394.22, 2433.89],
[2416.62, 2432.4, 2414.4, 2443.03],
[2441.91, 2421.56, 2418.43, 2444.8],
[2420.26, 2382.91, 2373.53, 2427.07],
[2383.49, 2397.18, 2370.61, 2397.94],
[2378.82, 2325.95, 2309.17, 2378.82],
[2322.94, 2314.16, 2308.76, 2330.88],
[2320.62, 2325.82, 2315.01, 2338.78],
[2313.74, 2293.34, 2289.89, 2340.71],
[2297.77, 2313.22, 2292.03, 2324.63],
[2322.32, 2365.59, 2308.92, 2366.16],
[2364.54, 2359.51, 2330.86, 2369.65],
[2332.08, 2273.4, 2259.25, 2333.54],
[2274.81, 2326.31, 2270.1, 2328.14],
[2333.61, 2347.18, 2321.6, 2351.44],
[2340.44, 2324.29, 2304.27, 2352.02],
[2326.42, 2318.61, 2314.59, 2333.67],
[2314.68, 2310.59, 2296.58, 2320.96],
[2309.16, 2286.6, 2264.83, 2333.29],
[2282.17, 2263.97, 2253.25, 2286.33],
[2255.77, 2270.28, 2253.31, 2276.22] ]
c = Kline()
c.add_xaxis(["2019/7/{}".format(i + 1) for i in range(31)])
c.add_yaxis("2019年7月份K线图", data)
c.set_global_opts(yaxis_opts=opts.AxisOpts(is_scale=True),
xaxis_opts=opts.AxisOpts(is_scale=True),
title_opts=opts.TitleOpts(title="Kline-基本示例"),)
c.render_notebook()
11.仪表盘图绘制
from pyecharts import options as opts
from pyecharts.charts import Gauge, Page
c = Gauge()
c.add("业务指标",[("完成率", 55.5)],axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(color=[(0.3, "#67e0e3"),
(0.7, "#37a2da"), (1, "#fd666d")], width=30)))
c.set_global_opts(title_opts=opts.TitleOpts(title="Gauge-不同颜色"),
legend_opts=opts.LegendOpts(is_show=False))
c.render_notebook()
11.词云绘制
from pyecharts import options as opts
from pyecharts.charts import Page, WordCloud
from pyecharts.globals import SymbolType
words = [
("牛肉面", 7800),("黄河", 6181),
("《读者》杂志", 4386), ("甜胚子", 3055),
("甘肃省博物馆", 2055),("莫高窟", 8067),("兰州大学", 4244),
("西北师范大学", 1868),("中山桥", 3484),
("月牙泉", 1112),("五泉山", 980),
("五彩丹霞", 865),("黄河母亲", 847),("崆峒山",678),
("羊皮筏子", 1582),("兴隆山",868),
("兰州交通大学", 1555),("白塔山", 2550),("五泉山", 2550)]
c = WordCloud()
c.add("", words, word_size_range=[20, 80])
c.set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-基本示例"))
c.render_notebook()
12.在指定地图上的城市标示某天最高温度
from pyecharts import options as opts
from pyecharts.charts import Map
temperature=[30,31,27,29,18]
loc = ['兰州市','天水市','白银市','武威市','甘南藏族自治州']
c = Map()
c.add("甘肃省", [list(z) for z in zip(loc, temperature)], "甘肃",is_roam=True)
c .set_global_opts(title_opts=opts.TitleOpts(title="甘肃省部分城市最高气温"))
c.render("APP类型.html")
c.render_notebook()
13.组合图表上下布局
from pyecharts import options as opts
from pyecharts.charts import Bar, Grid, Line,Scatter
A = ["小米", "三星", "华为", "苹果", "魅族", "VIVO", "OPPO"]
CA = [100,125,87,90,78,98,118]
B = ["草莓", "芒果", "葡萄", "雪梨", "西瓜", "柠檬", "车厘子"]
CB = [78,95,120,102,88,108,98]
bar = Bar()
bar.add_xaxis(A)
bar.add_yaxis("商家A",CA)
bar.add_yaxis("商家B", CB)
bar.set_global_opts(title_opts=opts.TitleOpts(title="Grid-Bar"))
bar.render_notebook()
line=Line()
line.add_xaxis(B)
line.add_yaxis("商家A", CA)
line.add_yaxis("商家B", CB)
line.set_global_opts(title_opts=opts.TitleOpts(title="Grid-Line", pos_top="48%"),
legend_opts=opts.LegendOpts(pos_top="48%"))
line.render_notebook()
grid = Grid()
grid.add(bar, grid_opts=opts.GridOpts(pos_bottom="60%"))
grid.add(line, grid_opts=opts.GridOpts(pos_top="60%"))
上面介绍的都是最基本的绘图方式,想要把图画的更美观、更漂亮,还需要认真研究一下官方文档,掌握各种配置方式,这里已经把官方中文文档给大家备好,总共137页,附带源码、可复制,请关注微信公众号 “python数据分析之禅”,回复关键词 “pyecharts” 获取