colorbar

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 ?

Thanks,

yves

···

--
                                                 (o o)
--------------------------------------------oOO--(_)--OOo-------
  Yves Revaz
  Laboratory of Astrophysics EPFL
  Observatoire de Sauverny Tel : ++ 41 22 379 24 28
  51. Ch. des Maillettes Fax : ++ 41 22 379 22 05
  1290 Sauverny e-mail : Yves.Revaz@...2003...
  SWITZERLAND Web : http://www.lunix.ch/revaz/
----------------------------------------------------------------

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,

Ryan

···

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma

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 ! :frowning: I should do a "subplots_adjust" to the subplot and not to the "fig", but
how can I do that ?

Thanks,

yves

···

Ryan

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma
  
--
                                                 (o o)
--------------------------------------------oOO--(_)--OOo-------
  Yves Revaz
  Laboratory of Astrophysics EPFL
  Observatoire de Sauverny Tel : ++ 41 22 379 24 28
  51. Ch. des Maillettes Fax : ++ 41 22 379 22 05
  1290 Sauverny e-mail : Yves.Revaz@...2003...
  SWITZERLAND Web : http://www.lunix.ch/revaz/
----------------------------------------------------------------

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 ! :frowning: 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

colorbar_loc.py (2.34 KB)

···

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma