Python+Matplotlib繪制帶有對角線的散點(diǎn)圖的示例代碼
效果圖
Matplotlib 是一個(gè)用于繪制二維圖形的 Python 庫,提供了一個(gè) pyplot 模塊,用于創(chuàng)建各種類型的圖表。其中一種圖表是散點(diǎn)圖(Scatter Plots),用于展示兩個(gè)變量之間的關(guān)系,以及數(shù)據(jù)的分布情況。要繪制散點(diǎn)圖,使用 pyplot.scatter() 函數(shù),接受以下參數(shù):
x, y:表示數(shù)據(jù)點(diǎn)的橫縱坐標(biāo),可以是浮點(diǎn)數(shù)或者數(shù)組。
s:表示數(shù)據(jù)點(diǎn)的大小,可以是一個(gè)常數(shù)或者一個(gè)數(shù)組,單位是點(diǎn)的平方(1 點(diǎn) = 1/72 英寸)。
c:表示數(shù)據(jù)點(diǎn)的顏色,可以是一個(gè)常數(shù)、一個(gè)數(shù)組、一個(gè)顏色序列或者一個(gè)顏色字符串。如果是一個(gè)數(shù)組,那么會(huì)根據(jù) cmap 參數(shù)來映射顏色;如果是一個(gè)顏色序列,那么長度必須和數(shù)據(jù)點(diǎn)相同;如果是一個(gè)顏色字符串,那么所有數(shù)據(jù)點(diǎn)都使用該顏色。
marker:表示數(shù)據(jù)點(diǎn)的形狀,可以是一個(gè) MarkerStyle 實(shí)例或者一個(gè)簡寫字符串。詳細(xì)的形狀列表可以參考 matplotlib.markers 文檔。
cmap:表示用于映射顏色的 Colormap 實(shí)例或者注冊的顏色圖名稱。如果 c 是 RGB(A) 值,那么該參數(shù)會(huì)被忽略。
norm:表示用于將數(shù)據(jù)標(biāo)準(zhǔn)化到 [0, 1] 區(qū)間的 Normalize 實(shí)例或者名稱。默認(rèn)情況下,使用線性標(biāo)準(zhǔn)化,將最小值映射到 0,最大值映射到 1。如果 c 是 RGB(A) 值,那么該參數(shù)會(huì)被忽略。
vmin, vmax:當(dāng)使用標(biāo)量數(shù)據(jù)并且沒有指定 norm 時(shí),vmin 和 vmax 定義了顏色圖覆蓋的數(shù)據(jù)范圍。默認(rèn)情況下,顏色圖覆蓋了所有數(shù)據(jù)的值域。
主要:
- 設(shè)置主題:
sns.set_theme(style="white")
- 設(shè)置 Figure 尺寸:
fig.set_size_inches(10, 10)
- 繪制對角線:
ax.plot()
- 繪制散點(diǎn)圖:
ax.scatter()
- 繪制標(biāo)注:
ax.annotate()
源碼如下
#!/usr/bin/env python # -- coding: utf-8 -- """ Copyright (c) 2022. All rights reserved. Created by C. L. Wang on 2023/6/25 """ import os import seaborn as sns sns.set_theme(style="white") import matplotlib.pyplot as plt import numpy as np from myutils.project_utils import read_excel_to_df from root_dir import DATA_DIR def draw_diagonal_scatter_plots( data_better, data_ref, label_list, min_scale=0.0, max_scale=1.05, x_label="", y_label="", save_name="" ): """ 繪制對角線散點(diǎn)圖 :param data_better: 優(yōu)質(zhì)數(shù)據(jù),數(shù)據(jù)位于右下方 :param data_ref: 對比數(shù)據(jù) :param label_list: 標(biāo)簽 :param min_scale: 最小范圍 :param max_scale: 最大范圍 :param x_label: x軸描述 :param y_label: y軸描述 :param save_name: 文件存儲(chǔ) :return: 圖 """ assert len(data_ref) == len(data_better) == len(label_list) fig, ax = plt.subplots() fig.set_size_inches(10, 10) ax.grid(True) ax.plot([min_scale, max_scale], [min_scale, max_scale], ls="--", c=".3") ax.scatter(data_ref, data_better, s=100, edgecolors="black") plt.xlim(min_scale, max_scale) plt.ylim(min_scale, max_scale) plt.xticks(fontsize=15) plt.yticks(fontsize=15) plt.xlabel(x_label, fontsize=20) plt.ylabel(y_label, fontsize=20) for i, txt in enumerate(label_list): if data_ref[i] - data_better[i] > 0.025: ax.annotate(txt, (data_ref[i] * 1.03, data_better[i]), fontsize=10, fontweight='bold') if save_name: # transparent=True assert save_name.endswith("png") or save_name.endswith("jpg") plt.savefig(save_name, bbox_inches='tight', format='png') plt.show() def main(): df = read_excel_to_df(os.path.join(DATA_DIR, "Strategy-v1v2v3v4-TMScore.xls")) data1 = df["m0-score"] data2 = df["max-score"] label_list = df["target"] print(f"data1 : {round(float(np.mean(data1)) * 100, 4)}±{round(float(np.std(data1)), 4)}") print(f"data2 : {round(float(np.mean(data2)) * 100, 4)}±{round(float(np.std(data2)), 4)}") draw_diagonal_scatter_plots(data1, data2, label_list, min_scale=0.35, max_scale=1.05, x_label="Our P.S.P. (TMScore)", y_label="CASP15 SOTA (TMScore)", save_name="img.png") if __name__ == '__main__': main()
到此這篇關(guān)于Python+Matplotlib繪制帶有對角線的散點(diǎn)圖的示例代碼的文章就介紹到這了,更多相關(guān)Python Matplotlib繪制散點(diǎn)圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
對python cv2批量灰度圖片并保存的實(shí)例講解
今天小編就為大家分享一篇對python cv2批量灰度圖片并保存的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11Qt實(shí)現(xiàn)炫酷啟動(dòng)圖動(dòng)態(tài)進(jìn)度條效果
最近接到一個(gè)新需求,讓做一個(gè)動(dòng)效進(jìn)度條。剛接手這個(gè)項(xiàng)目真的不知所措,后來慢慢理清思路,問題迎刃而解,下面小編通過本文給大家?guī)砹薗t實(shí)現(xiàn)炫酷啟動(dòng)圖動(dòng)態(tài)進(jìn)度條效果,感興趣的朋友一起看看吧2021-11-11Python腳本提取fasta文件單序列信息實(shí)現(xiàn)
這篇文章主要為大家介紹了Python腳本提取fasta文件單序列信息實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07python實(shí)現(xiàn)簡單日志記錄庫glog的使用
這篇文章主要介紹了python實(shí)現(xiàn)簡單日志記錄庫glog的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12python的格式化輸出(format,%)實(shí)例詳解
Python中格式化字符串目前有兩種陣營:%和format,哪一種比較適合我們使用呢?下面腳本之家小編給大家介紹下python的格式化輸出(format,%)實(shí)例詳解,感興趣的朋友一起看看吧2018-06-06