Setting range of colorbar in the matplotlib hist2d plot
Question:
Answers:
Add vmin=0, vmax=1
to the call to plt.hist2d
:
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import numpy as np
np.random.seed(2016)
N = 10**3
x_values = (np.random.random(size=N))*15
y_values = np.random.random(size=N)
plt.hist2d(x_values, y_values, bins=50, normed=True, vmin=0, vmax=1)
plt.colorbar(norm=mcolors.NoNorm)
plt.show()
The colorbar always associates itself to a single colorable artist. When no artist is supplied (as the mappable
), the current colorable artist is used. In this case, it is the artist created by plt.hist2d
. The vmin
and vmax
values of that artist affect the range of values displayed by the colorbar.
Add vmin=0, vmax=1
to the call to plt.hist2d
:
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import numpy as np
np.random.seed(2016)
N = 10**3
x_values = (np.random.random(size=N))*15
y_values = np.random.random(size=N)
plt.hist2d(x_values, y_values, bins=50, normed=True, vmin=0, vmax=1)
plt.colorbar(norm=mcolors.NoNorm)
plt.show()
The colorbar always associates itself to a single colorable artist. When no artist is supplied (as the mappable
), the current colorable artist is used. In this case, it is the artist created by plt.hist2d
. The vmin
and vmax
values of that artist affect the range of values displayed by the colorbar.