简体   繁体   English

曲面图原始数据

[英]Surface plot raw data

I'm looking for a way to fit some 3D data to a function and plot it using Python. 我正在寻找一种将某些3D数据拟合到函数并使用Python对其进行绘制的方法。

This tutorial describes how to plot a 3D surface but it assumes that we know the function describing the surface. 本教程描述了如何绘制3D曲面,但假设我们知道描述曲面的函数。

How can i import the data below and plot it using the plot_surface function (without knowing that z = x*y in this case)? 我如何导入下面的数据并使用plot_surface函数进行绘制(在这种情况下不知道z = x * y)?

    x   1   2   3   4
y                   
1       1   2   3   4
2       2   4   6   8
3       3   6   9   12
4       4   8   12  16
5       5   10  15  20

The data format is not very convenient to work with, but here is a way to read in the data, ignoring the coordinates and regenerating them afterwards. 数据格式使用起来不是很方便,但是这是一种读入数据,忽略坐标并随后重新生成它们的方法。

import io
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

d = u"""    x   1   2   3   4
y                   
1       1   2   3   4
2       2   4   6   8
3       3   6   9   12
4       4   8   12  16
5       5   10  15  20"""

s = io.StringIO(d)
a = np.loadtxt(s, skiprows=2)
Z = a[:,1:] # ignore first column

x = np.arange(1,Z.shape[1]+1)
y = np.arange(1,Z.shape[0]+1)
X,Y = np.meshgrid(x,y)

fig = plt.figure()
ax = fig.gca(projection='3d')

surf = ax.plot_surface(X, Y, Z, cmap=plt.cm.coolwarm,
                       linewidth=0, antialiased=False)

plt.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM