Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何制作数据帧的曲面图?_Python_Pandas_Numpy_Dataframe_Matplotlib - Fatal编程技术网

Python 如何制作数据帧的曲面图?

Python 如何制作数据帧的曲面图?,python,pandas,numpy,dataframe,matplotlib,Python,Pandas,Numpy,Dataframe,Matplotlib,我有一个很好的数据框df,其中我的X是行,我的Y是列,我的Z对应于df.loc[X,Y]: 我只想做一个表面图。到目前为止,我已经: import pandas as pd import numpy as np import matplotlib.pyplot as plt x = df.index y = df.columns X,Y = np.meshgrid(x, y) zs = np.array(df) Z = zs.reshape(X.shape) fig = plt.figu

我有一个很好的数据框
df
,其中我的X是行,我的Y是列,我的Z对应于
df.loc[X,Y]

我只想做一个表面图。到目前为止,我已经:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

x = df.index
y = df.columns
X,Y = np.meshgrid(x, y)

zs = np.array(df)
Z = zs.reshape(X.shape)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X,Y,Z)

ax.set_xlabel(df.index.name)
ax.set_ylabel(df.columns.name)
ax.set_zlabel('Approximation')
plt.show()
但我的结果看起来不连贯:


我认为问题在于我如何绘制图,而不是数据。我做错什么了吗?我觉得重塑数组会搞乱x、y和z之间的关系,但我已经尝试了所有np.Reformate的排序参数。

我希望我能提供一个更有趣的答案,但可能只是你的数据有噪音,反映在曲面图中

我重新创建了一些类似于你的正弦曲线数据,添加了一点但不是太多的错误,结果也同样不稳定

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


def my_func(x, y):
    return np.sin(x**2 + y**2)

x = np.linspace(0, 3, 61)
y = np.linspace(0, 3, 61)
X,Y = np.meshgrid(x, y)

# add some error to our function: for f(x,y) in the range of [-1,1]
# the noise has a standard deviation of 1/25 = 0.04
# which is close to the spacing in the mesh grid so it's noticeable
Z = my_func(X, Y) + np.random.standard_normal((len(x), len(y)))/25

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X,Y,Z)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Approximation')
plt.show()

您是否尝试过
Z=df.values
?虽然我确实认为问题确实在于你的数据,而不是你如何策划。