Search code examples
pythonmatplotlibplotcolorbar

How can I set the tick labels of my colorbar to a different color?


I am creating an image using the function below. Because I later want to integrate it into a window, I am changing the background color and as a result the text is not visible with the default color. I want to change the tick labels of my colorbar to be displayed in white (or any contrasting color).

Below you can see what I am currently doing:

 def create_image(self):

        fig, ax = plt.subplots(figsize=(10, 10), facecolor=BACKGROUND_COLOR)
        ax.set_aspect('equal', 'box')

        values = [hexagon['value'] for hexagon in self.sensorMatrix]
        cmap = plt.get_cmap(custom_colormap)
        norm = colors.LogNorm(vmin=np.min([i for i in values if i != 0]), vmax=np.max(values))

        for i, hexagon in enumerate(self.sensorMatrix):

            hexagon_color = cmap(norm(hexagon['value']))
            hexagon_edge = hexagon['edge']

            hexagon_coords = self.coordinates[i]
            hexagon_polygon = plt.Polygon(hexagon_coords, edgecolor=hexagon_edge, facecolor=hexagon_color, linewidth=1)

            ax.add_patch(hexagon_polygon)

        ax.set_xlim(-2, 3 * self.row_length + 3)
        ax.set_ylim(-2, 3 * self.row_length + 3)
        ax.axis('off')

        divider = make_axes_locatable(ax)
        cax = divider.append_axes('right', size='5%', pad=0.05)


        sm = cm.ScalarMappable(cmap=cmap, norm=norm)
        sm.set_array([])

        cbar = plt.colorbar(sm, cax=cax, orientation='vertical')
        cbar.set_label('ADC value', color=WHITE, rotation=270, labelpad=10)
        cbar.ax.tick_params(colors=WHITE)
        cbar.outline.set_edgecolor(WHITE)
        plt.show()

For some reason, cbar.ax.tick_params(colors=WHITE) does nothing to change the color of the tick labels, though all the other edits work.

"WHITE" is just defined elsewhere as #FFF so it should work. I have also tried with 'white'. I have tried looking for other arguments/methods for axes or the tick labels, including iterating over the ticks in a loop but nothing works.

Here is an example of the generated image

Every other color change in the function works aside from the one for ticks.


Solution

  • It seems that plt.colorbar() was adding minor ticks to the generated colorbar, as opposed to major ticks. I assumed that major ticks are the default as I found minor ticks to be generally OFF, though in this case it was wrong.

    @JohanC provided the solution with cbar.ax.tick_params(which='both', color='white', labelcolor='white'). It seems that LogNorm may be the culprit (still unsure as to how ticks are determined here and whether major ticks may be visible instead for a different range)

    Here is the colorbar segment as I currently have it:

    cbar = plt.colorbar(sm, cax=cax, orientation='vertical')
    cbar.set_label('ADC value', color=WHITE, rotation=270, labelpad=10)
    cbar.ax.tick_params(which='minor', color=WHITE, labelcolor='white')
    cbar.set_ticks([])