Python 将颜色条添加到现有轴
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32462881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add colorbar to existing axis
提问by C_Z_
I'm making some interactive plots and I would like to add a colorbar legend. I don't want the colorbar to be in its own axes, so I want to add it to the existing axes. I'm having difficulties doing this, as most of the example code I have found creates a new axes for the colorbar.
我正在制作一些交互式绘图,我想添加一个颜色条图例。我不希望颜色条在它自己的轴上,所以我想将它添加到现有的轴中。我在这样做时遇到了困难,因为我发现的大多数示例代码都为颜色条创建了一个新轴。
I have tried the following code using matplotlib.colorbar.ColorbarBase
, which adds a colorbar to an existing axes, but it gives me strange results and I can't figure out how to specify attributes of the colorbar (for instance, where on the axes it is placed and what size it is)
我已经尝试使用以下代码matplotlib.colorbar.ColorbarBase
,它向现有轴添加了一个颜色条,但它给了我奇怪的结果,我无法弄清楚如何指定颜色条的属性(例如,它在轴上的位置以及放置什么尺寸是)
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.cm import coolwarm
import numpy as np
x = np.random.uniform(1, 10, 10)
y = np.random.uniform(1, 10, 10)
v = np.random.uniform(1, 10, 10)
fig, ax = plt.subplots()
s = ax.scatter(x, y, c=v, cmap=coolwarm)
matplotlib.colorbar.ColorbarBase(ax=ax, cmap=coolwarm, values=sorted(v),
orientation="horizontal")
Using fig.colorbar
instead ofmatplotlib.colorbar.ColorbarBase
still doesn't give me quite what I want, and I still don't know how to adjust the attributes of the colorbar.
使用fig.colorbar
而不是matplotlib.colorbar.ColorbarBase
仍然没有给我想要的东西,我仍然不知道如何调整颜色条的属性。
fig.colorbar(s, ax=ax, cax=ax)
Let's say I want to have the colorbar in the top left corner, stretching about halfway across the top of the plot. How would I go about doing that?
假设我想在左上角设置颜色条,大约在绘图顶部的一半处。我该怎么做呢?
Am I better off writing a custom function for this, maybe using LineCollection
?
我最好为此编写一个自定义函数,也许使用LineCollection
?
采纳答案by Joe Kington
The colorbar has to have its own axes. However, you can create an axes that overlaps with the previous one. Then use the cax
kwarg to tell fig.colorbar
to use the new axes.
颜色条必须有自己的轴。但是,您可以创建一个与前一个轴重叠的轴。然后使用cax
kwarg 告诉fig.colorbar
使用新轴。
For example:
例如:
import numpy as np
import matplotlib.pyplot as plt
data = np.arange(100, 0, -1).reshape(10, 10)
fig, ax = plt.subplots()
cax = fig.add_axes([0.27, 0.8, 0.5, 0.05])
im = ax.imshow(data, cmap='gist_earth')
fig.colorbar(im, cax=cax, orientation='horizontal')
plt.show()
回答by Ramon Martinez
This technique is usually used for multiple axis in a figure. In this context it is often required to have a colorbar that corresponds in size with the result from imshow.This can be achieved easily with the axes gridtool kit:
这种技术通常用于图形中的多个轴。在这种情况下,通常需要有一个颜色条,其大小与 imshow 的结果相对应。这可以通过轴网格工具套件轻松实现:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
data = np.arange(100, 0, -1).reshape(10, 10)
fig, ax = plt.subplots()
divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size='5%', pad=0.05)
im = ax.imshow(data, cmap='bone')
fig.colorbar(im, cax=cax, orientation='vertical')
plt.show()
回答by Shir Portugez
Couldn't add this as a comment, but in case anyone is interested in using the accepted answer with subplots, the divider should be formed on specific axes object (rather than on the numpy.ndarray returned from plt.subplots)
无法将其添加为评论,但如果有人有兴趣将已接受的答案与子图一起使用,则应在特定轴对象上形成分隔线(而不是在从 plt.subplots 返回的 numpy.ndarray 上)
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
data = np.arange(100, 0, -1).reshape(10, 10)
fig, ax = plt.subplots(ncols=2, nrows=2)
for row in ax:
for col in row:
im = col.imshow(data, cmap='bone')
divider = make_axes_locatable(col)
cax = divider.append_axes('right', size='5%', pad=0.05)
fig.colorbar(im, cax=cax, orientation='vertical')
plt.show()