How to provide colorbars for scatterplots created this way?

I create PNG files of scatterplots with code that, in essence, goes as in the sketch below:

cmap = (matplotlib.color.LinearSegmentedColormap.
from_list(‘blueWhiteRed’, [‘blue’, ‘white’, ‘red’]))

fig = matplotlib.figure.Figure(figsize=(4, 4), dpi=72)
ax = fig.gca()

for marker in ‘o s ^ *’.split():

X, Y, COLOR = zip(*((record.x, record.y, record.level)
                    for record in data if record.marker == marker))

ax.scatter(X, Y, marker=marker,
           c=COLOR, vmin=0, vmax=1, cmap=cmap,
           **otherkwargs)

various settings of ticks, labels, etc. omitted

canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(fig)

fig.set_canvas(canvas)

IMPORTANT: the generated figure is not displayed on the screen, but

rather it is output to disk as a PNG file:

canvas.print_png(’/path/to/output/fig.png’)

My question is this:

What do I need to add to the code above to get a vertical colorbar (representing the colormap incmap) along the plot’s right edge?

I word the question in this way because I am not sufficiently facile with Matplotlib to deviate too far from the working code above.

In particular, my code has to be able to produce PNG files non-interactively, so the last line in the code sketch above is really essential.

Thanks in advance!

kj

cb = plt.colorbar(s)
cb.set_label(‘Cbar Label Here’)

···

I create PNG files of scatterplots with code that, in essence, goes as in the sketch below:

cmap = (matplotlib.color.LinearSegmentedColormap.
from_list(‘blueWhiteRed’, [‘blue’, ‘white’, ‘red’]))

fig = matplotlib.figure.Figure(figsize=(4, 4), dpi=72)
ax = fig.gca()

for marker in ‘o s ^ *’.split():

X, Y, COLOR = zip(*((record.x, record.y, record.level)
                    for record in data if record.marker == marker))



ax.scatter(X, Y, marker=marker,
           c=COLOR, vmin=0, vmax=1, cmap=cmap,
           **otherkwargs)

various settings of ticks, labels, etc. omitted

canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(fig)

fig.set_canvas(canvas)

IMPORTANT: the generated figure is not displayed on the screen, but

rather it is output to disk as a PNG file:

canvas.print_png(’/path/to/output/fig.png’)

My question is this:

What do I need to add to the code above to get a vertical colorbar (representing the colormap incmap) along the plot’s right edge?

I word the question in this way because I am not sufficiently facile with Matplotlib to deviate too far from the working code above.

In particular, my code has to be able to produce PNG files non-interactively, so the last line in the code sketch above is really essential.

Thanks in advance!

kj

Can you provide some more information and a self-contained example? What is your record object? Is it a pandas dataframe? Are the limits of record.level consistent with vmax and vmin kwargs fed in the call to ax.scatter?

Typically you can just do:

import matplotlib.pyplot as plt

blah blah

fig, ax = plt.subplots()

s = ax.scatter()…

Also, I don’t think you need to mess with the backend stuff. Just do fig.savefig(‘figname.png’). If you need separate markers for each set, make a single call to scatter for each data group, and use numpy to figure out what the appropriate vmax a vmin limits are for the colorbar.

-paul

I normally just plot a whole bunch of arrays… and then add the
color bar

sc1 = f1s1.scatter(array[:,0], array[:,1], c=array[:,2], s=50,

cmap=‘spectral’,edgecolor=‘w’)
bar = pyl.colorbar(sc1)
bar.set_label(“label”)

Steven
···

On 12/20/12 5:54 PM, Paul Hobson wrote:

On Thu, Dec 20, 2012 at 1:05 PM, Kynn Jones <kynnjo@…287…>
wrote:

              I

create PNG files of scatterplots with code that, in
essence, goes as in the sketch below:

            cmap =

(matplotlib.color.LinearSegmentedColormap.

                    from_list('blueWhiteRed', ['blue', 'white',

‘red’]))

            fig = matplotlib.figure.Figure(figsize=(4, 4), dpi=72)

            ax = fig.gca()




              for marker in

‘o s ^ *’.split():

                    X, Y, COLOR

= zip(*((record.x, record.y, record.level)

                                        for record in data if

record.marker == marker))

                      ax.scatter(X, Y, marker=marker,


                             
 c=COLOR, vmin=0, vmax=1, cmap=cmap,

                                   **otherkwargs)
                    # various

settings of ticks, labels, etc. omitted

canvas =
matplotlib.backends.backend_agg.FigureCanvasAgg(fig)

                    fig.set_canvas(canvas)
                    #

IMPORTANT: the generated figure is not
displayed on the screen, but

rather it is

output to disk as a PNG file:

canvas.print_png(’/path/to/output/fig.png’)

My question is this:

                  What do I need to add to the code above to get a

vertical colorbar (representing the colormap
incmap) along the plot’s right edge?

                  I word the question in this way because I am

not sufficiently facile with Matplotlib to deviate
too far from the working code above.

                  In particular, my code *has* to be able to

produce PNG files non-interactively, so the last
line in the code sketch above is really essential.

Thanks in advance!

kj

      Can you provide some more

information and a self-contained example? What is your record
object? Is it a pandas dataframe? Are the limits of
record.level consistent with vmax and vmin kwargs fed in the
call to ax.scatter?

Typically you can just do:

      import matplotlib.pyplot as

plt

blah blah

fig, ax = plt.subplots()

s = ax.scatter()…

cb = plt.colorbar(s)
cb.set_label(‘Cbar Label Here’)

        Also, I don't think you need

to mess with the backend stuff. Just do
fig.savefig(‘figname.png’). If you need separate markers for
each set, make a single call to scatter for each data group,
and use numpy to figure out what the appropriate vmax a vmin
limits are for the colorbar.

-paul

------------------------------------------------------------------------------
LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial
Remotely access PCs and mobile devices and provide instant support
Improve your efficiency, and focus on delivering more value-add services
Discover what IT Professionals Know. Rescue delivers
_______________________________________________
Matplotlib-users mailing list

-- Steven Boada
Doctoral Student
Dept of Physics and Astronomy
Texas A&M University

http://p.sf.net/sfu/logmein_12329d2dMatplotlib-users@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/matplotlib-usersboada@…3847…