1

I have the following data: https://www.dropbox.com/s/u7ee09chaixw5vb/draw?dl=0

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

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)

enter image description here

sschale
  • 5,168
  • 3
  • 29
  • 36
1a1a11a
  • 1,187
  • 2
  • 16
  • 25
  • Does the problem occur if you only do show (instead of savefig)? Also, if you try plotting it with imshow does the picture appear correctly? You should add the matplotlib version you are using to your question, it might be relevant. – armatita May 23 '16 at 14:42
  • Yes, imshow is correct, and when I use plot_surface for simple function like sin(x), it is also OK. – 1a1a11a May 28 '16 at 13:57

1 Answers1

3

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.

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()

, results in this:

Plotting surface with very unequal shape in matplotlib

armatita
  • 12,825
  • 8
  • 48
  • 49