来自 2D pandas 数据框的 Matplotlib 3D 曲面图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41960448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:53:31  来源:igfitidea点击:

Matplotlib 3D surface plot from 2D pandas dataframe

pythonpandasnumpymatplotlib3d

提问by Violet

I have a pandas dataframe containing four columns of measurement data. I'd like to create a 3D surface plot with the row index as X, the column index as Y, and the data as Z. (The data in each column is a series of discrete measurements output from a test that steps through all values of X for each category Y)

我有一个包含四列测量数据的Pandas数据框。我想创建一个 3D 曲面图,行索引为 X,列索引为 Y,数据为 Z。(每列中的数据是从测试中输出的一系列离散测量值,该测试逐步遍历所有值每个类别 Y 的 X)

import pandas as pd
import numpy as np


df = pd.DataFrame(np.random.randn(5, 4), columns=['A', 'B', 'C', 'D'])
print(df)

   A         B         C         D
0  0.791692 -0.945571  0.183304  2.039369
1 -0.474666  1.117902 -0.483240  0.137620
2  1.448765  0.228217  0.294523  0.728543
3 -0.196164  0.898117 -1.770550  1.259608
4  0.646730 -0.366295 -0.893671 -0.745815

I tried converting the df into a numpy grid using np.meshgrid as below but not sure I really understand what is required, or if I can use the df indices in this way.

我尝试使用 np.meshgrid 将 df 转换为 numpy 网格,如下所示,但不确定我是否真的理解需要什么,或者我是否可以以这种方式使用 df 索引。

import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x = df.columns
y = df.index
X,Y = np.meshgrid(x,y)
Z = df
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z)

I've read through the matplotlib 3D tutorial and related answers here, but am still stuck. Would be very grateful if someone could point me in the right direction please.

我已经在这里阅读了 matplotlib 3D 教程和相关答案,但仍然卡住了。如果有人能指出我正确的方向,我将不胜感激。

采纳答案by ImportanceOfBeingErnest

The general strategy you pursue is fine. The only error you have is that you create a meshgrid from a list of strings. Of course maplotlib cannot plot strings.

您采用的总体策略很好。您遇到的唯一错误是您从字符串列表创建了网格。当然 maplotlib 不能绘制字符串。

You can therfore create an array of the same length as the number of columns in your dataframe and plug that into the meshgrid.

因此,您可以创建一个与数据框中列数相同长度的数组,并将其插入meshgrid.

x = np.arange(len(df.columns))

回答by epattaro

just take of the columns names (['A', 'B', 'C', 'D']) and it should work.

只需取列名 (['A', 'B', 'C', 'D']) 就可以了。

you can later change the ticks of the axis for ['A', 'B', 'C', 'D'].

您可以稍后更改 ['A', 'B', 'C', 'D'] 轴的刻度。

import pandas as pd
import numpy as np


df = pd.DataFrame(np.random.randn(5, 4))

import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x = df.columns
y = df.index
X,Y = np.meshgrid(x,y)
Z = df
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z)

enter image description here

在此处输入图片说明