python - 在 matplotlib 中为颜色条添加白色背景

标签 python matplotlib

我想通过添加白色背景使我的颜色条更加明显。我需要图像内部的颜色条,这有时会让人难以阅读。

这是没有白色背景的代码。

import matplotlib.pyplot as plt
import numpy as np

a=np.random.rand(10,10)

fig=plt.figure()
ax=fig.add_axes([0,0,1,1]) #fill the entire axis
im=ax.imshow(a)
cbaxes=fig.add_axes([0.8,0.1,0.03,0.8]) #add axis for colorbar, define size
cb=fig.colorbar(im,cax=cbaxes) #make colorbar
#cb.outline.set_color('white') #this does not work
fig.show()

最佳答案

下面是创建背景的解决方案:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

a=np.random.rand(10,10)

fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
im=ax.imshow(a)

cbbox = inset_axes(ax, '15%', '90%', loc = 7)
[cbbox.spines[k].set_visible(False) for k in cbbox.spines]
cbbox.tick_params(axis='both', left='off', top='off', right='off', bottom='off', labelleft='off', labeltop='off', labelright='off', labelbottom='off')
cbbox.set_facecolor([1,1,1,0.7])

cbaxes = inset_axes(cbbox, '30%', '95%', loc = 6)

cb=fig.colorbar(im,cax=cbaxes) #make colorbar

fig.show()

enter image description here

关于python - 在 matplotlib 中为颜色条添加白色背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35950559/

相关文章:

python - 识别图形上升趋势或下降趋势

python - 使用第三列值将颜色渐变映射到第 1 列和第 2 列的图; Matplotlib

python - 在 matplotlib 中动画化 3d 散点图

python - 更新条目中的文本 (Tkinter)

python - 使用 matplotlib 子图时减少行之间的间隙?

python - 如何缓存依赖于其他类属性的方法返回的值?

python - Matplotlib 箭袋绘图,箭头大小不变

python - 是否有类似于运行 R-studio 服务器的类似 Python 服务器端 IDE(如 Spyder)?

python - basemap 导致python中止

python - Matplotlibrc 需要更新吗?