matplotlib 3D图,plot_surface黑色 [英] matplotlib 3D plot, plot_surface black

查看:91
本文介绍了matplotlib 3D图,plot_surface黑色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据: https://www.dropbox.com/s/u7ee09chaixw5vb/draw?dl= 0

它是使用pickle在python3中保存的,它只是一个二维python列表,形式为z=[[],[],[]...[]]

it is saved using pickle in python3 and it is just a two dimensional python list, in the form of z=[[],[],[]...[]]

,我使用以下代码绘制3D图形,但是它只显示黑色表面,为什么?可以从上面的文件中加载xydict:

and I use the following code to plot the 3D graph, but it only shows me black surface, why? xydict can be loaded from the file above:

    from mpl_toolkits.mplot3d import Axes3D

    fig = plt.figure()
    ax = Axes3D(fig)
    X = np.arange(0, len(xydict))
    Y = np.arange(0, len(xydict[0]))
    X, Y = np.meshgrid(X, Y)
    Z = np.array(xydict).T


    ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.hot)     
    # ax.contourf(X, Y, Z, zdir='z', offset=0, cmap=plt.cm.hot)
    ax.set_zlim(0,1)

    plt.savefig('plot3d_ex.png', dpi=480)

推荐答案

您的数据和使用的参数都存在一些问题.表面的形状极为不相等,并且您要求每条线一个rstride.结果是您只能从步幅上看到黑色.

There are a few problems with both your data and the arguments you used. The shape of your surface is extremely unequal and you are requesting one rstride for every line. The result is that you only see the black from the strides.

另一个问题是您的数据中似乎有nan值.如果将数据限制为有效值并选择更好的stride数字,则应该获得更好的图.例如:

The other problem is that you seem to have nan values in your data. Should you limit the data to the valid values and pick better stride numbers you should obtain a far better plot. For instance this:

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

with open('draw', 'rb') as pickle_file:
    xydict = pickle.load(pickle_file)

fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(0, len(xydict))
Y = np.arange(0, len(xydict[0]))
X, Y = np.meshgrid(X, Y)
Z = np.array(xydict).T

ax.plot_surface(X[:,:-2], Y[:,:-2], Z[:,:-2], rstride=100, cstride=1, cmap=plt.cm.hot)     
# ax.contourf(X, Y, Z, zdir='z', offset=0, cmap=plt.cm.hot)
ax.set_zlim(0,1)
plt.show()

,结果如下:

这篇关于matplotlib 3D图,plot_surface黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆