带有Pyplot的平滑表面图 [英] Smooth surface Plot with Pyplot
问题描述
我的问题几乎与此类似: 来自矩阵的平滑表面图
My question is almost similar to this on: smoothing surface plot from matrix
仅我的工具集是matplotlib和numpy(到目前为止).
only that my toolset is matplotlib and numpy (so far).
我已经成功生成了X,Y和Z网格以进行绘制 与
I have sucessfully generated a X, Y and Z-grid to plot with
fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap='summer', rstride=1, cstride=1, alpa=None)
但是,由于这些值非常跳跃,因此看起来非常糟糕.
However, as the values are quite jumpy, it looks terribly.
我想使事情变得平滑,至少使顶点连接或看起来像那样.
I'd like to smoothen things up, make at least the vertices connected, or look like that.
我的数据是这样生成的: 我有一个功能
My data is generated like that: I have a function
svOfMatrix(x, y)
根据x产生矩阵,计算其y次幂,选择列和行的子集,并计算最大奇异值. 因此,Z [x,y]是svOfMatrix(x,y)
which produces a matrix in dependence on x, calculates its y-th power, selects a subset of columns and rows, and calculates the maximum singular value. So, Z[x,y] is svOfMatrix(x, y)
由于此计算非常昂贵,我不想使x的步长太小,而Y必然是整数
此外,即使是很小的步骤,也可能会有一些变化,我不想看到.因此,我想以某种方式进行插值.
我发现
http://docs.scipy.org/doc/scipy-0.14.0/reference/tutorial/interpolate.html
但我不起作用.
As this calculation is quite expensive, I don't want to make the steps for x too small, and Y is bound to be integer
Further, even for very small steps, there might be quite some changes, I don't want see. So I'd like to interpolate it somehow.
I found
http://docs.scipy.org/doc/scipy-0.14.0/reference/tutorial/interpolate.html
but I don't get it to work.
推荐答案
From the link you suggested, the example here is probably closest to what you want. You can use the example with your values,
import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d, Axes3D
X, Y = np.mgrid[-1:1:20j, -1:1:20j]
Z = (X+Y) * np.exp(-6.0*(X*X+Y*Y)) + np.random.rand(X.shape[0])
xnew, ynew = np.mgrid[-1:1:80j, -1:1:80j]
tck = interpolate.bisplrep(X, Y, Z, s=0)
znew = interpolate.bisplev(xnew[:,0], ynew[0,:], tck)
fig = plt.figure(figsize=(12,12))
ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, Z, cmap='summer', rstride=1, cstride=1, alpha=None)
plt.show()
fig = plt.figure(figsize=(12,12))
ax = fig.gca(projection='3d')
ax.plot_surface(xnew, ynew, znew, cmap='summer', rstride=1, cstride=1, alpha=None, antialiased=True)
plt.show()
此外,antialiased=True
可能会使它看起来更好,但我认为默认情况下处于启用状态.第一个情节看起来像这样,
Also, antialiased=True
may make it look better but I think is on by default. The first plot looks like this,
和这样的平滑图
数据中的低频噪声的问题在于,很难定义足够精细以解决问题的网格.您可以将s
参数设置为interpolate.bisplrep
来调整平滑程度,或者粗略地过滤/过滤数据以仅保留主要趋势(例如,使用
The problem with your the low frequency noise in your data is that it will be difficult to define a grid fine enough to resolve. You can adjust the level of smoothing with the s
argument to interpolate.bisplrep
or perhaps coarse grain/filter your data to leave only major trends (e.g. using scipy.ndimage.interpolation.zoom
if you have regular gridded data). Alternatively, consider a different type of plot such as pcolormesh as the data is essentially 2D.
这篇关于带有Pyplot的平滑表面图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!