matplotlib icon indicating copy to clipboard operation
matplotlib copied to clipboard

Hexbin overlapping hexagons

Open lowderchris opened this issue 10 years ago • 4 comments

Hi! I'm running macOS Sierra with homebrew-installed python 2.7.12/3.5.2, and pip-installed matplotlib 1.5.2.

I'm running into a problem with the hexbin visualization routine, where my hexagonal bins are overlapping on top of one another to a varying degree. I suspected it might be an issue with automatic resizing of axes to insert a colorbar, but I'm not so sure. I noticed that the hexbin examples on the matplotlib website suffer from this same issue. I've attached a simple bit of code to reproduce this issue, and a sample plot.

Thanks!

import matplotlib
from matplotlib.pyplot import *
import numpy as np

x = np.random.exponential(size=10000)
y = np.random.exponential(size=10000)

f, (ax) = subplots(1,figsize=[5,4])
hb = hexbin(x,y,cmap='Greys',gridsize=50)
cb = colorbar()
ax.set_xlabel(r'Something')
ax.set_ylabel(r'Something else')
ax.set_xlim(0,4)
ax.set_ylim(0,4)
tight_layout()
savefig('hexbin_test.pdf')

hexbin_test.pdf

lowderchris avatar Oct 20 '16 12:10 lowderchris

The overlapping is caused by stroking the edges of the hexagons. The problem is that if this is turned off, there are often artifacts at the boundaries. All one can do is use thinner lines around the edges, e.g., include the kwarg linewidths=0.2.

efiring avatar Oct 20 '16 17:10 efiring

Ah, that seems to have worked for the most part. For an actual plot I was making, the problem was a bit more severe than in the example. See attached zoomed in portion: hex-overlap-before

But after repeating the same procedure with reduced linewidths, that seems to have mitigated the problem somewhat: hex-overlap-after

Is this an advisable workaround? Or would it somehow be possible to avoid by rescaling the axes manually first, and then creating the hexbin?

lowderchris avatar Oct 21 '16 09:10 lowderchris

I'm encountering the same problem. I had to manually adjust linewidths.

conchoecia avatar May 27 '17 06:05 conchoecia

The only other workaround I know of is to turn off both anti-aliasing and edge-stroking. That gives less accurate shapes. This problem with rendering edges is one we seem to be stuck with; it crops up in all of the plot types that generate adjacent patches, and it involves transparency as well as anti-aliasing and edge-stroking.

efiring avatar May 27 '17 17:05 efiring