日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

pyecharts如何實現顯示數據為百分比的柱狀圖_python

作者:陳年椰子 ? 更新時間: 2022-12-04 編程語言

pyecharts顯示數據為百分比的柱狀圖

pyecharts是做數據分析的好幫手,柱狀圖比較簡單,網站例子不夠多,一般柱狀圖就是直接傳兩組數據就搞掂了,如果想要顯示數據為百分比,比如下圖例子。

需要做兩處調整。

1、Y軸內容

2、標簽內容

查了文檔,都是修改opts.LabelOpts 。

做了個例子,供大家參考

# encoding: utf-8
"""
@author: seakingx
@contact: hndm@qq.com
@version: 1.0
@file: doex.py
@time: 2020/3/27 0019 09:39
說明 建立百分比的柱狀圖
"""
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType
 
def create_bar(bar_dict):
    # 建立百分比的柱狀圖
    bar_item = bar_dict['item']
    bar_head = bar_dict['head']
    bar_data = bar_dict['data']
    bar = (
        Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
            .add_xaxis(bar_item)
            .set_global_opts(title_opts=opts.TitleOpts(title="銷售情況", subtitle="占比情況"))
    )
    for i in range(len(bar_head)):
        bar.add_yaxis(bar_head[i], bar_data[i], label_opts=opts.LabelOpts(formatter="{c} %"))
    bar.set_global_opts(
            yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(formatter="{value} %"), interval=10))
    return bar
 
 
def get_data_dict():
    # 這里獲取要顯示的數據 , 可以改成連接數據庫
    data_a = [round(n*100,2) for n in [0.2155, 0.423, 0.351, 0.4422, 0.651, 0.722]]
    data_b = [round(n*100,2) for n in [0.1233, 0.231, 0.4522, 0.5612, 0.6667, 0.745]]
    pdt_list = ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
    data_dict= {'data':[data_a,data_b], 'head':['商家甲','商家乙'], 'item':pdt_list}
    return data_dict
 
 
if __name__=="__main__":
    data = get_data_dict()
    bar = create_bar(data)
    bar.render()

Echarts之顯示百分比問題

對于使用echarts要顯示百分比,要改兩個地方,第一個地方時坐標軸顯示為百分比的格式,第二個是讓值以百分比的形式顯示,如50,在圖上面顯示為50%。

yAxis: [ ?
? ? ? ? { ?
? ? ? ? ? ? type: 'value', ?
? ? ? ? ? ? axisLabel: { ?
? ? ? ? ? ? ? ? ? show: true, ?
? ? ? ? ? ? ? ? ? interval: 'auto', ?
? ? ? ? ? ? ? ? ? formatter: '{value} %' ?
? ? ? ? ? ? ? ? }, ?
? ? ? ? ? ? show: true ?
? ? ? ? } ?
? ? ], ?

第二個是改series

? itemStyle: { ?
? ? ? ? ? ? ? ? normal: { ?
? ? ? ? ? ? ? ? ? ? label: { ?
? ? ? ? ? ? ? ? ? ? ? ? show: true, ?
? ? ? ? ? ? ? ? ? ? ? ? position: 'top', ?
? ? ? ? ? ? ? ? ? ? ? ? formatter: '\n{c}%' ?
? ? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? }, ?

原文鏈接:https://blog.csdn.net/seakingx/article/details/105135110

欄目分類
最近更新