making scatter plots with histograms with equal axes (scatter_hist.py)

hi all,

i am trying to make scatter plots with marginal histograms shown in the same plot, using the recently checked in example ‘scatter_hist.py’. i want the scatter plot to have an equal aspect ratio, but when i do this, the scales of the marginal histograms get out of sync. for example, the code below makes the main scatter plot look as it should but is out of sync with the scales of the histograms:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import NullFormatter
from numpy.random import normal
from frame import FrameAxes
from numpy import *
def scatter_hist(x, y, num_bins=20):
nullfmt = NullFormatter() # no labels

left, width = 0.1, 0.65
bottom, height = 0.1, 0.65
bottom_h = left_h = left+width+0.02

rect1 = [left, bottom, width, height]
rect2 = [left, bottom_h, width, 0.2]
rect3 = [left_h, bottom, 0.2, height]
axScatter = plt.axes(rect1)
axHistx = plt.axes(rect2)#, sharex=axScatter)
axHisty = plt.axes(rect3)#, sharey=axScatter)
axHistx.xaxis.set_major_formatter(nullfmt)
axHisty.yaxis.set_major_formatter(nullfmt)
axScatter.set_aspect('equal')
axScatter.scatter(x, y)
c = .05
bins = np.linspace(min(x)-c,max(x)+c,num_bins)
axHistx.hist(x, bins=bins)
axHisty.hist(y, bins=bins, orientation='horizontal')

axHistx.set_xlim(axScatter.get_xlim())
axHisty.set_ylim(axScatter.get_ylim())

x = np.random.randn(1000)
y = np.random.randn(1000)
scatter_hist(x,y)
plt.show()

if i pass the ‘sharex’ and ‘sharey’ arguments to ‘axes’ (see commented lines in code above), then the dimensions are in sync, but the NullFormatter from these lines:

axHistx.xaxis.set_major_formatter(nullfmt)
axHisty.yaxis.set_major_formatter(nullfmt)

removes the axes from the main scatter plot. i simply want the scatter plot to have both x and y axes, and the marginal histograms to only have the yaxis, but have their dimensions be in sync with the square scatter plot. how can i do this?

thanks very much.