Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何设置matplotlib颜色栏范围?_Python_Matplotlib - Fatal编程技术网

Python 如何设置matplotlib颜色栏范围?

Python 如何设置matplotlib颜色栏范围?,python,matplotlib,Python,Matplotlib,我想在matplotlib imshow子图的旁边显示一个表示图像原始值的颜色条,该子图显示了该图像,并进行了规格化 我已经能够像这样成功地绘制图像和颜色条,但是颜色条的最小值和最大值表示标准化(0,1)图像,而不是原始(0,99)图像 如果有人对ColorBarAPI有更好的掌握,我很乐意听到你的消息 谢谢! Adam看起来您向colorbar构造函数传递了错误的对象 这应该起作用: # make namespace explicit from matplotlib import pyplot

我想在matplotlib imshow子图的旁边显示一个表示图像原始值的颜色条,该子图显示了该图像,并进行了规格化

我已经能够像这样成功地绘制图像和颜色条,但是颜色条的最小值和最大值表示标准化(0,1)图像,而不是原始(0,99)图像

如果有人对ColorBarAPI有更好的掌握,我很乐意听到你的消息

谢谢!
Adam

看起来您向colorbar构造函数传递了错误的对象

这应该起作用:

# make namespace explicit
from matplotlib import pyplot as PLT

cbar = fig.colorbar(result)
上面的代码片段基于您答案中的代码;下面是一个完整的独立示例:

import numpy as NP
from matplotlib import pyplot as PLT

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

cax = ax1.imshow(A, interpolation="nearest")

# set the tickmarks *if* you want cutom (ie, arbitrary) tick labels:
cbar = fig.colorbar(cax, ticks=[0, 5, 10])

# 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()
正如我在上面的评论中所建议的那样,我会选择一个更干净的名称空间——例如,在NumPy和Matplotlib中都有同名的模块

特别是,我将使用此import语句导入Matplotlib的“核心”绘图功能:

from matplotlib import pyplot as PLT

当然,这并不能得到整个matplotlib名称空间(这实际上是这个导入语句的要点),尽管这通常是您所需要的。

我知道这可能太晚了,但是…

对我来说,用ax替换Adam代码最后一行的
结果
是可行的。

出于兴趣@doug,为什么要大写PLT和NP?@bdforbes:所以我喜欢干净的名称空间(没有重叠),而且我喜欢用一种快速的视觉方式在代码中识别它们——当然这里,PLT和NP都是绑定到包含其他模块的库/模块的局部变量。
from matplotlib import pyplot as PLT