将配色杆添加到现有轴上
我正在制作一些交互式图,我想添加一个配色栏传奇。我不希望配色栏处于自己的轴上,因此我想将其添加到现有轴上。我很难做到这一点,因为我发现的大多数示例代码都为配色栏创建了一个新轴。
我已经使用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")
使用图。
matplotlib.colorbar.colorbarbase
仍然没有给我我想要的东西,我仍然不知道如何调整配色键的属性。
fig.colorbar(s, ax=ax, cax=ax)
假设我想在左上角放置配色栏,延伸到剧情顶部的一半。我该怎么做?
我最好为此编写自定义功能,也许是使用linecollection
?
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)
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(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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该技术通常用于图中的多个轴。在这种情况下,通常需要具有与Imshow结果相对应的ColorBar 。可以通过 axes grid 工具套件:
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 grid tool kit:
配色栏必须有自己的轴。但是,您可以创建一个与上一个重叠的轴。然后使用
CAX
kwarg来告诉图。Colorbar
使用新轴。例如:
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 tellfig.colorbar
to use the new axes.For example:
无法将其添加为注释,但是如果任何人有兴趣使用子图使用接受的答案,则应在特定轴对象上形成分隔线(而不是在numpy.ndarray上从plt.subplots返回)
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)