[SOLVED] Plotting value of each node using Python

Issue

I would like to plot the values of matrix A on y-axis as a function of node number on x-axis. However, since I have a 5×5 matrix, I don’t wish to define the node numbers manually. For instance, node 1 corresponds to 2.53734572e-01, node 2 to -1.08940733e-01,…, node 6 to -5.02000098e-01 and so on.

import numpy as np
import matplotlib.pyplot as plt
Node=np.array([[1,2,3,4,5],[6,7,8,9,10]])
A=np.array([[ 2.53734572e-01, -1.08940733e-01,  3.26138649e-03,
        -6.10246692e-03, -2.59115145e-02],
       [-5.02000098e-01,  1.08933714e-01, -3.65540228e-02,
         5.93536044e-03,  3.88767438e-02],
       [-1.42775456e+00,  4.52103243e-01, -2.33067190e-02,
         7.27554880e-03,  1.15638039e-01],
       [ 4.81030592e-01, -8.91302226e-02,  1.40486724e-03,
         2.28801066e-02, -3.83389182e-02],
       [ 8.39965176e-01, -2.81589587e-01,  2.24843962e-01,
        -8.47758268e-03, -6.84721033e-02]])


plt.scatter(Node, A)
plt.xlabel('Node')
plt.ylabel('Velocity')

Solution

We can reduce the matrix to one dimension and use numpy.arange on the length of the matrix:

import numpy as np
import matplotlib.pyplot as plt

ys=np.array([[ 2.53734572e-01, -1.08940733e-01,  3.26138649e-03,
        -6.10246692e-03, -2.59115145e-02],
       [-5.02000098e-01,  1.08933714e-01, -3.65540228e-02,
         5.93536044e-03,  3.88767438e-02],
       [-1.42775456e+00,  4.52103243e-01, -2.33067190e-02,
         7.27554880e-03,  1.15638039e-01],
       [ 4.81030592e-01, -8.91302226e-02,  1.40486724e-03,
         2.28801066e-02, -3.83389182e-02],
       [ 8.39965176e-01, -2.81589587e-01,  2.24843962e-01,
        -8.47758268e-03, -6.84721033e-02]]).flatten()

nodes=np.arange(len(ys))

plt.scatter(nodes, ys)
plt.xlabel('Node')
plt.ylabel('Velocity')
plt.show()

output

Answered By – Christian P

Answer Checked By – Pedro (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *