常用圖形:柱形圖-折線圖-餅圖-散點(diǎn)圖
from pyecharts import Line, Bar, Pie, EffectScatter
# 數(shù)據(jù)
attr =["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
v1 =[5, 20, 36, 10, 10, 100]
v2 =[55, 60, 16, 20, 15, 80]
柱形圖
bar = Bar('柱形圖', '庫存量')
bar.add('服裝', attr, v1, is_label_show=True)
bar.show_config()
bar.render(path='./data/01-01柱形圖.html')
bar2 = Bar("顯示標(biāo)記線和標(biāo)記點(diǎn)")
bar2.add('商家A', attr, v1, mark_point=['avgrage'])
bar2.add('商家B', attr, v2, mark_point=['min', 'max'])
bar2.show_config()
bar2.render(path='./data/01-02標(biāo)記點(diǎn)柱形圖.html')
bar3 = Bar("水平顯示")
bar3.add('商家A', attr, v1)
bar3.add('商家B', attr, v2, is_convert=True)
bar3.show_config()
bar3.render(path='./data/01-03水平柱形圖.html')
對應(yīng)的圖像如下
折線圖
# 普通折線圖
line = Line('折線圖')
line.add('商家A', attr, v1, mark_point=['max'])
line.add('商家B', attr, v2, mark_point=['min'], is_smooth=True)
line.show_config()
line.render(path='./data/01-04折線圖.html')
# 階梯折線圖
line2 = Line('階梯折線圖')
line2.add('商家A', attr, v1, is_step=True, is_label_show=True)
line2.show_config()
line2.render(path='./data/01-05階梯折線圖.html')
# 面積折線圖
line3 =Line("面積折線圖")
line3.add("商家A", attr, v1, is_fill=True, line_opacity=0.2, area_opacity=0.4, symbol=None, mark_point=['max'])
line3.add("商家B", attr, v2, is_fill=True, area_color='#a3aed5', area_opacity=0.3, is_smooth=True)
line3.show_config()
line3.render(path='./data/01-06面積折線圖.html')
對應(yīng)圖形如下
柱形圖-折線圖
# 柱形圖-折線圖
from pyecharts import Bar, Line, Overlap
att = ['A', 'B', 'C', 'D', 'E', 'F']
v3 = [10, 20, 30, 40, 50, 60]
v4 = [38, 28, 58, 48, 78, 68]
bar = Bar("柱形圖-折線圖")
bar.add('bar', att, v3)
line = Line()
line.add('line', att, v4)
overlap = Overlap()
overlap.add(bar)
overlap.add(line)
overlap.show_config()
overlap.render(path='./data/01-066柱形圖-折線圖.html')
餅圖
# 餅圖
pie = Pie('餅圖')
pie.add('芝麻餅', attr, v1, is_label_show=True)
pie.show_config()
pie.render(path='./data/01-07餅圖.html')
# 玫瑰餅圖
pie2 = Pie("餅圖-玫瑰圖示例", title_pos='center', width=900)
pie2.add("商品A", attr, v1, center=[25, 50], is_random=True, radius=[30, 75], rosetype='radius')
pie2.add("商品B", attr, v2, center=[75, 50], is_random=True, radius=[30, 75], rosetype='area', is_legend_show=False, is_label_show=True)
pie2.show_config()
pie2.render(path='./data/01-08玫瑰餅圖.html')
散點(diǎn)圖
靜態(tài)散點(diǎn)圖
from pyecharts import Scatter
# 散點(diǎn)圖
v1 =[10, 20, 30, 40, 50, 60]
v2 =[10, 20, 30, 40, 50, 60]
scatter =Scatter("散點(diǎn)圖示例")
scatter.add("A", v1, v2)
scatter.add("B", v1[::-1], v2)
scatter.show_config()
scatter.render(path='./data/03-06散點(diǎn)圖.html')
# 散點(diǎn)打印Pyecharts字體 白底圖片
scatter =Scatter("散點(diǎn)圖示例")
v1, v2 = scatter.draw("./data/two.jpg")
scatter.add("pyecharts", v1, v2, is_random=True)
scatter.show_config()
scatter.render(path='./data/03-06打印字體.html')
動態(tài)散點(diǎn)圖
from pyecharts import EffectScatter
attr =["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
v1 =[5, 20, 36, 10, 10, 100]
v2 =[55, 60, 16, 20, 15, 80]
# 動態(tài)散點(diǎn)圖
es =EffectScatter("動態(tài)散點(diǎn)圖")
# v1 x坐標(biāo) v2 y坐標(biāo)
es.add("商家", v1, v2)
es.show_config()
es.render('./data/01-09散點(diǎn)圖.html')
# 動態(tài)散點(diǎn)圖各種圖形
es = EffectScatter("動態(tài)散點(diǎn)圖各種圖形")
es.add("", [10], [10], symbol_size=20, effect_scale=3.5, effect_period=3, symbol="pin")
es.add("", [20], [20], symbol_size=12, effect_scale=4.5, effect_period=4,symbol="rect")
es.add("", [30], [30], symbol_size=30, effect_scale=5.5, effect_period=5,symbol="roundRect")
es.add("", [40], [40], symbol_size=10, effect_scale=6.5, effect_brushtype='fill',symbol="diamond")
es.add("", [50], [50], symbol_size=16, effect_scale=5.5, effect_period=3,symbol="arrow")
es.add("", [60], [60], symbol_size=6, effect_scale=2.5, effect_period=3,symbol="triangle")
es.show_config()
es.render(path = "./data/01-10動態(tài)散點(diǎn)圖各種圖形.html")
圖片為動圖,效果很炫
綜合圖
多個餅圖
from pyecharts import Pie
pie =Pie('各類電影中"好片"所占的比例', "數(shù)據(jù)來著豆瓣", title_pos='center')
pie.add("", ["劇情", ""], [25, 75], center=[10, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None, )
pie.add("", ["奇幻", ""], [24, 76], center=[30, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None, legend_pos='left')
pie.add("", ["愛情", ""], [14, 86], center=[50, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["驚悚", ""], [11, 89], center=[70, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["冒險", ""], [27, 73], center=[90, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["動作", ""], [15, 85], center=[10, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["喜劇", ""], [54, 46], center=[30, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["科幻", ""], [26, 74], center=[50, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["懸疑", ""], [25, 75], center=[70, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["犯罪", ""], [28, 72], center=[90, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None, is_legend_show=True, legend_top="center")
pie.show_config()
pie.render(path='./data/01-多個餅圖.html')
多標(biāo)記柱形圖
from pyecharts import Bar
attr =["{}月".format(i) for i in range(1, 13)]
v1 =[2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
v2 =[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
bar =Bar("柱狀圖示例")
bar.add("蒸發(fā)量", attr, v1, mark_line=["average"], mark_point=["max", "min"])
bar.add("降水量", attr, v2, mark_line=["average"], mark_point=["max", "min"])
bar.show_config()
bar.render(path='./data/02-多標(biāo)記柱形圖.html')
支持保存做種格式
對象.render(path='snapshot.html')
對象.render(path='snapshot.png')
對象.render(path='snapshot.pdf')
舉個栗子:
bar = Bar("我的第一個圖表", "這里是副標(biāo)題")
bar.add("服裝", ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"], [5, 20, 36, 10, 75, 90])
# bar.print_echarts_options()
bar.render(path='snapshot.html')
bar.render(path='snapshot.png')
bar.render(path='snapshot.pdf')
pyecharts繪畫優(yōu)美圖形<一>:最全地圖
pyecharts繪畫優(yōu)美圖形<二>:柱形圖-折線圖-餅圖
pyecharts繪畫優(yōu)美圖形<三>:儀表盤 - 漏斗圖 - 關(guān)系圖 - 水球 - 極坐標(biāo) - 雷達(dá)
pyecharts繪畫優(yōu)美圖形<四>:詞云--支持中文
關(guān)注公眾號:Python瘋子 后臺回復(fù): pyecharts 獲取源代碼
分享最實(shí)用的Python功能,歡迎您的關(guān)注