Yves Revaz wrote:
Ryan May wrote:
Yves Revaz wrote:
Hi all,

When I use:

colorbar(orientation='horizontal')

the color bar is drawn on the bottom of the corresponding graph.
Which option will draw the colorbar on the top of the graph ?
I think (correct me if I'm wrong devs) you'll have to use the cax
keyword argument to manually specifiy the position of the axes in which
to draw the colorbar.  You'll also need to adjust the position of the
plot using figure.subplots_adjust.  Like this maybe:

import numpy as np
import matplotlib.pyplot as plt
data = np.random.randn(30,30)
plt.pcolor(data)
fig = plt.gcf()
fig.subplots_adjust(top=0.85)
ax = fig.add_axes([0.12, 0.9, 0.8, 0.05])
plt.colorbar(cax=ax, orientation='horizontal')

Hope this helps,


ok, it works fine for that case.
Now, my problem is that my graph is a subplot:
Taking your example it corresponds to :

import numpy as np
import matplotlib.pyplot as plt

fig = plt.gcf()

plt.subplot(2,2,2)
data = np.random.randn(30,30)
plt.pcolor(data)

fig.subplots_adjust(top=0.85)
ax = fig.add_axes([0.12, 0.9, 0.8, 0.05])
plt.colorbar(cax=ax, orientation='horizontal')

plt.show()


Obviously, the colorbar has now the length of the whole figure and not
the one
of the subplot ! :-( I should do a "subplots_adjust" to the subplot and not to the "fig", but
how can I do that ?

Ok, I've now made a full out solution here. It's a modification of the function make_axes() in colorbar.py which performs the task of splitting an existing axes object into a colobar, the plot, and some padding. I've added the location keyword parameter to specify where you want the colorbar. Location should be 0 or 1, depending where you want the colorbar located along either the x or y direction. In your case, for a colorbar at the top, it should be 1.0. The example

Matplotlib devs, can I get some help getting this small feature added to colorbar? I'm not in love with the "location" keyword or having it's value be 0 or 1, but I wasn't sure how to do this with a single parameter while simultaneously keeping the old defaults. There's also some spacing issues with a vertical colorbar on the left side of the axes that I can't seem to sort out. Thoughts?

Ryan

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma
def make_axes(parent, **kw):
    orientation = kw.setdefault('orientation', 'vertical')
    fraction = kw.pop('fraction', 0.15)
    shrink = kw.pop('shrink', 1.0)
    aspect = kw.pop('aspect', 20)
    pb = parent.get_position(original=True).frozen()
    if orientation == 'vertical':
        location = kw.pop('location', 1)
        pad = kw.pop('pad', 0.05)
        if location:
            x1 = 1.0-fraction
            pb1, pbx, pbcb = pb.splitx(x1-pad, x1)
            pbcb = pbcb.shrunk(1.0, shrink).anchored('C', pbcb)
            anchor = (0.0, 0.5)
            panchor = (1.0, 0.5)
        else:
            pbcb, pbx, pb1 = pb.splitx(fraction, fraction+pad)
            pbcb = pbcb.shrunk(1.0, shrink).anchored('C', pbcb)
            anchor = (1.0, 0.5)
            panchor = (0.0, 0.5)
    else:
        location = kw.pop('location', 0)
        pad = kw.pop('pad', 0.15)
        if location:
            y1 = 1.0-fraction
            pb1, pbx, pbcb = pb.splity(y1-pad, y1)
            pbcb = pbcb.shrunk(shrink, 1.0).anchored('C', pbcb)
            anchor = (0.5, 0.0)
            panchor = (0.5, 1.0)
        else:
            pbcb, pbx, pb1 = pb.splity(fraction, fraction+pad)
            pbcb = pbcb.shrunk(shrink, 1.0).anchored('C', pbcb)
            anchor = (0.5, 1.0)
            panchor = (0.5, 0.0)
        aspect = 1.0/aspect
    parent.set_position(pb1)
    parent.set_anchor(panchor)
    fig = parent.get_figure()
    cax = fig.add_axes(pbcb)
    cax.set_aspect(aspect, anchor=anchor, adjustable='box')
    return cax, kw

import numpy as np
import matplotlib.pyplot as plt

data = np.random.rand(50,50)

fig = plt.figure()

ax = fig.add_subplot(2, 2, 1)
im = ax.pcolor(data)
cax,kw = make_axes(ax, orientation='vertical', location=0.0)
fig.colorbar(im, ax=ax, cax=cax, **kw)
ax.set_title('Vertical, 0.0')

ax = fig.add_subplot(2, 2, 2)
im = ax.pcolor(data)
cax,kw = make_axes(ax, orientation='vertical', location=1.0)
fig.colorbar(im, ax=ax, cax=cax, **kw)
ax.set_title('Vertical, 1.0')

ax = fig.add_subplot(2, 2, 3)
im = ax.pcolor(data)
cax,kw = make_axes(ax, orientation='horizontal', location=0.0)
fig.colorbar(im, ax=ax, cax=cax, **kw)
ax.set_title('Horizontal, 0.0')

ax = fig.add_subplot(2, 2, 4)
im = ax.pcolor(data)
cax,kw = make_axes(ax, orientation='horizontal', location=1.0)
fig.colorbar(im, ax=ax, cax=cax, **kw)
ax.set_title('Horizontal, 1.0')

plt.show()
-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to