首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于二进制值的Python 3D绘图

基于二进制值的Python 3D绘图
EN

Stack Overflow用户
提问于 2021-09-20 06:47:58
回答 2查看 58关注 0票数 1

我有一个脚本,它创建一个带有XYZ值的dict。下面的判据包括x处的值,从-2到2,y从0到2。

代码语言:javascript
复制
my_dict = {
-2:{0:1,1:1,2:0},
-1:{0:3,1:1,2:0},
 0:{0:6,1:1,2:9},
 1:{0:-2,1:1,2:2},
 2:{0:1,1:1,2:6}}

现在,我不知道如何在此基础上创建3D图。我知道matplotlib库,但我不确定如何生成Z-Data。我试着写一个函数,让我的Z数据在网格中,但它不起作用。这就是我到目前为止得到的:

代码语言:javascript
复制
    x = np.arange(-2, 2, 1)
    y = np.arange(0, 2, 1)

    X, Y = np.meshgrid(x, y)
    Z = f(X,Y) #HERE, the function f is what I am searching for.

    fig = plt.figure()
    ax = plt.axes(projection='3d')
    ax.contour3D(X, Y, Z, 50, cmap='binary')
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_zlabel('z')

有没有任何麻木或蟒蛇的方法来做到这一点?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-09-20 07:39:47

这就是你要找的吗?

代码语言:javascript
复制
my_dict = {
-2:{0:1,1:1,2:0},
-1:{0:3,1:1,2:0},
 0:{0:6,1:1,2:9},
 1:{0:-2,1:1,2:2},
 2:{0:1,1:1,2:6}}

x = np.arange(-2, 3, 1)
y = np.arange(0, 3, 1)
X, Y = np.meshgrid(x, y)


def f(x, y):
    z = np.zeros(X.reshape(-1).shape)  # Create an "empty" tensor that matches the "flattened" meshgrid
    c = 0  # To index over our "z"
    for i in y:
        for j in x:
            z[c] = my_dict[j][i]  # Fill the empty tensor with its corresponding values from the dictionary (depending on x and y)
            c += 1
    z = z.reshape(X.shape)  # Reshape it back to match meshgrid's shape
    return z


Z = f(x, y)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z, 50, cmap='binary')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
票数 1
EN

Stack Overflow用户

发布于 2021-09-20 07:25:31

我相信你可以通过以下方式访问你的字典中的正确值:

Z = mydict[x][y]

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69250123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档