gpt4 book ai didi

python - matplotlib:有什么方法可以获取现有的颜色条?

转载 作者:行者123 更新时间:2023-11-28 18:40:06 25 4
gpt4 key购买 nike

在 matplotlib 的面向对象风格中,您可以获得现有图形中的当前轴、线和图像:

fig.axes
fig.axes[0].lines
fig.axes[0].images

但是我还没有找到获取现有颜色条的方法,我必须在第一次创建颜色条时为其分配一个名称:

cbar = fig.colorbar(image)

如果我没有为它们分配名称,是否有任何方法可以获取给定图形中的颜色条对象?

最佳答案

问题是颜色条被添加为“只是另一个”轴,因此它将与“正常”轴一起列出。

import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(6,6)
fig = plt.figure(1)
fig.clf()
ax = fig.add_subplot(1,1,1)
cax = ax.imshow(data, interpolation='nearest', vmin=0.5, vmax=0.99)
print "Before adding the colorbar:"
print fig.axes
fig.colorbar(cax)
print "After adding the colorbar:"
print fig.axes

对我来说,这给出了结果:

Before adding the colorbar:
[<matplotlib.axes._subplots.AxesSubplot object at 0x00000000080D1D68>]
After adding the colorbar:
[<matplotlib.axes._subplots.AxesSubplot object at 0x00000000080D1D68>,
<matplotl ib.axes._subplots.AxesSubplot object at 0x0000000008268390>]

也就是说,你的图中有两个轴,第二个是新的颜色条。

编辑:代码基于此处给出的答案: https://stackoverflow.com/a/2644255/2073632

关于python - matplotlib:有什么方法可以获取现有的颜色条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27325149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com