KarinP
KarinP

Reputation: 153

How to unite the color bar for hist2d subplots?

I created a multi-subplot plot in matplotlib using the hist2d function. Each one of the subplots shows a different range of data, and the fourth subplot shows the sum of all the other subplots.

Everything is perfect except the fact that the color bar isn't united for all the subplots... I saw that all of the solutions for this problem require converting the data to an image (imshow for example), and that really messes up my plot.

I want the plots to stay the way they are and just change the colors. Does anyone have an idea how to do it?

Thanks :-)

Upvotes: 1

Views: 870

Answers (2)

Adnan
Adnan

Reputation: 61

Here is the easiest approach:

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10,5))
b=ax1.hist2d(x_b, y_b, bins =[x_bins, y_bins], cmap='Blues')

ax1.set_xlim([-130.05, -129.95])
ax1.set_ylim([45.90, 46])
ax1.set_yticks([45.90, 45.925, 45.95, 45.975, 46])
ax1.set_xticks([-130.05, -130.025, -130, -129.975, -129.95])
cb1=plt.colorbar(b[3], ax=ax1, shrink=0.80)
cb1.set_label('Observations/day', fontsize=10)
ax1.set_ylabel('Latitude', fontsize=10)
ax1.set_xlabel('Longitude', fontsize=10)

Upvotes: 0

KarinP
KarinP

Reputation: 153

I researched more about the topic and seems that vmin and vmax is after all the solution.

If you write your code this way-

 hist = ax.hist2d(x, y, bins=(500, 200), norm= LogNorm(vmin=min_value, vmax= max_value))

then you can control the borders of your plots. If you loop over all of your subplots you can configure this vmin and vmax to all of them- so the scale color is uniform!

To get your vmin and vmax just run this code-

vmin, vmax = hist[-1].get_clim()

Upvotes: 2

Related Questions