简体   繁体   English

Matplotlib:未对齐的颜色条刻度?

[英]Matplotlib: misaligned colorbar ticks?

I'm trying to plot data in the range 0-69 with a bespoke colormap.我正在尝试使用定制的颜色图绘制 0-69 范围内的数据。 Here is an example:下面是一个例子:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

colors = [(0.9, 0.9, 0.9),    # Value = 0
          (0.3, 0.3, 0.3),    # Value = 9
          (1.0, 0.4, 0.4),    # Value = 10
          (0.4, 0.0, 0.0),    # Value = 19
          (0.0, 0.7, 1.0),    # Value = 20
          (0.0, 0.1, 0.3),    # Value = 29
          (1.0, 1.0, 0.4),    # Value = 30
          (0.4, 0.4, 0.0),    # Value = 39
          (1.0, 0.4, 1.0),    # Value = 40
          (0.4, 0.0, 0.4),    # Value = 49
          (0.4, 1.0, 0.4),    # Value = 50
          (0.0, 0.4, 0.0),    # Value = 59
          (1.0, 0.3, 0.0),    # Value = 60
          (1.0, 0.8, 0.6)]    # Value = 69

# Create the values specified above
max_val = 69
values = [n for n in range(max_val + 1) if n % 10 == 0 or n % 10 == 9]

# Create colormap, first normalise values
values = [v / float(max_val) for v in values]
values_and_colors = [(v, c) for v, c in zip(values, colors)]
cmap = LinearSegmentedColormap.from_list('my_cmap', values_and_colors,
                                         N=max_val + 1)

# Create sample data in range 0-69
data = np.round(np.random.random((20, 20)) * max_val)

ax = plt.imshow(data, cmap=cmap, interpolation='nearest')
cb = plt.colorbar(ticks=range(0, max_val, 10))
plt.show()

在此处输入图片说明

I'm thoroughly puzzled as to why the colorbar ticks do not line up with the distinct separations between the color gradients (for which there are 10 colors each).我完全不明白为什么颜色条刻度与颜色渐变之间的明显分隔不一致(每个有 10 种颜色)。

I've tried setting the data and view intervals from [0, 69] to [0, 70]:我尝试将数据和视图间隔设置为 [0, 69] 到 [0, 70]:

cb.locator.axis.set_view_interval(0, 70)
cb.locator.axis.set_data_interval(0, 70)
cb.update_ticks()

but this doesn't appear to do anything.但这似乎没有任何作用。

Please can someone advise?请问有人可以建议吗?

The simplest way to solve my problem was to set vmax in the definition of the mappable:解决我的问题的最简单方法是在可映射的定义中设置vmax

ax = plt.imshow(data, cmap=cmap, interpolation='nearest', vmax=max_val + 1)

It was being set at max_val because the Colorbar class has the call mappable.autoscale_None() in its __init__ , which was setting vmax to data.max() , ie 69.它被设置为max_val是因为 Colorbar 类在其__init__有调用mappable.autoscale_None() ,它将vmax设置为data.max() ,即 69。

I think I am just a victim of using the LinearSegmentedColormap in the wrong way.我只是以错误的方式使用LinearSegmentedColormap的受害者。 I want discrete values assigned to specific colors, but the display of a colorbar associated with LinearSegmentedColormap assumes continuous data and therefore defaults to setting unspecified limits to data.min() and data.max() , ie in this case 0 and 69.我想要分配给特定颜色的离散值,但与LinearSegmentedColormap关联的颜色条的显示假定连续数据,因此默认设置为data.min()data.max()未指定限制,即在这种情况下为 0 和 69。

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

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