Python+Matplotlib繪制高亮顯示餅圖的示例代碼
餅圖 (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)文章
Python 多線程并行執(zhí)行的實(shí)現(xiàn)示例
本文主要介紹了Python 多線程并行執(zhí)行的實(shí)現(xiàn)示例,通過使用threading和concurrent.futures模塊可以進(jìn)行實(shí)現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-07-07python使用點(diǎn)操作符訪問字典(dict)數(shù)據(jù)的方法
這篇文章主要介紹了python使用點(diǎn)操作符訪問字典(dict)數(shù)據(jù)的方法,涉及Python操作字典的技巧,需要的朋友可以參考下2015-03-03pytorch之torch.flatten()和torch.nn.Flatten()的用法
這篇文章主要介紹了pytorch之torch.flatten()和torch.nn.Flatten()的用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04python遺傳算法之單/多目標(biāo)規(guī)劃問題
本文主要介紹了python遺傳算法之單/多目標(biāo)規(guī)劃問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Pandas實(shí)現(xiàn)復(fù)制dataframe中的每一行
這篇文章主要介紹了Pandas實(shí)現(xiàn)復(fù)制dataframe中的每一行方式,2024-02-02win10子系統(tǒng)python開發(fā)環(huán)境準(zhǔn)備及kenlm和nltk的使用教程
這篇文章主要介紹了win10子系統(tǒng)python開發(fā)環(huán)境準(zhǔn)備及kenlm和nltk的使用教程,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10