matplotlib颜色栏勾号标签外部错误

matplotlib颜色栏勾号标签外部错误,matplotlib,colorbar,Matplotlib,Colorbar,当自定义标记位于vmin或vmax之外时,自定义标签将移动: import matplotlib import matplotlib.pyplot as plt import numpy as np from matplotlib import cm from numpy.random import random # Make plot with horizontal colorbar fig, ax = plt.subplots() data = random((250,250)) + 3

当自定义标记位于vmin或vmax之外时,自定义标签将移动:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from numpy.random import random

# Make plot with horizontal colorbar
fig, ax = plt.subplots()

data = random((250,250)) + 3.5

norm = matplotlib.colors.Normalize(vmin=2.5,vmax=4.5)

cax = ax.imshow(data, interpolation='nearest', cmap=cm.afmhot, norm=norm)
ax.set_title('Gaussian noise with horizontal colorbar')

cbar = fig.colorbar(cax, ticks=[1,2,3,4], orientation='horizontal')
cbar.set_ticklabels(['one','two', 'three', 'four'])# horizontal colorbar

plt.savefig("example.png")


是虫子吗?任何解决方法?

您可以获得第一个实际的刻度,并将标签与指定的刻度值/位置进行比较。然后,您可以使用该索引启动自定义标签。
下面是一个经过修改的示例:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from numpy.random import random

# Make plot with horizontal colorbar
fig, ax = plt.subplots()

data = random((250,250)) + 3.5

norm = matplotlib.colors.Normalize(vmin=2.5,vmax=4.5)

cax = ax.imshow(data, interpolation='nearest', cmap=cm.afmhot, norm=norm)
ax.set_title('Gaussian noise with horizontal colorbar')

TICKS = [1,2,3,4]

cbar = fig.colorbar(cax, ticks=TICKS, orientation='horizontal')

# the following command extracts the first tick object from the x-axis of
# the colorbar:
tick = cbar.ax.get_xaxis().get_major_ticks()[0]

# Here you compare the text of the first tick label to all the tick locations
# you have defined in TICKS (they need to be strings for this):
CUSTOM_INDEX = [str(S) for S in TICKS].index(tick.label1.get_text())

TICKLABELS = ['one','two', 'three', 'four']

# Now, you can use the index of the actual first tick as a starting tick to
# your list of custom labels:
cbar.set_ticklabels(TICKLABELS[CUSTOM_INDEX:])# horizontal colorbar

plt.show()

您可以获取第一个实际刻度,并将标签与指定刻度的值/位置进行比较。然后,您可以使用该索引启动自定义标签。
下面是一个经过修改的示例:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from numpy.random import random

# Make plot with horizontal colorbar
fig, ax = plt.subplots()

data = random((250,250)) + 3.5

norm = matplotlib.colors.Normalize(vmin=2.5,vmax=4.5)

cax = ax.imshow(data, interpolation='nearest', cmap=cm.afmhot, norm=norm)
ax.set_title('Gaussian noise with horizontal colorbar')

TICKS = [1,2,3,4]

cbar = fig.colorbar(cax, ticks=TICKS, orientation='horizontal')

# the following command extracts the first tick object from the x-axis of
# the colorbar:
tick = cbar.ax.get_xaxis().get_major_ticks()[0]

# Here you compare the text of the first tick label to all the tick locations
# you have defined in TICKS (they need to be strings for this):
CUSTOM_INDEX = [str(S) for S in TICKS].index(tick.label1.get_text())

TICKLABELS = ['one','two', 'three', 'four']

# Now, you can use the index of the actual first tick as a starting tick to
# your list of custom labels:
cbar.set_ticklabels(TICKLABELS[CUSTOM_INDEX:])# horizontal colorbar

plt.show()

您可以获取第一个实际刻度,并将标签与指定刻度的值/位置进行比较。然后,您可以使用该索引启动自定义标签。
下面是一个经过修改的示例:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from numpy.random import random

# Make plot with horizontal colorbar
fig, ax = plt.subplots()

data = random((250,250)) + 3.5

norm = matplotlib.colors.Normalize(vmin=2.5,vmax=4.5)

cax = ax.imshow(data, interpolation='nearest', cmap=cm.afmhot, norm=norm)
ax.set_title('Gaussian noise with horizontal colorbar')

TICKS = [1,2,3,4]

cbar = fig.colorbar(cax, ticks=TICKS, orientation='horizontal')

# the following command extracts the first tick object from the x-axis of
# the colorbar:
tick = cbar.ax.get_xaxis().get_major_ticks()[0]

# Here you compare the text of the first tick label to all the tick locations
# you have defined in TICKS (they need to be strings for this):
CUSTOM_INDEX = [str(S) for S in TICKS].index(tick.label1.get_text())

TICKLABELS = ['one','two', 'three', 'four']

# Now, you can use the index of the actual first tick as a starting tick to
# your list of custom labels:
cbar.set_ticklabels(TICKLABELS[CUSTOM_INDEX:])# horizontal colorbar

plt.show()

您可以获取第一个实际刻度,并将标签与指定刻度的值/位置进行比较。然后,您可以使用该索引启动自定义标签。
下面是一个经过修改的示例:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from numpy.random import random

# Make plot with horizontal colorbar
fig, ax = plt.subplots()

data = random((250,250)) + 3.5

norm = matplotlib.colors.Normalize(vmin=2.5,vmax=4.5)

cax = ax.imshow(data, interpolation='nearest', cmap=cm.afmhot, norm=norm)
ax.set_title('Gaussian noise with horizontal colorbar')

TICKS = [1,2,3,4]

cbar = fig.colorbar(cax, ticks=TICKS, orientation='horizontal')

# the following command extracts the first tick object from the x-axis of
# the colorbar:
tick = cbar.ax.get_xaxis().get_major_ticks()[0]

# Here you compare the text of the first tick label to all the tick locations
# you have defined in TICKS (they need to be strings for this):
CUSTOM_INDEX = [str(S) for S in TICKS].index(tick.label1.get_text())

TICKLABELS = ['one','two', 'three', 'four']

# Now, you can use the index of the actual first tick as a starting tick to
# your list of custom labels:
cbar.set_ticklabels(TICKLABELS[CUSTOM_INDEX:])# horizontal colorbar

plt.show()

这不是特定于
彩色条的内容。如果将记号标签指定为字符串列表,则它们始终(也在X或Y轴上)分配给第一个记号。请看这个简单的例子:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([0,1],[1,0])
ax.set_xticklabels(["one", "two", "three"])
这吸引了:

现在,勾号标签
one
two
three
分别对应于0.0、0.2和0.4

如果我们将其放大:

ax.set_xlim(.1,.9)
我们得到:

现在,相同的标签对应于0.1、0.2、0.3,因为它们是最左侧的标签

因此,标签字符串不会绑定到记号位置


有一些变通办法,但最好的办法实际上取决于你想完成什么。

这不是特定于
彩色条的。如果将记号标签指定为字符串列表,则它们始终(也在X或Y轴上)分配给第一个记号。请看这个简单的例子:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([0,1],[1,0])
ax.set_xticklabels(["one", "two", "three"])
这吸引了:

现在,勾号标签
one
two
three
分别对应于0.0、0.2和0.4

如果我们将其放大:

ax.set_xlim(.1,.9)
我们得到:

现在,相同的标签对应于0.1、0.2、0.3,因为它们是最左侧的标签

因此,标签字符串不会绑定到记号位置


有一些变通办法,但最好的办法实际上取决于你想完成什么。

这不是特定于
彩色条的。如果将记号标签指定为字符串列表,则它们始终(也在X或Y轴上)分配给第一个记号。请看这个简单的例子:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([0,1],[1,0])
ax.set_xticklabels(["one", "two", "three"])
这吸引了:

现在,勾号标签
one
two
three
分别对应于0.0、0.2和0.4

如果我们将其放大:

ax.set_xlim(.1,.9)
我们得到:

现在,相同的标签对应于0.1、0.2、0.3,因为它们是最左侧的标签

因此,标签字符串不会绑定到记号位置


有一些变通办法,但最好的办法实际上取决于你想完成什么。

这不是特定于
彩色条的。如果将记号标签指定为字符串列表,则它们始终(也在X或Y轴上)分配给第一个记号。请看这个简单的例子:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([0,1],[1,0])
ax.set_xticklabels(["one", "two", "three"])
这吸引了:

现在,勾号标签
one
two
three
分别对应于0.0、0.2和0.4

如果我们将其放大:

ax.set_xlim(.1,.9)
我们得到:

现在,相同的标签对应于0.1、0.2、0.3,因为它们是最左侧的标签

因此,标签字符串不会绑定到记号位置


有一些解决方法,但最好的解决方法实际上取决于您想要完成什么。

请让我完全理解:您希望能够指定独立于数据范围的刻度(其中一些可能在数据范围之外)。然后要指定自定义标签。打印时,matplotlib应该知道——在本例中——前两个记号超出范围(且未打印),因此仅使用第三个和第四个自定义标签。我还在github上写了一篇文章。这是
FixedFormatter
的正确行为,当您使用
set_ticklabels
时,您应该设置刻度本身,以控制标签的显示位置……我完全理解:您希望能够独立指定刻度(其中一些可能超出)数据范围。然后,您需要指定自定义标签。打印时,matplotlib应该知道(在本例中)前两个记号超出范围(且未打印)因此,只使用第三个和第四个自定义标签?没错。我还在github上写了一篇文章。这是
FixedFormatter
的正确行为,当您使用
set_ticklabels
时,您也应该设置标记本身,以控制标签的显示位置……让我完全理解:您要能够指定独立于数据范围的记号(其中一些可能在数据范围之外)。然后要指定自定义标签。打印时,matplotlib应该知道(在本例中)前两个记号在范围之外(并且未打印)因此,只使用第三个和第四个自定义标签?没错。我还在github上写了一篇文章。这是
FixedFormatter
的正确行为,当您使用
set_ticklabels
时,您也应该设置标记本身,以控制标签的显示位置……让我完全理解:您要能够指定独立于数据范围的记号(其中一些可能在数据范围之外)。然后要指定自定义标签。打印时,matplotlib应该知道(在本例中)前两个记号在范围之外(并且未打印)因此,只使用第三个和第四个自定义标签?没错。我还在github上写了一篇文章。这是
FixedFormatter
的正确行为,这就是您在使用