在不使用 imshow 的情况下在 matplotlib 中绘制 2D 数据

发布于 2025-01-10 20:36:56 字数 737 浏览 5 评论 0原文

我想在颜色图上绘制一些 2D 数据,像素均匀分布在坐标 X、Y 上(例如 np.meshgrid 的结果)。有没有办法在没有 imshow() 的情况下在 matplotlib 中执行此操作?

考虑两个 2D 数据字段 Z 和 W,它们具有为坐标 X、Y 定义的值。假设我想将 W 绘制为具有最近值插值的简单颜色图,并将 Z 绘制为一组轮廓。我希望有两个函数可以使用非常相似的调用签名来完成此操作,例如

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.interp2d(X, Y, W)  # plots W with interpolation
ax.contour(X, Y, Z)   # plots Z contours

我想知道是否存在像占位符interp2d这样的函数。

imshow 无法接受 XY,而是需要配置 extentorigin、等等,我觉得很麻烦、不直观,而且可读性较差。

我不介意该函数是否来自 matplotlib 以外的库,只要它可以与 matplotlib 的轴对象交互即可。

I want to plot some 2D data on a colormap, with pixels uniformly distributed over the coordinates X, Y (as would result from e.g. np.meshgrid). Is there a way to do this in matplotlib without imshow()?

Consider two 2D data fields Z and W, which have values defined for the coordinates X, Y. Let's say I want to plot W as a simple colormap with nearest-value interpolation, and to over-plot Z as a set of contours. I would expect there to be two functions to do this with very similar calling signatures, e.g.

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.interp2d(X, Y, W)  # plots W with interpolation
ax.contour(X, Y, Z)   # plots Z contours

I am wondering if a function like the placeholder interp2d exists.

imshow cannot accept X and Y, and instead requires configuring extent, origin, etc. in way that I find cumbersome an unintuitive, and is less readable.

I don't mind if the function is from a library other than matplotlib, as long asit can interface with matplotlib's axis objects.

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

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

发布评论

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

评论(1

谢绝鈎搭 2025-01-17 20:36:56

答案是plt.pcolor。我欣喜若狂,再也不会使用 imshow 了。这就像我在问题中建议的那样工作:

ax.imshow(X, Y, W)

The answer is plt.pcolor. I am overjoyed and will never use imshow again. This works just as I suggested in the question:

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