desa
desa

Reputation: 1390

Change the width of spines in colobar axis

I would like to change the width of spines for both axis:

img = np.random.normal(size=(100, 100))

fig, ax = plt.subplots()
cax = ax.imshow(img)

cbar = fig.colorbar(cax, ticks=np.linspace(0, 70, 8))
[i.set_linewidth(3) for i in cbar.ax.spines.itervalues()]
[i.set_linewidth(3) for i in ax.spines.itervalues()]

However, as the result I get the following:

enter image description here

How can I change the length of the spines for the colorer axis?

Upvotes: 5

Views: 10446

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339430

You may try to set the frame of the colorbar axes on again, such that the changes to its spines can take an effect.

cbar.ax.set_frame_on(True)

Complete example (only tested with matplotlib 2.0)

import matplotlib.pyplot as plt
import numpy as np

img = np.random.normal(size=(100, 100))

fig, ax = plt.subplots()
im = ax.imshow(img)

cbar = fig.colorbar(im, ticks=np.linspace(0, 70, 8))
cbar.ax.set_frame_on(True)
[i.set_linewidth(3) for i in cbar.ax.spines.itervalues()]
[i.set_linewidth(3) for i in ax.spines.itervalues()]

plt.show()

enter image description here

Upvotes: 3

bear24rw
bear24rw

Reputation: 4627

I would have expected your solution to work but a possible work around is to set axes.linewidth and remove the set_linewidth(3) lines

import matplotlib
matplotlib.rcParams['axes.linewidth'] = 3

I figured out this work around by finding this:

https://github.com/matplotlib/matplotlib/blob/ac8f340c9a771871c894aecdc12a9111958271dd/lib/matplotlib/colorbar.py#L429

edit

Alternatively you can set the outline line width directly:

img = np.random.normal(size=(100, 100))

fig, ax = plt.subplots()
cax = ax.imshow(img)

cbar = fig.colorbar(cax, ticks=np.linspace(0, 70, 8))
cbar.outline.set_linewidth(3)
[i.set_linewidth(3) for i in ax.spines.itervalues()]

Upvotes: 7

Related Questions