-1

Here is my example:

import numpy as np
import matplotlib.pyplot as plt
a1 = np.random.randn(10000)
b1 = np.random.randn(10000)
a2 = np.random.randn(5000)
b2 = np.random.randn(5000)
fig, axs = plt.subplots(1, 2, figsize=(9, 4))
ax = axs[0]
m = ax.hist2d(a1, b1, bins=30)
fig.colorbar(m[3], ax=ax)
ax = axs[1]
m = ax.hist2d(a2, b2, bins=30)
fig.colorbar(m[3], ax=ax)
plt.tight_layout()

The output figure is like this: enter image description here

My question is how can I change the color range of the right figure, so that its maxium color values is 115 instead of 66?(Note: Not just change the values of colorbar, the color of the same color values should be same with the left figure.)

I tried set the cmax keywords like this:

m = ax.hist2d(a2, b2, bins=30, cmax=115)

But the results have no change. So how should I do?

keeptg
  • 27
  • 6
  • 2
    Use `ax.hist2d(..., vmin=0, vmax=115)` for both 2d histograms for them to have the same range of colors. Note that `cmax` is a cutoff: all values higher than `cmax` will be invisible (as will all values lower than `cmin`). – JohanC Apr 19 '20 at 22:17
  • @JohanC Thanks very well! – keeptg Apr 20 '20 at 00:39
  • I'm not sure why you got the down votes here. This seems like a good question to me. – tom10 Apr 20 '20 at 00:46
  • @tom10 Maybe it just because those parameters like```vmin```, ```vmax``` are some basic keywords parameters. But that's OK. I've understood them now. Thanks for all of your votes and answers – keeptg Apr 20 '20 at 18:30

1 Answers1

3

You want to add vmin and vmax ranges explicitly to get desired result. To answer your question -

import numpy as np
import matplotlib.pyplot as plt
a1 = np.random.randn(10000)
b1 = np.random.randn(10000)
a2 = np.random.randn(5000)
b2 = np.random.randn(5000)
fig, axs = plt.subplots(1, 2, figsize=(9, 4))
ax = axs[0]
m = ax.hist2d(a1, b1, bins=30)
fig.colorbar(m[3], ax=ax)
ax = axs[1]
m = ax.hist2d(a2, b2, bins=30, vmax = 115) # Added Vmax here.
fig.colorbar(m[3], ax=ax)
plt.tight_layout()
lostin
  • 720
  • 4
  • 10