欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python+Matplotlib繪制高亮顯示餅圖的示例代碼

 更新時間:2023年06月08日 11:12:44   作者:SpikeKing  
餅圖 (Pie Chart) 是一種圓形統(tǒng)計圖,被分割成片用于表示數(shù)值間的比例關(guān)系,本文為大家介紹了Matplotlib繪制高亮顯示的餅圖的函數(shù)源碼,需要的可以參考一下

餅圖 (Pie Chart) 是一種圓形統(tǒng)計圖,被分割成片用于表示數(shù)值間的比例關(guān)系。每個切片的弧長以及相應(yīng)的中心角和面積與其表示的量成正比。餅圖適合用于展示構(gòu)成、占比、份額等數(shù)據(jù)。

示例如下:

源碼如下:

其中,高亮顯示使用 plt.pie() 函數(shù)的 explode 參數(shù),默認(rèn)是全0列表,當(dāng)設(shè)置超過0時,如0.05,則當(dāng)前位置是高亮顯示。

def draw_pie_chart(
    val_list, label_list, highlight_idx=-1,
    title="", xlabel="", ylabel="", figsize=(8, 8), font_scale=1.2,
    is_show=False, save_name="",
    **plot_kwargs,
):
    """
    繪制餅圖

    :param val_list: 待處理數(shù)據(jù)
    :param label_list: 待處理數(shù)據(jù)的標(biāo)簽,
    :param highlight_idx: 高亮顯示區(qū)域
    :param title: 圖表標(biāo)題
    :param xlabel: X 軸標(biāo)簽
    :param ylabel: Y 軸標(biāo)簽
    :param figsize: 圖像尺寸
    :param font_scale: 字體縮放尺寸
    :param is_show: 是否顯示
    :param save_name:  是否存儲圖像
    :param plot_kwargs:  其余參數(shù)
    :return:
    """

    assert len(val_list) == len(label_list)
    assert highlight_idx < len(val_list)

    sns.set(font_scale=font_scale)
    plt.figure(figsize=figsize)
    explode_list = [0.0 for _ in range(len(val_list))]

    # 高亮顯示區(qū)域
    if highlight_idx >= 0:
        highlight_idx = min(highlight_idx, len(explode_list) - 1)
        explode_list[highlight_idx] = 0.05

    # 繪制餅圖
    plt.pie(
        x=val_list,
        labels=label_list,
        autopct='%1.2f%%',
        colors=sns.color_palette('Set2'),
        # Add space around each slice
        explode=explode_list,
        ** plot_kwargs,
    )

    plt.title(title)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)

    if save_name:
        # transparent=True
        assert save_name.endswith("png") or save_name.endswith("jpg")
        plt.savefig(save_name, bbox_inches='tight', format='png')
    if is_show:
        plt.show()

    return plt.gcf()

def main():
    data_list = [11, 11, 7, 26]
    label_list = ["MSA Update", "Model Diversity", "MSA Ranking", "Unified Prediction"]
    draw_pie_chart(data_list, label_list, highlight_idx=0, is_show=True, save_name="xxx.png")

到此這篇關(guān)于Python+Matplotlib繪制高亮顯示餅圖的示例代碼的文章就介紹到這了,更多相關(guān)Python Matplotlib餅圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論