从MATLAB到Python等效 [英] equivalent from MATLAB to Python
问题描述
我刚开始使用Python,我将把这个例子从MATLAB转换为Python,但是我没有在Python中找到等效的东西.
I just have started with Python and I would translate this example from MATLAB to Python, but I have not found the equivalent in Python.
https://www.mathworks.com/help/matlab/ref /surface.html
load clown
surface(peaks,flipud(X),...
'FaceColor','texturemap',...
'EdgeColor','none',...
'CDataMapping','direct')
colormap(map)
view(-35,45)
谢谢!
推荐答案
Matplotlib提供了Matlab几乎所有的绘图选项.也可以完成表面绘图: http://matplotlib.org/mpl_toolkits/mplot3d /tutorial.html#surface-plots
Matplotlib offers nearly all plotting options Matlab does. Surface plots can be done as well: http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#surface-plots
要加载图像,scipy有一个PIL包装器(不包括小丑,对不起),它可以加载与matplotlib兼容的numpy数组.
To load images scipy has a PIL-wrapper (no clown included, sorry), which loads matplotlib-compatible numpy arrays.
总而言之,您需要以下软件包:numpy,scipy,matplotlib和PIL.这四个库的组合应该可以满足您的所有需求.还请检查这些库的pylab接口,因为它与Matlab非常相似.
To sum up, you want the following packages: numpy, scipy, matplotlib and PIL. The combination of those four libraries should give you all you need. Also check out the pylab interface of these libraries, as it is very similar to Matlab.
可以完成我认为您想做的事的示例:
Example that does what I believe you want to do:
from mpl_toolkits.mplot3d import Axes3D
from scipy.misc import imread
from matplotlib.pyplot import figure, show
from numpy import linspace, meshgrid, sqrt, sin, mean, flipud
clown = imread('clown.png')
fig = figure()
ax = fig.gca(projection='3d')
X = linspace(-5, 5, clown.shape[0])
Y = linspace(-5, 5, clown.shape[1])
X, Y = meshgrid(X, Y)
R = sqrt(X**2 + Y**2)
Z = sin(R)
clown = clown.swapaxes(0,1) / 255. # meshgrid orients axes the other way around, scaling of rgb to [0-1]
ax.plot_surface(X, Y, Z, facecolors=flipud(clown))
ax.view_init(45,-35) # swapped wrt matlab
show()
这篇关于从MATLAB到Python等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!