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

Python使用matplotlib.pyplot畫熱圖和損失圖的代碼詳解

 更新時間:2023年09月19日 10:53:23   作者:童話ing  
眾所周知,在完成論文相關(guān)工作時畫圖必不可少,如損失函數(shù)圖、熱力圖等是非常常見的圖,在本文中,總結(jié)了這兩個圖的畫法,下面給出了完整的代碼,開箱即用,感興趣的同學可以自己動手嘗試一下

一、損失函數(shù)圖

import matplotlib.pyplot as plt
file = open('E:\\5120154230PythonCode\\PBAN-PyTorch-master\\state_dict\\loss\\PBAN_New_restaurant15_0.001_80_0.2_16.csv')  # 打開文檔
data = file.readlines()  # 讀取文檔數(shù)據(jù)
para_1 = []  # 新建列表,用于保存第一列數(shù)據(jù)
para_2 = []  # 新建列表,用于保存第二列數(shù)據(jù)
cnt = 0
for num in data:
    try:
        temp = num.split(",")
        cnt += 1
        if cnt==700:
            break
    except:
        continue
    para_1.append(float(num.split(',')[0]))
    para_2.append(float(num.split(',')[1]))
plt.figure()
# plt.title('loss')
plt.xlabel("iterations")
plt.ylabel("loss")
#color in cnblogs.com/qccc/p/12795541.html
#orange、teal、red、chocolate
plt.plot(para_1, para_2)
plt.show()

CSV數(shù)據(jù)格式:第一列為Epoch或者迭代次數(shù)等,第二列為損失值。

效果圖:

二、熱圖

import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.ticker as ticker
d = [
 [0.43757705, 0.30564879, 0.08757705, 0.013755781, 0.13755781, 0.04080211, 0.03615228],
 [0.31525328, 0.42328909, 0.04004493, 0.01735733, 0.01755249, 0.02630009, 0.09020273],
 [0.01546572, 0.09022246, 0.4166335, 0.09773314, 0.10259592, 0.0447391, 0.03261019],
 [0.01536734, 0.010553601, 0.045800883, 0.39755909, 0.1465714, 0.0408309, 0.03612638],
 [0.11513351, 0.01193435, 0.051866556, 0.046714543, 0.42510962, 0.03154159, 0.4848393],
 [0.11544053, 0.0941444, 0.050161916, 0.09768857, 0.11385846, 0.43073818, 0.13351071],
 [0.01529034, 0.07752335, 0.04121181, 0.01742287, 0.35099512, 0.03777161, 0.38087882]
]
variables = ['Great', 'food', 'but', 'the', 'service', 'was', 'dreadful']
labels = ['Great', 'food', 'but', 'the', 'service', 'was', 'dreadful']
df = pd.DataFrame(d, columns=variables, index=labels)
fig = plt.figure(figsize=(7, 6)) #寬、高
ax = fig.add_subplot(1, 1, 1) #畫布設(shè)置為1行1列顯示在第一塊中
# cmap參考:https://matplotlib.org/2.0.2/users/colormaps.html
# hot_r、afmhot_r、plasma_r、ocean_r
# interpolation:nearest,None、none
cax = ax.matshow(df, interpolation='nearest', cmap='hot_r')
fig.colorbar(cax)
tick_spacing = 1
ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
ax.yaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
ax.set_xticklabels([''] + list(df.columns))
ax.set_yticklabels([''] + list(df.index))
plt.show()

效果:

另一份代碼:

import matplotlib.pylab as plt
import numpy as np
def samplemat(dims):
    aa = np.zeros(dims)
    for i in range(dims[1]):
            aa[0,i] = i
    return aa
dimlist = [(1, 12)]
for d in dimlist:
    arr = samplemat(d)
    plt.matshow(arr)
plt.show()

以上就是Python使用matplotlib.pyplot畫熱圖和損失圖的代碼詳解的詳細內(nèi)容,更多關(guān)于Python matplotlib.pyplot畫圖的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python處理圖片之PIL模塊簡單使用方法

    python處理圖片之PIL模塊簡單使用方法

    這篇文章主要介紹了python處理圖片之PIL模塊簡單使用方法,涉及Python使用PIL模塊實現(xiàn)針對圖片的銳化、繪制直線、繪制橢圓等相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • 詳解Python中生成隨機數(shù)據(jù)的示例詳解

    詳解Python中生成隨機數(shù)據(jù)的示例詳解

    在日常工作編程中存在著各種隨機事件,同樣在編程中生成隨機數(shù)字的時候也是一樣。每當在?Python?中生成隨機數(shù)據(jù)、字符串或數(shù)字時,最好至少大致了解這些數(shù)據(jù)是如何生成的。所以本文將詳細為大家講解一下Python是如何生成隨機數(shù)據(jù),需要的可以參考一下
    2022-04-04
  • 再談Python中的字符串與字符編碼(推薦)

    再談Python中的字符串與字符編碼(推薦)

    這篇文章主要介紹了再談Python中的字符串與字符編碼(推薦),具有一定的參考價值,有需要的可以了解一下。
    2016-12-12
  • Python 自動化表單提交實例代碼

    Python 自動化表單提交實例代碼

    今天以一個表單的自動提交,來進一步學習selenium的用法,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2017-06-06
  • python之如何進行去重問題

    python之如何進行去重問題

    這篇文章主要介紹了python之如何進行去重問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 解決遇到:PytorchStreamReader failed reading zip archive:failed finding central錯誤問題

    解決遇到:PytorchStreamReader failed reading zip&n

    本文針對"PytorchStreamReaderfailedreadingziparchive:failedfindingcentral"錯誤提出解決方案,包括檢查文件完整性、文件路徑,嘗試更新PyTorch版本,檢查壓縮文件格式,代碼問題,或?qū)で蠹夹g(shù)支持等,希望這些經(jīng)驗?zāi)芙o遇到同樣問題的人一個參考
    2024-09-09
  • Python lambda表達式原理及用法解析

    Python lambda表達式原理及用法解析

    這篇文章主要介紹了Python lambda表達式原理及用法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • Django如何在不停機的情況下創(chuàng)建索引

    Django如何在不停機的情況下創(chuàng)建索引

    在本篇內(nèi)容里小編給大家整理的是關(guān)于Django如何在不停機的情況下創(chuàng)建索引的相關(guān)文章,有興趣的朋友們參考學習下。
    2020-08-08
  • matplotlib 縱坐標軸顯示數(shù)據(jù)值的實例

    matplotlib 縱坐標軸顯示數(shù)據(jù)值的實例

    今天小編就為大家分享一篇matplotlib 縱坐標軸顯示數(shù)據(jù)值的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • Python工具腳本調(diào)用外層模塊的操作方法

    Python工具腳本調(diào)用外層模塊的操作方法

    今天有同學問我,這種情況應(yīng)該怎么調(diào)用,才能讓remove_outdated_data.py正確導(dǎo)入models里面的模塊,下面通過本文介紹下Python工具腳本調(diào)用外層模塊的方法,感興趣的朋友一起看看吧
    2024-02-02

最新評論