python matplotlib:从字符串索引数组绘制三维曲面

python matplotlib:从字符串索引数组绘制三维曲面,python,matplotlib,Python,Matplotlib,我的问题是:我有一个层次结构的子文件夹,每个子文件夹包含一个带有值的文件。例如: 折叠1/ folderA/result.xml folderB/result.xml folderC/result.xml 折叠2/ folderA/result.xml folderB/result.xml folderC/result.xml 折页3/ folderA/result.xml folderB/result.xml folderC/result.xml 我想用matplotlib绘制

我的问题是:我有一个层次结构的子文件夹,每个子文件夹包含一个带有值的文件。例如:

  • 折叠1/
    • folderA/result.xml
    • folderB/result.xml
    • folderC/result.xml
  • 折叠2/
    • folderA/result.xml
    • folderB/result.xml
    • folderC/result.xml
  • 折页3/
    • folderA/result.xml
    • folderB/result.xml
    • folderC/result.xml
我想用matplotlib绘制一个曲面,其中folder1到folder3为X值,folderA到folderC为Y值,相应的结果(来自每个result.xml文件)为Z值。但我不知道如何生成Z数组,以便matplotlib可以正确地绘制曲面

为了清楚起见,假设我有两个数组:

x = ["folder1", "folder2", "folder3"]
y = ["folderA", "folderB", "folderC"]
X,Y = numpy.meshgrid (x,y)
如何生成Z数组,以便按如下方式使用它:

fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X,Y,Z)
我的问题只涉及数组的实际创建(维度和填充),而不是访问XML文件或遍历子文件夹


谢谢大家!

您可以首先将x,y坐标转换为整数:

import numpy as np
xi = np.arange(len(x))
yi = np.arange(len(y))
Xi, Yi = np.meshgrid(xi, yi)
对于Z数组,每对x和y都需要一个值(即
('folder1','folderA'),('folder1','folderB')…
)。您可以在for循环中执行此操作:

Z = np.zeros(Xi.shape)
for i in xi:
    for j in xj:
        xy_pair = (xi[i], yi[j])
        Z[j,i] = calcZ(xy_pair)
我猜
calcZ
函数背后的逻辑取决于如何解析XML文件中的数据

为清晰起见,可以在绘图中更改记号标签以表示所访问的文件夹/文件

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

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')

# ... plot some stuff ...

ax.set_xticks(xi)
ax.set_yticks(yi)
ax.set_xticklabels(x)
ax.set_yticklabels(y)

plt.show()

除了几个小错误,它工作得很好!谢谢你的帮助
np.range()
不存在,我使用了
np.arange()
Z[i,j]
指数顺序错误,
Z[j,i]
worked。