如何在Matplotlib(Numpy)中产生MATLAB plot(interpolation)?

编程入门 行业动态 更新时间:2024-10-18 18:14:14
本文介绍了如何在Matplotlib(Numpy)中产生MATLAB plot(interpolation)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试遵循MATLAB网格+插值示例.可以在 HERE

I am trying to follow a MATLAB example of meshgrid + interpolation. The example code is found HERE. On that site, I am going through the following example: Example – Displaying Nonuniform Data on a Surface.

现在,我想在Python(Numpy + Matplotlib)中生成与MATLAB中所示相似的图.这是MATLAB生成的图:

Now, I would like to produce a similar plot in Python (Numpy + Matplotlib) to what is shown there in MATLAB. This is the plot that MATLAB produces:

我在Python中执行此操作时遇到了麻烦.这是我的代码和我在Python 2.7中的输出:

I am having trouble with doing this in Python. Here is my code and my output in Python 2.7:

from matplotlib.mlab import griddata import matplotlib.pyplot as plt from matplotlib import cm import numpy as np from mpl_toolkits.mplot3d import Axes3D x = np.random.rand(200)*16 - 8 y = np.random.rand(200)*16 - 8 r = np.sqrt(x**2 + y**2) z = np.sin(r)/r xi = np.linspace(min(x),max(x), 100) yi = np.linspace(min(y),max(y), 200) X,Y = np.meshgrid(xi,yi) Z = griddata(x, y, z, X, Y, interp='linear') fig = plt.figure() ax = fig.gca(projection='3d') surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1,cmap=cm.jet)

这是我尝试使用matplotlib和NumPy进行操作的结果.

Here is the result of my attempt at doing this with matplotlib and NumPy..

有人可以帮我在matplotlib中以网格图或曲面图的形式重新创建MATLAB图吗?

Could someone please help me recreate the MATLAB plot in matplotlib, as either a mesh or a surface plot?

推荐答案

因此,外观上的主要差异似乎与matlab绘制的默认线数有关,可以通过增加rstride和cstride.在颜色方面,为了正确缩放颜色图,在这种情况下最好设置极限值vmin和vmax,因为自动设置时,它将使用Z的最小值和最大值,但是在在这种情况下,它们都是nan,因此您可以使用np.nanmin和np.nanmax.

So it seems that the major differences in the look have to do with the default number of lines plotted by matlab, which can be adjusted by increasing rstride and cstride. In terms of color, in order for the colormap to be scaled properly it is probably best in this case to set your limits, vmin and vmax because when automatically set, it will use the min and max of Z, but in this case, they are both nan, so you could use np.nanmin and np.nanmax.

from matplotlib.mlab import griddata import matplotlib.pyplot as plt from matplotlib import cm import numpy as np from mpl_toolkits.mplot3d import Axes3D x = np.random.rand(200)*16 - 8 y = np.random.rand(200)*16 - 8 r = np.sqrt(x**2 + y**2) z = np.sin(r)/r xi = np.linspace(min(x),max(x), 100) yi = np.linspace(min(y),max(y), 200) X,Y = np.meshgrid(xi,yi) Z = griddata(x, y, z, X, Y, interp='linear') fig = plt.figure() ax = fig.gca(projection='3d') surf = ax.plot_surface(X, Y, Z, rstride=5, cstride=5, cmap=cm.jet, vmin=np.nanmin(Z), vmax=np.nanmax(Z), shade=False) scat = ax.scatter(x, y, z)

不幸的是,在matplotlib中,我遇到了一些令人讨厌的重叠/剪切"问题,其中Axes3d并不总是正确地确定对象的显示顺序.

In matplotlib unfortunately I get some annoying overlapping/'clipping' problems, where Axes3d doesn't always properly determine the order in which object should be displayed.

更多推荐

如何在Matplotlib(Numpy)中产生MATLAB plot(interpolation)?

本文发布于:2023-11-02 17:10:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552954.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Matplotlib   Numpy   interpolation   plot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!