This question leads on from a previous question I asked yesterday that got me most of the way to what I am after: Rotate transformation on matplotlib axis in subplot
I want to create a two by two array of graphs where the bottom-left is a scatter plot and the other three are histograms projecting the x, y, and x-y data from that plot. That final histogram I want to have at a 45 degree angle, and it is the positioning of that plot which I am trying to adjust.
Currently, I have this:
from matplotlib.transforms import Affine2D
import mpl_toolkits.axisartist.floating_axes as floating_axes
import matplotlib.pyplot as plt
def setup_axes(fig, rect, rotation, axisScale, axisLimits, doShift):
tr_rot = Affine2D().scale(axisScale[0], axisScale[1]).rotate_deg(rotation)
# This seems to do nothing
if doShift:
tr_trn = Affine2D().translate(-90,-5)
else:
tr_trn = Affine2D().translate(0,0)
tr = tr_rot + tr_trn
grid_helper = floating_axes.GridHelperCurveLinear(tr, extremes=axisLimits)
ax = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
fig.add_subplot(ax)
aux_ax = ax.get_aux_axes(tr)
return ax, aux_ax
fig = plt.figure(1, figsize=(8, 8))
axes = []
axisOrientation = [0, 0, 270, -45]
axisScale = [[1,1],[6,1],[6,1],[6,1]]
axisPosition = [223,221,224,222]
axisLimits = [(-0.5, 4.5, -0.5, 4.5),
(-0.5, 4.5, 0, 12),
(-0.5, 4.5, 0, 12),
(-3.5, 3.5, 0, 12)]
doShift = [False, False, False, True]
label_axes = []
for i in range(0, len(axisOrientation)):
ax, aux_ax = setup_axes(fig, axisPosition[i], axisOrientation[i],
axisScale[i], axisLimits[i], doShift[i])
axes.append(aux_ax)
label_axes.append(ax)
numPoints = 100
x = []
y = []
for i in range(0,numPoints):
x.append(np.random.rand() + i/100.0)
y.append(np.random.rand() + i/100.0 + np.mod(i,2)*2)
axes[0].plot(x,y,ls='none',marker='x')
label_axes[0].axis["bottom"].label.set_text('Variable 1')
label_axes[0].axis["left"].label.set_text('Variable 2')
b = np.linspace(-0.5,4.5,50)
axes[1].hist(x, bins = b)
axes[2].hist(y, bins = b)
b = np.linspace(-3.5,3.5,50)
axes[3].hist(np.array(x)-np.array(y), bins=b)
for i in range(1,len(label_axes)):
for axisLoc in ['top','left','right']:
label_axes[i].axis[axisLoc].set_visible(False)
label_axes[i].axis['bottom'].toggle(ticklabels=False)
fig.subplots_adjust(wspace=-0.30, hspace=-0.30, left=0.00, right=0.99, top=0.99, bottom=0.0)
plt.show()
Which gives:
As you can see in the code, I try to shift the position of that top-right plot with an Affine2D().translate()
but it seems to have no effect. Does anybody know how I might move this plot so that its x-axis almost-touches the top-right corner of the bottom-left plot's axes?
Edit:
I have also just noticed that the bottom-right plot is upside-down compared to how it should be. It needs to be top-bottom mirrored somehow.
Edit 2:
This code before fig.subplots_adjust() will fix that:
label_axes[2].invert_yaxis()