在不使用 imshow 的情况下在 matplotlib 中绘制 2D 数据
我想在颜色图上绘制一些 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
无法接受 X
和 Y
,而是需要配置 extent
、origin
、等等,我觉得很麻烦、不直观,而且可读性较差。
我不介意该函数是否来自 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是
plt.pcolor
。我欣喜若狂,再也不会使用imshow
了。这就像我在问题中建议的那样工作:The answer is
plt.pcolor
. I am overjoyed and will never useimshow
again. This works just as I suggested in the question: