Skip to content

Improvements and bugfixes for hexbin marginals #18875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 26 additions & 25 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4603,6 +4603,9 @@ def reduce_C_function(C: array) -> float
y = np.log10(y)
if extent is not None:
xmin, xmax, ymin, ymax = extent
if xmin > xmax or ymin > ymax:
raise ValueError("extent values in wrong order. Should be"
" (left, right, bottom, top)")
else:
xmin, xmax = (np.min(x), np.max(x)) if len(x) else (0, 1)
ymin, ymax = (np.min(y), np.max(y)) if len(y) else (0, 1)
Expand Down Expand Up @@ -4783,32 +4786,28 @@ def reduce_C_function(C: array) -> float
if not marginals:
return collection

if C is None:
C = np.ones(len(x))

def coarse_bin(x, y, coarse):
ind = coarse.searchsorted(x).clip(0, len(coarse) - 1)
mus = np.zeros(len(coarse))
for i in range(len(coarse)):
yi = y[ind == i]
if len(yi) > 0:
mu = reduce_C_function(yi)
else:
mu = np.nan
mus[i] = mu
ind = coarse.searchsorted(x)
mus = np.zeros(len(coarse)-1)
if y is None:
for i in range(len(coarse)-1):
mus[i] = np.sum(ind == i+1)
else:
for i in range(len(coarse)-1):
yi = y[ind == i+1]
if len(yi) > 0:
mus[i] = reduce_C_function(yi)
mus[mus == 0] = np.nan
return mus

coarse = np.linspace(xmin, xmax, gridsize)
coarse = np.linspace(xmin, xmax, nx+1)

xcoarse = coarse_bin(xorig, C, coarse)
valid = ~np.isnan(xcoarse)
verts, values = [], []
for i, val in enumerate(xcoarse):
thismin = coarse[i]
if i < len(coarse) - 1:
thismax = coarse[i + 1]
else:
thismax = thismin + np.diff(coarse)[-1]
thismax = coarse[i + 1]

if not valid[i]:
continue
Expand All @@ -4831,27 +4830,29 @@ def coarse_bin(x, y, coarse):
hbar.update(kwargs)
self.add_collection(hbar, autolim=False)

coarse = np.linspace(ymin, ymax, gridsize)
coarse = np.linspace(ymin, ymax, ny+1)

ycoarse = coarse_bin(yorig, C, coarse)
valid = ~np.isnan(ycoarse)
verts, values = [], []
for i, val in enumerate(ycoarse):
thismin = coarse[i]
if i < len(coarse) - 1:
thismax = coarse[i + 1]
else:
thismax = thismin + np.diff(coarse)[-1]
thismax = coarse[i + 1]

if not valid[i]:
continue
verts.append([(0, thismin), (0.0, thismax),
(0.05, thismax), (0.05, thismin)])

verts.append([(0, thismin),
(0, thismax),
(0.05, thismax),
(0.05, thismin)])
values.append(val)

values = np.array(values)

trans = self.get_yaxis_transform(which='grid')

vbar = mcoll.PolyCollection(verts, transform=trans, edgecolors='face')

vbar.set_array(values)
vbar.set_cmap(cmap)
vbar.set_norm(norm)
Expand Down