亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何 hist() 使用 Matplotlib 繪制二維 NumPy 數組的每個數據數組行?

如何 hist() 使用 Matplotlib 繪制二維 NumPy 數組的每個數據數組行?

精慕HU 2023-04-18 15:53:18
我有一個 3x10 2d ndarray,我想做一個 matplotlib 歷史圖。我想要一個子圖中每個數組行的歷史圖。我嘗試直接提供 ndarray,但發現 matplotlib 會提供 ndarray 每一列的直方圖,這不是我想要的。我怎樣才能實現我的目標?目前,我必須明確聲明hist()每一行的命令,我寧愿避免這種方法。import numpy as npimport matplotlib.pyplot as pltd = np.array([[1, 2, 2, 2, 3, 1, 3, 1,   2,  4, 5],              [4, 4, 5, 5, 3, 6, 6,  7,   6,  5, 7],              [5, 6, 7, 7, 8, 8, 9, 10, 11, 12, 10]] )print( '\nd', d )             fig, ax = plt.subplots(4, 1)dcount, dbins, dignored = ax[0].hist( d, bins=[2, 4, 6, 8, 10, 12], histtype='bar', label='d' )d0count, d0bins, d0ignored = ax[1].hist( d[0,:], bins=[2, 4, 6, 8, 10, 12], histtype='bar', label='d0', alpha=0.2 )d1count, d1bins, d1ignored = ax[2].hist( d[1,:], bins=[2, 4, 6, 8, 10, 12], histtype='bar', label='d1', alpha=0.2 )d2count, d2bins, d2ignored = ax[3].hist( d[2,:], bins=[2, 4, 6, 8, 10, 12], histtype='bar', label='d2', alpha=0.2 )ax[0].legend()ax[1].legend()ax[2].legend()ax[3].legend()print( '\ndcount', dcount )print( '\ndbins', dbins )print( '\ndignored', dignored )print( '\nd0count', d0count )print( '\nd0bins', d0bins )print( '\nd0ignored', d0ignored )print( '\nd1count', d0count )print( '\nd1bins', d0bins )print( '\nd1ignored', d0ignored )print( '\nd2count', d0count )print( '\nd2bins', d0bins )print( '\nd2ignored', d0ignored )plt.show()
查看完整描述

2 回答

?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

# import needed packages

import numpy as np

import matplotlib.pyplot as plt

創建要繪制的數據

使用列表理解和numpy.random.normal


gaussian0=[np.random.normal(loc=0, scale=1.5) for _ in range(100)]

gaussian1=[np.random.normal(loc=2, scale=0.5) for _ in range(100)]


gaussians = [gaussian0, gaussian1]

僅用一次歷史調用進行繪圖

for gaussian in gaussians:

? ? plt.hist(gaussian,alpha=0.5)

plt.show()

導致:

http://img2.sycdn.imooc.com/643e4cbc000187f103670247.jpg

查看完整回答
反對 回復 2023-04-18
?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

我找到了一個更簡單的方法。轉置d。也就是說,更換

dcount, dbins, dignored = ax[0].hist( d, bins=[2, 4, 6, 8, 10, 12], histtype='bar', label='d' )

dcount, dbins, dignored = ax[0].hist( d.T, bins=[2, 4, 6, 8, 10, 12], histtype='bar', label=['d0', 'd1','d2'], alpha=0.5 )

http://img1.sycdn.imooc.com//643e4cd00001083c04570291.jpg

我希望 matplotlib 的hist()命令會有一些命令來執行它,但沒有找到它。轉置 numpy 數組有效。我想知道這是否是 matplotlib 用戶通常這樣做的方式?


查看完整回答
反對 回復 2023-04-18
  • 2 回答
  • 0 關注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號