Skip to content Skip to sidebar Skip to footer

Altering Height Range Of Matplotlib Histogram2d

I am trying to plot some 2D empirical probability distributions using matplotlib's histogram2d. I want the colours to be on the same scale across several different plots, but canno

Solution 1:

In general, the color scaling of most things in matplotlib is controlled by the vmin and vmax keyword arguments.

You have to read between the lines a bit, but as the documentation mentions, additional kwargs in hist2d are passed on to pcolorfast. Therefore, you can specify the color limits through the vmin and vmax kwargs.

For example:

import numpy as np
import matplotlib.pyplot as plt

small_data = np.random.random((2, 10))
large_data = np.random.random((2, 100))

fig, axes = plt.subplots(ncols=2, figsize=(10, 5), sharex=True, sharey=True)

# For consistency's sake, we'll set the bins to be identical
bins = np.linspace(0, 1, 10)

axes[0].hist2d(*small_data, bins=bins, vmin=0, vmax=5)
axes[1].hist2d(*large_data, bins=bins, vmin=0, vmax=5)

plt.show()

enter image description here

Post a Comment for "Altering Height Range Of Matplotlib Histogram2d"