【问题标题】:How can you change the spine color of a matplotlib colorbar?如何更改 matplotlib 颜色条的脊椎颜色?
【发布时间】:2020-04-12 06:15:21
【问题描述】:

我只修改了this basic example一点点,这样颜色条的脊椎颜色就会改变。 有趣的是,只有蜱的颜色在变化,而脊椎的颜色没有变化。我做错了什么?

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np

fig, ax = plt.subplots()
im = ax.imshow(np.arange(100).reshape((10, 10)))

# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)

colorbar = fig.colorbar(im, cax=cax)
colorbar.ax.tick_params(axis='both', colors='#f9f2d7')
colorbar.ax.spines['bottom'].set_color('#f9f2d7')
colorbar.ax.spines['top'].set_color('#f9f2d7')
colorbar.ax.spines['right'].set_color('#f9f2d7')
colorbar.ax.spines['left'].set_color('#f9f2d7')

fig.canvas.show()

【问题讨论】:

    标签: python matplotlib colors colorbar spine


    【解决方案1】:

    你可以使用 colorbar.ax.spines ... 代替:

    colorbar.outline.set_edgecolor('#f9f2d7')
    

    这适用于颜色条的所有 4 个边/脊。

    【讨论】:

    • 谢谢!这在示例中有效,但是,它对我更复杂的程序没有影响,我无法弄清楚为什么......非常奇怪。
    • 所以当我打印出colorbar.outline.get_edgecolor() 之前和之后我改变它时,返回值也从(0.0, 0.0, 0.0, 1.0) 变为(0.9764705882352941, 0.9490196078431372, 0.8431372549019608, 1.0) 所以它确实做了一些事情,但它没有显示某种原因..
    • 好吧,仅供参考,如果你使用im.set_clim()然后重绘,你还需要重置边缘颜色,但勾号颜色仍然存在。很奇怪。
    猜你喜欢
    • 1970-01-01
    • 2019-09-13
    • 2021-01-07
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2010-12-31
    相关资源
    最近更新 更多