首页 > 解决方案 > 平滑 3D 表面 [Python]

问题描述

我想平滑/展平以下 3D 表面:

在此处输入图像描述

z 值是随机生成的,我无法每次都手动调整这些值。因此,对于生成的每一组 z 值,我想调整表面,使其具有更平滑/更平坦(而不是锯齿状)的外观。我尝试使用 来完成此操作interpolate.interp2d,但是,表面仍然看起来不光滑:

在此处输入图像描述

我的代码:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from scipy import interpolate

x_values = [5000, 6000, 7000, 8000, 9000, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000]
y_values = range(10, 10 + 15 * 10, 10)

x, y  = np.meshgrid(x_values, y_values)
plt.style.use('ggplot')

z = np.array([[7.67481329e-03, 5.67279182e-03, 6.17227688e-03, 8.18225024e-03, 9.20576452e-03, 1.20461633e-02, 6.97846662e-03, 1.04070408e-02, 1.11354992e-02, 8.31865352e-03, 7.83007923e-03, 9.01903087e-03, 1.02823552e-02, 9.11136251e-03, 1.32630354e-02],
          [2.10349098e-03, 2.52001640e-03, 2.49460907e-03, 9.86443995e-04, 3.08958474e-03, 2.25651880e-03, 6.89463140e-03, 9.43232455e-03, 2.98107747e-03, 2.65597177e-03, 3.79974206e-03, 3.75051075e-03, 1.87866681e-03, 6.18229524e-03, 1.70829167e-03],
          [5.37015450e-03, 1.64333831e-03, 4.08523696e-03, 6.82044289e-04, 2.44882185e-04, 9.97058687e-03, 7.40913448e-03, 2.12975758e-03, 8.64138886e-03, 2.97420653e-04, 2.15716446e-03, 1.96488762e-03, 6.77434148e-03, 3.89113175e-03, 6.45237223e-03],
          [5.68649385e-03, 6.23399162e-03, 5.51545841e-03, 2.16382107e-03, 4.49401805e-03, 8.24158467e-03, 1.02393551e-03, 3.66267334e-03, 3.87859179e-03, 1.88835543e-04, 6.54640622e-04, 2.01664949e-03, 7.94334090e-04, 3.06483481e-04, 1.28796587e-03],
          [3.82220125e-03, 5.57725771e-03, 6.65356054e-03, 5.73723329e-03, 1.57695388e-03, 1.17996532e-02, 1.24682917e-03, 2.58828966e-03, 3.32018285e-03, 6.10250150e-03, 6.27852475e-04, 1.73029971e-03, 4.52648543e-04, 3.21875990e-03, 1.19367051e-03],
          [8.76890933e-03, 3.83808686e-03, 9.06876749e-04, 6.74119847e-03, 1.46870078e-03, 3.33126781e-03, 4.74421043e-03, 8.52173734e-03, 1.36006531e-03, 5.58394132e-04, 1.16593055e-03, 1.55916069e-03, 5.18676156e-03, 6.86024612e-03, 1.48534094e-03],
          [7.96927816e-03, 2.52233419e-03, 5.47989653e-03, 2.48407529e-04, 3.30486746e-03, 9.81360221e-03, 6.73552857e-03, 6.54734720e-03, 3.93282576e-03, 4.72248681e-03, 7.09641974e-03, 5.53744494e-03, 1.66808219e-03, 1.08243200e-04, 4.09115540e-03],
          [1.03434732e-02, 5.48739187e-03, 3.51817025e-03, 7.84126503e-03, 5.81807439e-03, 3.68019636e-03, 4.98474441e-03, 3.67466423e-03, 1.39580782e-03, 3.58472792e-03, 1.45510724e-03, 8.83906154e-04, 4.30000460e-03, 2.60812348e-03, 1.47295237e-05],
          [1.42723014e-02, 4.17136269e-03, 6.31735034e-03, 9.26834581e-03, 4.15215166e-03, 2.54667560e-03, 1.34555752e-03, 3.60576540e-03, 8.37454859e-04, 5.57819114e-04, 2.32990606e-03, 4.27596008e-03, 5.56193561e-04, 8.53850632e-04, 3.61020319e-03],
          [1.02312919e-02, 4.59340567e-03, 1.11434754e-02, 1.32977181e-02, 7.01157721e-04, 3.33566891e-03, 1.50496088e-03, 4.67634786e-03, 9.50853453e-04, 5.96965539e-03, 1.23052818e-03, 2.05581381e-03, 2.67516371e-03, 1.04107099e-03, 3.23683144e-04],
          [5.00906115e-03, 2.96232937e-03, 1.00700696e-02, 3.53669864e-03, 6.54745504e-03, 3.70682307e-03, 1.64279717e-03, 1.14166720e-03, 2.86436113e-03, 5.72541265e-04, 1.63184329e-03, 7.28343514e-04, 3.35994093e-03, 1.52953340e-04, 2.49887490e-04],
          [9.49011518e-03, 7.87593885e-03, 9.73562612e-03, 4.19310507e-03, 4.75337375e-04, 5.39243039e-03, 8.76021762e-03, 1.02261342e-02, 5.52473123e-03, 5.25679188e-03, 1.06865938e-03, 1.62514312e-04, 4.39454759e-05, 5.92759881e-03, 5.16724115e-03],
          [1.27999012e-02, 1.52182674e-04, 9.16109981e-03, 5.79193691e-03, 8.47254998e-03, 6.85043930e-03, 3.38743793e-03, 3.62688496e-03, 6.78659501e-03, 3.68577740e-03, 4.52053314e-03, 8.24251727e-04, 4.26871143e-03, 6.36134824e-04, 4.54659545e-05],
          [1.08298293e-02, 7.95693340e-03, 7.66274094e-03, 9.99039566e-03, 9.37226706e-03, 9.21200478e-03, 2.19057821e-03, 2.47963218e-03, 3.63675368e-04, 4.08058361e-03, 6.87026238e-04, 3.11584499e-03, 3.90252895e-03, 1.49334850e-03, 2.43520046e-03],
          [1.90757004e-02, 1.11593796e-02, 9.54164445e-03, 2.75071415e-03, 5.59048589e-03, 1.61950357e-02, 2.94542780e-03, 1.90708445e-03, 1.86626715e-03, 3.82148674e-04, 1.87731416e-03, 1.21899768e-03, 1.26173092e-03, 2.16443780e-03, 2.23049538e-03]])

fig = plt.figure()

ax = fig.add_subplot(projection='3d')
surf = ax.plot_surface(x, y, z, rstride=1, cstride=1,alpha=0.75,cmap=cm.RdYlBu_r)

colbar = plt.colorbar(surf, shrink=1.0, extend='both', aspect = 30)
l,b,w,h = plt.gca().get_position().bounds
ll,bb,ww,hh = colbar.ax.get_position().bounds
colbar.ax.set_position([ll, b+0.1*h, ww, h*0.8])

plt.show()   

# Interpolated Graph
x2 = np.linspace( 5000, 100000, 100 )
y2 = np.linspace( 10, 150, 100 )

f = interpolate.interp2d( x, y, z, kind='cubic' )

xx2, yy2 = np.meshgrid( x2, y2 )
zz2 = f( x2, y2 )

fig = plt.figure()

ax = fig.add_subplot(projection='3d')
surf = ax.plot_surface(xx2, yy2, zz2, rstride=1, cstride=1,alpha=0.75,cmap=cm.RdYlBu_r)   

colbar = plt.colorbar(surf, shrink=1.0, extend='both', aspect = 30)
l,b,w,h = plt.gca().get_position().bounds
ll,bb,ww,hh = colbar.ax.get_position().bounds
colbar.ax.set_position([ll, b+0.1*h, ww, h*0.8])

plt.show()                

我该怎么做才能获得更光滑的外观?谢谢你。任何帮助,将不胜感激。

标签: pythonmatplotlibinterpolationsurfacesmoothing

解决方案


推荐阅读