missing label in colorbar

Hi Everyone,

I’m struggling to understand how to put the highest value on the ticks of a colorbar.
My problem is that the highest value is simply not there, and it looks weird … I would be happy to solve this issue.

See what my code produces,
http://yfrog.com/j9wrongip

And here is how I want it to be:
http://yfrog.com/2pcorrectsp

And of course here is my code:

from pylab import *
import numpy as N
from matplotlib.ticker import FuncFormatter, ScalarFormatter
a=ScalarFormatter(useOffset=True, useMathText=False)
a.set_scientific(True)

def read_array2(filename, dtypes,lineskip=3, separator=’ '):
“”" Read a file with an arbitrary number of columns.
The type of data in each column is arbitrary
It will be cast to the given dtype at runtime

    This is an improved function that also cleanes the data
"""
startFromLine = lineskip
linesCounter = 1
cast = N.cast
# a nice syntax to initialize a list with determine size

data = [[] for dummy in xrange(len(dtypes))]
for line in open(filename, 'r'):
    #print type(line)
    if linesCounter>startFromLine:
        fields = line.strip().split(separator)

        #clean double numbers because of minus signs
        for i, number in enumerate(fields):
            temp=number.split(" ")
            if len(temp)>1:
                #pdb.set_trace()

                del fields[i]
                for j, hold in enumerate(temp):
                    #print j, hold
                    #pdb.set_trace()
                    fields.insert(i+j,hold)

                    #print len(fields)
                del temp
        #remove trailing calvin degrees in fields
        for i, number in enumerate(fields):
            if number[-4]=='-':

                hold=number[:-4]
                del fields[i]
                fields.insert(i,hold)
        #split fields and append to data
        for i, number in enumerate(fields):

            data[i].append(number)
           #data[i].append(number)
    linesCounter=linesCounter+1
#cast data to a nice array
#pdb.set_trace()
for i in xrange(len(dtypes)):
    data[i] = cast[dtypes[i]](data[i])

return N.rec.array(data, dtype=dtypes)

def readTechPlotHeader(fileName):
‘’’
This function reads a Techplot file header
format. It returns a list which can be used in
other functions, to visualize techplot file format

data using Python.
the function takes in a file name.
'''
fileObject=open(fileName, 'r')
fileObject.next()
header=fileObject.next()
#remove the expresions 'variables = ' and '\n'

header=header[12:-1]
headerCopy=header
#remove all commas, and convert to list
header=header.strip().split(',')
for x in xrange(len(header)):
    header[x]=header[x].strip(' ')

    header[x]=(header[x].strip("\""),'float32')
fileObject.close()
return header, headerCopy

gsp_descr2,headerCopy=readTechPlotHeader(‘dedo2d_0.gsv’)
print gsp_descr2

gsp_descr2=N.dtype(gsp_descr2)

data = read_array2(‘dedo2d_0.gsv’, gsp_descr2)

x,y,dolomite,calcite=data[“x”],data[“y”],data[“dolomite”],data[“calcite”]

x=N.unique(x)
y=N.unique(y)

fig = plt.figure(1,figsize=(10, 6))

ax1 = fig.add_subplot(111)

x=N.unique(x)
y=N.unique(y)
print max(dolomite)
calcites=resize(calcite,(y.size,x.size))

dolomites=resize(dolomite,(y.size,x.size))

CS=ax1.pcolormesh(x,y,dolomites)

ax1.set_title(“dolomite [phi]”)

cbar = fig.colorbar(CS,ticks=[0, 0.0001, 0.00015, 0.0002])
cbar.ax.set_yticklabels([“lo”, “med”, “hi”,‘missing’])

grid(True, which=‘minor’)

from matplotlib.ticker import MultipleLocator, FormatStrFormatter
minorLocator = MultipleLocator(1.0)

ax1.yaxis.set_minor_locator(minorLocator)
ax1.xaxis.set_minor_locator(minorLocator)

ax1.set_title(“dolomite [phi]”)
savefig(‘minerals1.png’,papertype=‘a4’, orientation=‘landscape’)#,bbox_inches=‘tight’)

show()

The correct image is produced with:

import numpy as NP
from matplotlib import pyplot as PLT

A = NP.random.random_integers(0, 2, 100).reshape(10, 10)
print A
A=A/10000.0
print A
fig = PLT.figure()
ax1 = fig.add_subplot(111)

cax = ax1.pcolormesh(A)#, interpolation=“nearest”)

set the tickmarks if you want cutom (ie, arbitrary) tick labels:

cbar = fig.colorbar(cax, ticks=[0, 0.0001, 0.0002])

note: ‘ax’ is not the same as the ‘axis’ instance created by calling ‘add_subplot’

the latter instance i bound to the variable ‘ax1’ to avoid confusing the two

cbar.ax.set_yticklabels([“lo”, “med”, “hi”])
PLT.show()

Thanks in advance,

(If someone needs, I can directly send the data files, they are not to big, but I wanted to spare them from the list)

···


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