varantir
varantir

Reputation: 6854

Colorbar breaks axes

I have the problem that a colorbar breaks the scaling of my axis, and I have no idea why. Consider the following code

import matplotlib.pyplot as plt
import numpy as np
fig, axes = plt.subplots(1,3,figsize = (8,6/Φ), sharey=True,gridspec_kw={"width_ratios":[1,1, 0.05]})
(ax1,ax2,cax) = axes

x_ = np.linspace(-1,0,350)
y_ = np.linspace(0,1,350)
X,Y = np.meshgrid(x_,y_,indexing = "ij")

dq = -Y/X
dq[dq>1] = 1

im1 = ax1.pcolormesh(X, Y*1e28, (1+dq)*1e12, vmin=0, vmax=2e12,cmap = "inferno",)

im2 = ax2.pcolormesh(X, Y*1e28, (1-dq)*1e12, vmin=0, vmax=2e12,cmap = "inferno")

plt.show()

which creates

Figure 1

Now, I add a colorbar as following

fig.colorbar(im1,  cax=cax)

I get the following result

Figure 2

Can you tell me what goes wrong here?! Also note the funny bug that the 1e28 of the y-axis has been replaced by a 1e12!

Thanks in advance for your help

Upvotes: 1

Views: 193

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339340

You are sharing the plot axes with the colorbar axes. This is not useful since they would in general be scaled very differently.

Use sharey=False to obtain the desired plot.

Upvotes: 6

Related Questions