colorbar questions ...

Hi Everyone,

First I must cryout a little bit :slight_smile: Why colorbar is always so tricky ???

I’m trying to fine tune the colorbar of my contour plots.
I would like to do the following things:

  1. remove the box around it
  2. hide the xticks
  3. make the lines with the indicating colors with custom widths

so far I succeeded only with task 2 !

here is what I did:
cs=axs[4].contour(x,y,phcr1t,linestyles=‘solid’,cmap=cm.jet_r)

a=fig.colorbar(cs,ax=axs[4],orientation=‘horizontal’)
ticks = a.ax.get_xticklines()
#print getp(a.ax)
for tick in ticks:
setp(tick,alpha=0)

Would be great if someone could tell me how to do the 2 other things …

Many thanks

···


Oz Nahum
Graduate Student
Zentrum für Angewandte Geologie
Universität Tübingen


Imagine there’s no countries
it isn’t hard to do

Nothing to kill or die for

And no religion too
Imagine all the people
Living life in peace

Hi Everyone again,

So, with the weekend comes some time to think and I found an answer to another question of mine.

I know now how to remove xticks in colorbar, and I also know how to customize the widths of the lines in the color bar.

import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
from pylab import *

matplotlib.rcParams[‘xtick.direction’] = ‘out’

matplotlib.rcParams[‘ytick.direction’] = ‘out’

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)

Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)

difference of Gaussians

Z = 10.0 * (Z2 - Z1)

Create a simple contour plot with labels using default colors. The

inline argument to clabel will control whether the labels are draw

over the line segments of the contour, removing the lines beneath

the label

plt.figure()
CS = plt.contour(X, Y, Z)
plt.clabel(CS, inline=1, fontsize=10,inlinespacing=50)
a=plt.colorbar()
ticks = a.ax.get_xticklines()

lines = a.ax.get_ygridlines()
children=a.ax.get_children()

children=a.ax.get_children()

children[4].set_linewidths([12,12,12,12,12,12])

for tick in ticks:
setp(tick, [])

plt.title(‘Customize colorbar’)

I hope someone finds that useful. And someone is still following my monologue, my question, how to remove the axes around the colorbar, or at least changed the to be non-visible, still stands…

Thanks,

Oz

···


Oz Nahum
Graduate Student
Zentrum für Angewandte Geologie
Universität Tübingen


Imagine there’s no countries
it isn’t hard to do
Nothing to kill or die for

And no religion too
Imagine all the people
Living life in peace

Oz,

I agree, Colorbar isn’t the most elegant of objects and is probably due for some improvements. I am sure there is probably a better way to do what you have done, but I am not familiar with it. Anyway, to get rid of the box around the colorbar, the colorbar object has a member attribute called “outline” which you can set_visible(False).

a = plt.colorbar()
a.outline.set_visible(False)

Should do the trick for that part.

I hope this helps!
Ben Root

···

On Sat, Sep 11, 2010 at 2:10 PM, Oz Nahum <nahumoz@…287…> wrote:

Hi Everyone again,

So, with the weekend comes some time to think and I found an answer to another question of mine.

I know now how to remove xticks in colorbar, and I also know how to customize the widths of the lines in the color bar.

import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
from pylab import *

matplotlib.rcParams[‘xtick.direction’] = ‘out’

matplotlib.rcParams[‘ytick.direction’] = ‘out’

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)

Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)

difference of Gaussians

Z = 10.0 * (Z2 - Z1)

Create a simple contour plot with labels using default colors. The

inline argument to clabel will control whether the labels are draw

over the line segments of the contour, removing the lines beneath

the label

plt.figure()
CS = plt.contour(X, Y, Z)
plt.clabel(CS, inline=1, fontsize=10,inlinespacing=50)
a=plt.colorbar()

ticks = a.ax.get_xticklines()

lines = a.ax.get_ygridlines()
children=a.ax.get_children()

children=a.ax.get_children()

children[4].set_linewidths([12,12,12,12,12,12])

for tick in ticks:
setp(tick, [])

plt.title(‘Customize colorbar’)

I hope someone finds that useful. And someone is still following my monologue, my question, how to remove the axes around the colorbar, or at least changed the to be non-visible, still stands…

Thanks,

Oz

Did you resolve this?

Alan Isaac

···

On 9/11/2010 3:10 PM, Oz Nahum wrote:

my question, how to remove the axes around the colorbar, or at least changed the to be non-visible, still stands...

Hi Ben,
Thanks for your reply it sure helps.
My awkward work around until now was to put before the colorbar:
matplotlib.rc(‘axes’,edgecolor=‘w’)

and the return it to
matplotlib.rc(‘axes’,edgecolor=‘k’).

Does some one knows how to increase the distance between the color bar and the X-axis ?

Now I noticed that the color bar hides the xlabel …

Thanks again,

Oz

···

I agree, Colorbar isn’t the most elegant of objects and is probably due for some improvements. I am sure there is probably a better way to do what you have done, but I am not familiar with it. Anyway, to get rid of the box around the colorbar, the colorbar object has a member attribute called “outline” which you can set_visible(False).

a = plt.colorbar()
a.outline.set_visible(False)

Should do the trick for that part.

I hope this helps!
Ben Root


Oz Nahum
Graduate Student
Zentrum für Angewandte Geologie
Universität Tübingen


Imagine there’s no countries
it isn’t hard to do

Nothing to kill or die for
And no religion too
Imagine all the people
Living life in peace