简体   繁体   中英

Python, plot matrix diagonal elements

I have an 120x70 matrix of which I want to graph diagonal lines.

for ease of typing here, I will explain my problem with a smaller 4x4 matrix.

index 2020 2021 2022 2023
0 1 2 5 7
1 3 5 8 10
0 1 2 5 3
1 3 5 8 4

I now want to graph for example starting at 2021 index 0 so that I get the following diagonal numbers in a graphs: 2, 8, 10

or if I started at 2020 I would get 1, 5, 5, 4.

Kind regards!

You can do this with a simple for-loop. eg:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM