plotting 3d scatter in matplotlib plotting 3d scatter in matplotlib numpy numpy

plotting 3d scatter in matplotlib


Try replacing 'ax.scatter' with ax.plot', possibly with the 'o' parameter to get similar circles. This fixes the transparency and the legend.

import matplotlib as mplfrom mpl_toolkits.mplot3d import Axes3Dimport numpy as npimport matplotlib.pyplot as pltfrom numpy.random import randommpl.rcParams['legend.fontsize'] = 10fig = plt.figure(1)fig.clf()ax = Axes3D(fig)datasets = random((8,100,3))*512my_labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']colors = ['k', "#B3C95A", 'b', '#63B8FF', 'g', "#FF3300",          'r', 'k']index = 0for data, curr_color in zip(datasets, colors):    ax.plot(np.log2(data[:, 0]), np.log2(data[:, 1]),                    np.log2(data[:, 2]), 'o', c=curr_color, label=my_labels[index])    index += 1ax.set_zlim3d([-1, 9])ax.set_ylim3d([-1, 9])ax.set_xlim3d([-1, 9])ax.set_xticks(range(0,11))ax.set_yticks([1,2,8])ax.set_zticks(np.arange(0,9,.5))ax.legend(loc = 'upper left')    plt.draw()plt.show()

I added a few lines and tweaks to get some sample data and get the rest of your demo working. I assume you'll be able to get it to work.

Setting the ticks requires the August 2010 update to mplot3d as described here. I got the latest mplot3d from Sourceforge. I'm not quite sure if Matplotlib 1.0.1 contains this latest update as I'm still running Python 2.6 with Matplotlib 1.0.0.

Edit

A quick and dirty dummy plot for the legends while keeping the 3d transparency effect you get from scatter:

index = 0for data, curr_color in zip(datasets, colors):    ax.scatter(np.log2(data[:, 0]), np.log2(data[:, 1]),                    np.log2(data[:, 2]), 'o', c=curr_color, label=my_labels[index])    ax.plot([], [], 'o', c = curr_color, label=my_labels[index])                        index += 1


AFAIK, legends for 3d scatter artists are not directly supported. See here:http://matplotlib.sourceforge.net/users/legend_guide.html#plotting-guide-legend

However, you can use a hack/work-around with a 'proxy artist', using something like:

p = Rectangle((0, 0), 1, 1, fc="r")axis.legend([p], ["Red Rectangle"])

So the proxy artist won't be added to the axis, but you can use it to create a legend.


Setting the kwarg depthshade=False fixed it for me:

ax.scatter(np.log2(data[:, 0]), np.log2(data[:, 1]),                np.log2(data[:, 2]), 'o', c=curr_color,  label=my_labels[index], depthshade=False)