I know that this topic has been addressed already in several matplotlib blogs, but I still could not find a solution to this. I want to create a plot like this:
(plot from the answer of @ImportanceOfBeingErnest)
So these are two subplots which share the same y-axis with y-limits that I define. The x-axis should be in equal units as the y-axis, meaning if I would draw a circle, it would really be a circle.
Now I would like to also specify x-limits and like the subplot size to adjust to this limits, but I cannot make it work. The general problem seems to be that matplotlib always keeps the fig-size of the different subplots.
Here is a minimal working example:
import matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()
ax1=fig.add_subplot(1,2,1,aspect='equal')
ax2=fig.add_subplot(1,2,2,aspect='equal',sharey=ax1)
def create_data(xmin,xmax):
delta = 0.025
----
x = np.arange(xmin, xmax, delta)
y = np.arange(-3,3,delta)
X, Y = np.meshgrid(x, y)
Z1 = plt.mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = plt.mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 10 * (Z1 - Z2)
----
nr, nc = Z.shape
----
# put NaNs in one corner:
Z[-nr//6:, -nc//6:] = np.nan
# contourf will convert these to masked
----
----
Z = np.ma.array(Z)
# mask another corner:
Z[:nr//6, :nc//6] = np.ma.masked
----
# mask a circle in the middle:
interior = np.sqrt((X**2) + (Y**2)) < 0.5
Z[interior] = np.ma.masked
return X,Y,Z
X,Y,Z=create_data(-2,4)
ax1.contourf(X,Y,Z)
ax1.set_ylim(-1,1)
ax1.set_xlim(-2,4)
X,Y,Z=create_data(-1,0)
ax2.contourf(X,Y,Z)
ax2.set_ylim(-1,1)
ax2.set_xlim(-1,0)
plt.show()
In this example the y-axis is shared but the x-limits are not applied correctly:
Further, how can I add a colorbar to the right of the subplots aligned to the y-axis?
fig.colorbar(CS, ax=ax,shrink=xx)
seems to work but requires to manually edit the shrink parameter.