Manos Bougioukoglou
Manos Bougioukoglou

Reputation: 21

Setting limits of the colorbar in Python

enter image description here

I made a contour plot and a colorbar to show the range of the values I plot. The limits of the colorbar are (-0.4, 0.4) and I would like to convert them to (0, 100) with a step 20, so 0, 20, 40, 60, 80 and 100.

I tried to do that with:

plt.clim(0, 100)

plt.colorbar(label="unit name", orientation="vertical")

but instead the colorbar's limits remain the same and my plot changes color, it becomes purple.

Could you please help me with this?

Upvotes: 1

Views: 3070

Answers (1)

romain gal
romain gal

Reputation: 413

If you translate your array of [-0.4, 0.4] values to the range [0, 100], you will be able to plot what you need.

This is what I did in the example below :

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

size = 50
A = np.array([randint(-4, 4)/10 for _ in range(size*size)])
A = (A*10 + 4)*12.5    # Translate values from [-0.4, 0.4] to [0, 100].
B = A.reshape((size, size))

ax = plt.subplot()

im = ax.imshow(B)
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
im.set_clim(0, 100)
plt.colorbar(im, cax=cax)
plt.show()

Output: enter image description here

Upvotes: 1

Related Questions