equivalent from MATLAB to Python

↘锁芯ラ 提交于 2019-12-11 19:14:13

问题


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)

Thanks!


回答1:


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

To load images scipy has a PIL-wrapper (no clown included, sorry), which loads matplotlib-compatible numpy arrays.

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()


来源:https://stackoverflow.com/questions/17142979/equivalent-from-matlab-to-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!