简体   繁体   English

颜色条上的手动刻度

[英]Manual ticks on colorbar

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.我想独立于 plot 函数进行操作。 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.所以假设我有以下情节。

在此处输入图片说明

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.因此添加了额外的位置,但都在位置 0 并且没有标签。

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))

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.可以将 Mappable 设置为使用对数范数。 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.但是,我无法更改标签的问题仍然存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM