我想通过添加白色背景使我的颜色条更加明显。我需要图像内部的颜色条,这有时会让人难以阅读。
这是没有白色背景的代码。
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()
关于python - 在 matplotlib 中为颜色条添加白色背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35950559/