Colorbar for pcolor plot

Hi everybody,
I have the problem, that I cannot add a color bar to a pcolor plot, which I
generate of some Data files. If I do
fig = plt.figure()

plot1 = fig.add_subplot(231,aspect='equal')
plot1.pcolor(xsr)
plot1.axis([0, 127, 0, 127])
plot1.colorbar()

it just gives me
AttributeError: 'AxesSubplot' object has no attribute 'colorbar'
WARNING: Failure executing file: <test.py>

What am I doing wrong? At the end of this file you find the whole plot-file

Thank you for your help,
Markus

#!usr/bin/python
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt

# Open files

file1 = open('../physical/x-ray/193XM_phys.am', 'rb')
file1.seek(-131072,2)
xm=np.fromfile(file1,dtype='>d')
file1.close()

file2 = open('../physical/x-ray/193XSB_phys.am', 'rb')
file2.seek(-131072,2)
xs=np.fromfile(file2,dtype='>d')
file2.close()

file3 = open('../physical/x-ray/193XT_phys.am', 'rb')
file3.seek(-131072,2)
xt=np.fromfile(file3,dtype='>d')
file3.close()

xsr=np.transpose(np.reshape(xs,(128,128)))
xtr=np.transpose(np.reshape(xt,(128,128)))
xmr=np.transpose(np.reshape(xm,(128,128)))

ind_xmax=np.where(xsr==np.max(xsr))[1][0]
ind_ymax=np.where(xsr==np.max(xsr))[0][0]

profil_xsr=np.zeros(np.minimum(ind_xmax,ind_ymax))
profil_xtr=np.zeros(np.minimum(ind_xmax,ind_ymax))
profil_xmr=np.zeros(np.minimum(ind_xmax,ind_ymax))
anzahl_gridpunkte=np.zeros(np.minimum(ind_xmax,ind_ymax))

for k in range(0,26):
    # Schleife über alle Gitterzellen
    for i in range(128):
  for j in range (128):
      if (k)**2 < (i-ind_ymax)**2+(j-ind_xmax)**2 <= (k+1)**2:
    profil_xsr[k]=profil_xsr[k]+xsr[i][j]
    profil_xtr[k]=profil_xtr[k]+xtr[i][j]
    profil_xmr[k]=profil_xmr[k]+xmr[i][j]
    anzahl_gridpunkte[k]=anzahl_gridpunkte[k]+1

profil_xsr=profil_xsr/anzahl_gridpunkte
profil_xtr=profil_xtr/anzahl_gridpunkte
profil_xmr=profil_xmr/anzahl_gridpunkte
    
fig = plt.figure()

plot1 = fig.add_subplot(231,aspect='equal')
plot1.pcolor(xsr)
plot1.axis([0, 127, 0, 127])
plot1.colorbar()

plot2 = fig.add_subplot(232,aspect='equal')
plot2.pcolor(10**xtr)
plot2.axis([1, 128, 1, 128])

plot3 = fig.add_subplot(233,aspect='equal')
plot3.pcolor(xmr,vmin=0.0,vmax=0.5)
plot3.axis([1, 128, 1, 128])

plot4 = fig.add_subplot(234)
plot4.plot(profil_xsr)
plot4.axis(ymin=0, ymax=10)

plot5 = fig.add_subplot(235)
plot5.plot(profil_xtr)
plot5.axis(ymin=0, ymax=10)

plot6 = fig.add_subplot(236)
plot6.plot(profil_xmr)
plot6.axis([0, 25,0, 1])

fig.show()

···

--
View this message in context: http://www.nabble.com/Colorbar-for-pcolor-plot-tp23631013p23631013.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Thanks for the full example, but if you carefully read the exception, it was telling you the problem. :slight_smile: plot1 here is an axes object, which does not have a colorbar() method. Instead, you should change that to:

plt.colorbar()

Assuming everything else was working, you should be good to go with this change.

Ryan

···

On Wed, May 20, 2009 at 3:17 AM, marcusantonius <markus.haider@…2356…> wrote:

Hi everybody,

I have the problem, that I cannot add a color bar to a pcolor plot, which I

generate of some Data files. If I do

fig = plt.figure()

plot1 = fig.add_subplot(231,aspect=‘equal’)

plot1.pcolor(xsr)

plot1.axis([0, 127, 0, 127])

plot1.colorbar()

it just gives me

AttributeError: ‘AxesSubplot’ object has no attribute ‘colorbar’

WARNING: Failure executing file: <test.py>

What am I doing wrong? At the end of this file you find the whole plot-file


Ryan May
Graduate Research Assistant
School of Meteorology

University of Oklahoma
Sent from Norman, Oklahoma, United States

It looks like Markus is trying to use the API, so rather than suggest
the pyplot colorbar method, I suggest using the figure instance
method. Markus the pyplot method plt.colorbar is a thin wrapper
around the figure method fig.colorbar -- see also:

  http://matplotlib.sourceforge.net/faq/usage_faq.html#matplotlib-pylab-and-pyplot-how-are-they-related

  http://matplotlib.sourceforge.net/api/figure_api.html#matplotlib.figure.Figure.colorbar

It may be a good idea and refer to the return value of fig.add_subplot
as "ax" or something that, rather than "plot1" because add_subplot
returns an Axes instance and thus ax is a better mnemonic; see

  http://matplotlib.sourceforge.net/api/figure_api.html#matplotlib.figure.Figure.add_subplot

So I suggest something like::

    fig = plt.figure()

    ax1 = fig.add_subplot(231,aspect='equal')
    ax1.pcolor(xsr)
    ax1.axis([0, 127, 0, 127])
    fig.colorbar()

JDH

···

On Wed, May 20, 2009 at 9:53 AM, Ryan May <rmay31@...287...> wrote:

Thanks for the full example, but if you carefully read the exception, it was
telling you the problem. :slight_smile: plot1 here is an axes object, which does not
have a colorbar() method. Instead, you should change that to:

plt.colorbar()

Assuming everything else was working, you should be good to go with this
change.

Except that it won’t work like that. :slight_smile: (I actually tried that the first time) You need to give Figure.colorbar() the mappable as the first argument. So this would then become:

ax1 = fig.add_subplot(231,aspect='equal')
pc = ax1.pcolor(xsr)
ax1.axis([0, 127, 0, 127])
fig.colorbar(pc)

Ryan

···

On Wed, May 20, 2009 at 10:04 AM, John Hunter <jdh2358@…287…> wrote:

On Wed, May 20, 2009 at 9:53 AM, Ryan May <rmay31@…287…> wrote:

Thanks for the full example, but if you carefully read the exception, it was

telling you the problem. :slight_smile: plot1 here is an axes object, which does not

have a colorbar() method. Instead, you should change that to:

plt.colorbar()

Assuming everything else was working, you should be good to go with this

change.

It looks like Markus is trying to use the API, so rather than suggest

the pyplot colorbar method, I suggest using the figure instance

method. Markus the pyplot method plt.colorbar is a thin wrapper

around the figure method fig.colorbar – see also:

http://matplotlib.sourceforge.net/faq/usage_faq.html#matplotlib-pylab-and-pyplot-how-are-they-related

http://matplotlib.sourceforge.net/api/figure_api.html#matplotlib.figure.Figure.colorbar

It may be a good idea and refer to the return value of fig.add_subplot

as “ax” or something that, rather than “plot1” because add_subplot

returns an Axes instance and thus ax is a better mnemonic; see

http://matplotlib.sourceforge.net/api/figure_api.html#matplotlib.figure.Figure.add_subplot

So I suggest something like::

fig = plt.figure()



ax1 = fig.add_subplot(231,aspect='equal')

ax1.pcolor(xsr)

ax1.axis([0, 127, 0, 127])

fig.colorbar()


Ryan May
Graduate Research Assistant

School of Meteorology

University of Oklahoma
Sent from Norman, Oklahoma, United States

Thank you very much for your detailed explanations. When I try

plot1 = fig.add_subplot(231,aspect='equal')
plot1.pcolor(xsr)
plot1.axis([0, 127, 0, 127])
plt.colorbar()
I get the error message
AttributeError: 'NoneType' object has no attribute 'autoscale_None'
WARNING: Failure executing file: <test.py>
But I don't really know, what this means.

It may be a good idea and refer to the return value of fig.add_subplot
as "ax" or something that, rather than "plot1" because add_subplot
returns an Axes instance and thus ax is a better mnemonic; see

Thank you very much, I was not aware of that. However, if I try

fig = plt.figure()
ax1 = fig.add_subplot(231,aspect='equal')
ax1.pcolor(xsr)
ax1.axis([0, 127, 0, 127])
fig.colorbar()
I also get an error message:
TypeError: colorbar() takes at least 2 arguments (1 given)
WARNING: Failure executing file: <test.py>

Cheers,
Markus

Yep, that's it. It works now. Thank you for your help,

Cheers,
Markus

···

Am Mittwoch, den 20.05.2009, 10:21 -0500 schrieb Ryan May:

Except that it won't work like that. :slight_smile: (I actually tried that the
first time) You need to give Figure.colorbar() the mappable as the
first argument. So this would then become:

    ax1 = fig.add_subplot(231,aspect='equal')
    pc = ax1.pcolor(xsr)
    ax1.axis([0, 127, 0, 127])
    fig.colorbar(pc)