Newbie : How to add a Colored legend with Text for 3d plot

Hi everyone,
I have successfully plotted a set of 372 (x,y,z ) scattered values in 7 series. Each point is a circle colored according to a color based on a simple lookup table.

Lookup table for color

col_lut = dict(Bistris7p2 = “burlywood”, Cit7p2 = “c”,APhosph8p0 = “m”, Acetate5p5 = “k”,Borate8p5 = “y”,Mes6p7 = “c”,Hep8p2 = “g”,ATris9p0 = “r”)

I was wondering how can I have a legend for this color code positioned somewhere alongside or within the interactive 3D figure.
The legend text would be the col_lut keys and the colored values would be a box or circle filled with color alongside. I am sorry I couldnt find an appropriate example to help with the legend creation and am asking to be spoon fed.

Do I use a matplotlib widget or some other utility to programmatically do this.

Thanks for your help
Hari

My code is as follows:

#!/usr/bin/python

import csv file with pH ,salt_conc , temp and do a 3d plot

import csv
from mpl_toolkits.mplot3d.axes3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import ListedColormap

ph = []
salt_conc = []
tm = []
buffcomp = []

Get the raw data

f = open(“stability_buffer_matrix_lookup_table_temp.csv”)
csvfile = csv.reader(f)

#color lookup table
col_lut = dict(Bistris7p2 = “burlywood”, Cit7p2 = “c”,APhosph8p0 = “m”, Acetate5p5 = “k”,Borate8p5 = “y”,Mes6p7 = “c”,Hep8p2 = “g”,ATris9p0 = “r”)

for i in csvfile:

weed out incomplete data points

if "" not in i:
    ph.append(float(i[4]))
    salt_conc.append(float(i[5]))
    tm.append(float(i[-1]))
    #gets a color from the lookup table based on the buffer component entry
    buffcomp.append(col_lut[i[3]])

print set(buffcomp) , col_lut

fig = plt.figure(figsize=plt.figaspect(0.5))
ax = fig.add_subplot(1, 1, 1, projection=‘3d’)
surf = ax.scatter(ph,salt_conc,tm, c=buffcomp)
ax.set_xlabel(‘pH’)
ax.set_ylabel(‘Salt Conc’)
ax.set_zlabel(‘Tm’)

#Tried this does not show up
#colmap = ListedColormap(col_lut.values())
#colmap.Bounds = range(len(col_lut.keys()) +1 )

plt.show()

Legend-handling in mplot3d is no different from how it is done for 2D plots. I don’t know exactly how to do your particular example, but it would likely involve passing legend() a list of Patch objects that represent the markers you made, along with names.

Maybe these examples might be useful?

http://matplotlib.sourceforge.net/examples/pylab_examples/legend_demo.html
http://matplotlib.sourceforge.net/examples/pylab_examples/legend_demo2.html

http://matplotlib.sourceforge.net/examples/pylab_examples/legend_demo3.html

This one in particular might be very relevant:

http://matplotlib.sourceforge.net/examples/pylab_examples/legend_scatter.html

I hope that helps!
Ben Root

···

On Wed, Aug 17, 2011 at 3:29 PM, hari jayaram <harijay@…287…> wrote:

Hi everyone,
I have successfully plotted a set of 372 (x,y,z ) scattered values in 7 series. Each point is a circle colored according to a color based on a simple lookup table.

Lookup table for color

col_lut = dict(Bistris7p2 = “burlywood”, Cit7p2 = “c”,APhosph8p0 = “m”, Acetate5p5 = “k”,Borate8p5 = “y”,Mes6p7 = “c”,Hep8p2 = “g”,ATris9p0 = “r”)

I was wondering how can I have a legend for this color code positioned somewhere alongside or within the interactive 3D figure.
The legend text would be the col_lut keys and the colored values would be a box or circle filled with color alongside. I am sorry I couldnt find an appropriate example to help with the legend creation and am asking to be spoon fed.

Do I use a matplotlib widget or some other utility to programmatically do this.

Thanks for your help
Hari

Thanks for your email Ben. Sorry I am still lost.

I dont understand what the handles type is . In my example I guess the handles are an array of circles representing each x,y,z point.I am still a little lost since the plot autmatically plots my 3 arrays

The color of each circle is arbitrary and stored a the color array.

Even after looking at the examples I dont know how to construct my plt.legend() call.

Hari

···

On Wed, Aug 17, 2011 at 5:35 PM, Benjamin Root <ben.root@…1304…> wrote:

On Wed, Aug 17, 2011 at 3:29 PM, hari jayaram <harijay@…287…> wrote:

Hi everyone,
I have successfully plotted a set of 372 (x,y,z ) scattered values in 7 series. Each point is a circle colored according to a color based on a simple lookup table.

Lookup table for color

col_lut = dict(Bistris7p2 = “burlywood”, Cit7p2 = “c”,APhosph8p0 = “m”, Acetate5p5 = “k”,Borate8p5 = “y”,Mes6p7 = “c”,Hep8p2 = “g”,ATris9p0 = “r”)

I was wondering how can I have a legend for this color code positioned somewhere alongside or within the interactive 3D figure.
The legend text would be the col_lut keys and the colored values would be a box or circle filled with color alongside. I am sorry I couldnt find an appropriate example to help with the legend creation and am asking to be spoon fed.

Do I use a matplotlib widget or some other utility to programmatically do this.

Thanks for your help
Hari

Legend-handling in mplot3d is no different from how it is done for 2D plots. I don’t know exactly how to do your particular example, but it would likely involve passing legend() a list of Patch objects that represent the markers you made, along with names.

Maybe these examples might be useful?

http://matplotlib.sourceforge.net/examples/pylab_examples/legend_demo.html

http://matplotlib.sourceforge.net/examples/pylab_examples/legend_demo2.html

http://matplotlib.sourceforge.net/examples/pylab_examples/legend_demo3.html

This one in particular might be very relevant:

http://matplotlib.sourceforge.net/examples/pylab_examples/legend_scatter.html

I hope that helps!
Ben Root

Take a look at the proxy artist section of the legend guide and see if
that helps

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist

JDH

···

On Wed, Aug 17, 2011 at 5:30 PM, hari jayaram <harijay@...287...> wrote:

Thanks for your email Ben. Sorry I am still lost.

I dont understand what the handles type is . In my example I guess the
handles are an array of circles representing each x,y,z point.I am still a
little lost since the plot autmatically plots my 3 arrays
The color of each circle is arbitrary and stored a the color array.
Even after looking at the examples I dont know how to construct
my plt.legend() call.

Thanks a lot John and Benjamin for your help.

The Proxy Artist approach fits the bill perfectly.

I used the following code to make a legend from my color_lut lookup table.

Lookup table for color

col_lut = dict(Bistris7p2 = “burlywood”, Cit7p2 = “c”,APhosph8p0 = “m”, Acetate5p5 = “k”,Borate8p5 = “y”,Mes6p7 = “c”,Hep8p2 = “g”,ATris9p0 = “r”)

for key,value in col_lut.items():

legend_box.append(Rectangle((0,0),1,1,fc=“%s” % value))

legend_text.append(key)

ax.legend(legend_box, legend_text)

Hari

···

On Wed, Aug 17, 2011 at 7:23 PM, John Hunter <jdh2358@…287…> wrote:

On Wed, Aug 17, 2011 at 5:30 PM, hari jayaram <harijay@…1003…7…> wrote:

Thanks for your email Ben. Sorry I am still lost.

I dont understand what the handles type is . In my example I guess the

handles are an array of circles representing each x,y,z point.I am still a

little lost since the plot autmatically plots my 3 arrays

The color of each circle is arbitrary and stored a the color array.

Even after looking at the examples I dont know how to construct

my plt.legend() call.

Take a look at the proxy artist section of the legend guide and see if

that helps

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist

JDH