[英]Python, plot matrix diagonal elements
我有一個 120x70 的矩陣,我想在其中繪制對角線。
為了方便在這里打字,我將用一個較小的 4x4 矩陣來解釋我的問題。
指數 | 2020 | 2021 | 2022 | 2023 |
---|---|---|---|---|
0 | 1 | 2 | 5 | 7 |
1 | 3 | 5 | 8 | 10 |
0 | 1 | 2 | 5 | 3 |
1 | 3 | 5 | 8 | 4 |
例如,我現在想從 2021 索引 0 開始繪制圖表,以便在圖表中獲得以下對角線數字:2、8、10
或者如果我從 2020 年開始,我會得到 1、5、5、4。
親切的問候!
你可以用一個簡單的 for 循環來做到這一點。 例如:
matrix = np.array((120, 70))
graph_points = []
column_index = 0 # Change this to whatever column you want to start at
for i in range(matrix.shape[0]):
graph_points.append(matrix[i, column_index])
column_index += 1
if column_index >= matrix.shape[1]:
break
## Plot graph_points here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.