Python+Matplotlib繪制小提琴圖的示例代碼
小提琴圖 (Violin Plot) 類似紡錘,小提琴圖是一種用來顯示數(shù)據(jù)分布和概率密度的圖形,結(jié)合了箱線圖和核密度圖的特點。小提琴圖的中間部分是一個箱線圖,顯示了數(shù)據(jù)的中位數(shù)、四分位數(shù)和異常值。小提琴圖的兩側(cè)是一個核密度圖,顯示了數(shù)據(jù)的分布形狀。小提琴圖可以用來比較不同類別或分組的數(shù)據(jù),展示數(shù)據(jù)的差異和相似性。
示例如下:

源碼如下:
#!/usr/bin/env python
# -- coding: utf-8 --
"""
Copyright (c) 2022. All rights reserved.
Created by C. L. Wang on 2023/6/6
"""
import os
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from myutils.project_utils import read_excel_to_df
from root_dir import DATA_DIR
def draw_violin_plot(
df, score_col_name, label_col_name,
x_label="", y_label="", title="",
is_show=False, save_name=""):
"""
繪制小提琴圖
:param df: 數(shù)據(jù)格式,至少包括兩列,即數(shù)據(jù)值列score,標簽列l(wèi)abel,相同類別的標簽相同
:param score_col_name: 數(shù)值列名稱
:param label_col_name: 標簽列名稱
:param x_label: 顯示的x標簽
:param y_label: 顯示的y標簽
:param title: 顯示的圖名稱
:param is_show: 是否IDE顯示
:param save_name: 是否存儲文件,tight格式
:return:
"""
plt.figure(figsize=(16, 10), dpi=80)
sns.violinplot(x=label_col_name, y=score_col_name, data=df, inner='quartile')
plt.xlabel(xlabel=x_label)
plt.ylabel(ylabel=y_label)
if title:
plt.title(title, fontsize=12)
if save_name:
# transparent=True
plt.savefig(save_name, bbox_inches='tight', format='png')
if is_show:
plt.show()
def main():
df = read_excel_to_df(os.path.join(DATA_DIR, "ourbest_20230601_tmscore_56.xls"))
# df = read_excel_to_df(os.path.join(DATA_DIR, "ourbest_20230605_dockq_9_final.xls"))
# df.info()
# 加工數(shù)據(jù)格式,滿足數(shù)值和標簽的格式
score1 = df["m0-score"]
score2 = df["m1-score"]
score3 = df["cur-score"]
label_col = ["SOTA" for _ in range(df["m0-score"].size)] + \
["Baseline" for _ in range(df["m1-score"].size)] + \
["Our Best" for _ in range(df["cur-score"].size)]
score_list = pd.concat([score1, score2, score3])
score_name = "DockQ"
data = {score_name: score_list, "label": label_col}
new_df = pd.DataFrame(data)
# df.info()
draw_violin_plot(new_df, score_col_name=score_name, label_col_name="label",
x_label="", y_label=score_name,
is_show=True, save_name="xxx.png")
if __name__ == '__main__':
main()
到此這篇關(guān)于Python+Matplotlib繪制小提琴圖的示例代碼的文章就介紹到這了,更多相關(guān)Python Matplotlib繪制小提琴圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python爬蟲:將headers請求頭字符串轉(zhuǎn)為字典的方法
今天小編就為大家分享一篇Python爬蟲:將headers請求頭字符串轉(zhuǎn)為字典的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
關(guān)于 Python json中l(wèi)oad和loads區(qū)別
這篇文章主要介紹了關(guān)于 Python json中l(wèi)oad和loads區(qū)別,文章也有簡單的說明它們之間的相同點,然后詳細介紹不同點,需要的朋友可以參考一下文章的具體內(nèi)容2021-11-11
三步快速實現(xiàn)Python解包EXE文件獲取源碼的完整教學
你是否曾經(jīng)遇到過這樣的情況,需要分析一個Python打包的可執(zhí)行文件,但卻無法獲取原始代碼,Python EXE解包技術(shù)正是解決這個問題的關(guān)鍵,下面我們就來看看Python解包exe的具體步驟吧2026-01-01
python pandas.DataFrame.loc函數(shù)使用詳解
這篇文章主要介紹了python pandas.DataFrame.loc函數(shù)使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03

