如何从 anxn 矩阵生成等值线图?

发布于 2024-12-04 08:39:49 字数 753 浏览 0 评论 0原文

我有一个名为 o_pottial 的 xn 矩阵,其中填充了一些值,并希望从中生成等值线图。我尝试了几种方法但没有成功。这就是我现在所拥有的。

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

o_pottial 看起来像这样

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.        ]]

我收到以下错误消息:“用作索引的数组必须是整数(或布尔)类型。”

从昨天开始我就一直在为此苦苦挣扎。我尝试了几乎所有的Google结果,但无法解决问题。感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新人笑 2024-12-11 08:39:49

尝试:

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

或者只是

plt.contourf(o_potential)
plt.show()

很难确切地说问题是什么,因为您没有显示 o_pottial,但上面的方法可能会起作用。

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.

再浓的妆也掩不了殇 2024-12-11 08:39:49

你也可以尝试这个:

import matplotlib.pyplot as plt
plt.imshow(o_potential, cmap='viridis')

You can try this as well:

import matplotlib.pyplot as plt
plt.imshow(o_potential, cmap='viridis')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文