Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate contour plot from a n x n matrix?

I have a n x n matrix called o_potential filled with some values and would like to generate a contour plot out of it. I tried several approaches without any success. This is what I have right now.

n = 20
x = arange(0,n-1)
y = arange(0,n-1)
plt.contourf(x, y, o_potential[x][y])
plt.show()

o_potential looks like this

o_potential = [[ -1.,          -1.,          -1.,          -1.,          -1.        ],
 [ 10.,           4.12244898,   2.7755102,    4.12244898,  10.        ],
 [ 10.,           5.7755102,    4.24489796,   5.7755102,   10.        ],
 [ 10.,           4.12244898,   2.7755102,    4.12244898,  10.        ],
 [ -1.,          -1.,          -1.,          -1.,          -1.        ]]

I am getting the following error message: "arrays used as indices must be of integer (or boolean) type."

I have been struggling with this from yesterday. I tried almost all the Google results, but couldn't solve the problem. Your help is appreciated!

like image 309
entanglement Avatar asked Nov 05 '25 16:11

entanglement


2 Answers

Try:

n = 20
x = arange(n)
y = arange(n)
X, Y = meshgrid(x, y)
plt.contourf(X, Y, o_potential)
plt.show()

or just

plt.contourf(o_potential)
plt.show()

It's hard to say exactly what the problem is since you don't show o_potential, but the above will likely work.

like image 176
tom10 Avatar answered Nov 07 '25 09:11

tom10


You can try this as well:

import matplotlib.pyplot as plt
plt.imshow(o_potential, cmap='viridis')
like image 20
Sina Avatar answered Nov 07 '25 11:11

Sina



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!