0

I am trying to create a logarithmic colorbar. I know this question has been asked before, like here or here, however in the answers the plot function is used to create the logarithmic colorbar. I want to do it independent of the plot function. So I wrote a function which will calculate the positions of the ticks and create the labels accordingly, but when I try to update the ticks and labels on the colorbar it does not work:

So let's say I have the following plot.

enter image description here

Now my axis looks like this:

list(cbar.ax.get_yticklabels())
>>> [Text(1, 0.0, '$\\mathdefault{10^{0}}$'),
 Text(1, 0.5, ''),
 Text(1, 1.0, ''),
 Text(1, 1.5, ''),
 Text(1, 2.0, ''),
 Text(1, 2.5, ''),
 Text(1, 3.0, ''),
 Text(1, 3.5, '')]

After using my function I get the position for the ticks (every natural number gets a label, the others don't as they are used like minor ticks):

pos = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 3.1, 3.2, 3.3, 3.4, 3.5]

and my labels:

labels = ['$\\mathdefault{10^{3}}$', '', '', '', '', '', '', '', '', '', '$\\mathdefault{10^{3}}$', '', '', '', '', '', '', '', '', '', '$\\mathdefault{10^{3}}$', '', '', '', '', '', '', '', '', '', '$\\mathdefault{10^{3}}$', '', '', '', '', '']

When I try and change the ticks and labels though:

cbar.set_ticks(pos)
cbar.set_ticklabels(labels)

I get this:

[Text(1, 0.0, '$\\mathdefault{10^{0}}$'), Text(1, 0.5, ''), Text(1, 1.0, ''), Text(1, 1.5, ''), Text(1, .0, ''), Text(1, 2.5, ''), Text(1, 3.0, ''), Text(1, 3.5, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, '')]

So additional positions are added but all at position 0 and with no label.

Using

cbar.ax.set_yticks(pos)
cbar.ax.set_yticklabels(labels)

does not work either, instead:

[Text(1, 0.0, '$\\mathdefault{10^{3}}$'), Text(1, 0.5, ''), Text(1, 1.0, ''), Text(1, 1.5, ''), Text(1, .0, ''), Text(1, 2.5, ''), Text(1, 3.0, ''), Text(1, 3.5, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, '$\\mathdefault{10^{3}}$'), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, '$\\mathdefault{10^{3}}$'), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, '$\\mathdefault{10^{3}}$'), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, '')]

So I am wondering what I am doing wrong when trying to change the ticks/labels. Any help is highly appreciated.

In case it is of interest here the functions I use to create the colorbar and to create the new ticks and labels:

import matplotlib as mpl
import numpy as np

def make_log_ticks(current_ticks):
    min_ = np.inf
    max_ = -np.inf
    for elem in current_ticks:
        pos, val = elem.get_position()
        if val < min_:
            min_ = val
        if val > max_:
            max_ = val
    #new_ticks = []
    new_labels = []
    new_pos = []
    for i in range(int(min_*10), int(max_*10+1)):
        if i%10 == 0:
            new_pos.append(i/10)
            new_labels.append("$\\mathdefault{10^{"+str(int(val))+"}}$")
            #new_ticks.append(mpl.text.Text(1, i/10, "$\\mathdefault{10^{int(val)}}$"))
        else:
            new_pos.append(i/10)
            new_labels.append('')
            #new_ticks.append(mpl.text.Text(1, i/10, ""))
    #return new_ticks, new_pos, new_labels
    return new_pos, new_labels


def make_cbar(data, cmap=mpl.cm.jet, log=False):
    m = mpl.cm.ScalarMappable(cmap=cmap)
    if log:
        m.set_array(np.log10(list(set(data))))
        norm = plt.Normalize(np.log10(min(data)), np.log10(max(data)))
    else:
        m.set_array(list(set(data)))
        norm = plt.Normalize(min(data), max(data))
    cbar = plt.colorbar(m)
    cbar.ax.tick_params(axis='both', which='major', width=3 , length=5)
    cbar.ax.tick_params(axis='both', which='minor', width=2 , length=3)
    if log:
        pos, labels = make_log_ticks(list(cbar.ax.get_yticklabels()))
        cbar.ax.set_yticks(pos)
        cbar.ax.set_yticklabels(labels)
        return cbar, lambda c: m.cmap(norm(np.log10(c)))
    else:
        return cbar, lambda c: m.cmap(norm(c))
petezurich
  • 9,280
  • 9
  • 43
  • 57
Nils
  • 910
  • 8
  • 30

1 Answers1

0

I did not know how to tell the colorbar to be logarithmic. However I found out how to do so, in case anyone ever stumbles upon a similar problem:

m = mpl.cm.ScalarMappable(cmap=cmap, norm=mpl.colors.LogNorm())
norm = mpl.colors.LogNorm(min(data), max(data))
m.set_array(list(set(data)))
cbar = plt.colorbar(m)

It is possible to set the Mappable to use a logarithmic norm. Then the labeling will be done automatically. No need to do it by hand then. However, the problem that I could not change the labeling still remains.

Nils
  • 910
  • 8
  • 30