Skip to content
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

Heatmap polygon triangles don't meet exactly #44

Open
JoshuaMeyers opened this issue Apr 6, 2016 · 6 comments
Open

Heatmap polygon triangles don't meet exactly #44

JoshuaMeyers opened this issue Apr 6, 2016 · 6 comments

Comments

@JoshuaMeyers
Copy link

Hey Marc,

Lovely library. I've used it to make some great heatmaps from a data dictionary.
Just thought I'd file a bug report as the triangles don't quite match up if you zoom in - its really noticeable if you remove gridlines and have a low scale.

Cheers,
J.

@marcharper
Copy link
Owner

Hi @JoshuaMeyers,

Thanks for the report. Can you post a screenshot/image, and if possible, a small data set that reproduces the issue?

@JoshuaMeyers
Copy link
Author

Hey Marc,

Sorry that was a lazy report!
Here is some simplified code and an example image.
I have not included a dataset, any should recreate the issue.

If you zoom in on this image. You will see that the corners of the triangles do not meet each other.

Hope this is more helpful.

image

screen shot 2016-04-07 at 15 04 22

#PMI ternary density plot
#set up figure
figure, ax = plt.subplots()
tax = ternary.TernaryAxesSubplot(ax=ax, scale=scale)
plt.axis('off') #turn border off

figure.set_size_inches(15.3, 14)
tax.boundary(linewidth=1.5)

#make colormap with white zero value (or more accurately any value below vmin)
cmap_whiteZero = matplotlib.cm.get_cmap('Spectral_r')
cmap_whiteZero.set_under('w')
eps = np.spacing(0.0)

#plot ternary heatmap
tax.heatmap(log_dict, style="t", cmap=cmap_whiteZero, vmin=eps, colorbar=True) # style can be t, d, or h
# tax.gridlines(color="0.2", multiple=1, linewidth=0.8, linestyle='-') #uncomment for gridlines
figure.gca().invert_yaxis()

plt.savefig('/Users/jmeyers/Desktop/myfig.pdf')

@marcharper
Copy link
Owner

marcharper commented Apr 9, 2016

Thanks. Some of this is simply because of how matplotlib draws lines. I may be able to fix the corners. The other edge artifacts are because of the line width of the border -- the thickness of the border lines are covering up part of the corners of the interior colored trianges. This could be fixed by slightly moving the lines outward by the line-width. I'll have to think about how to do that for an arbitrary scale factor.

You might have better results in the meantime by setting the border to have smaller line-width.

As for the odd interior overlaps, I'll have to investigate cause -- I suspect it's something like rounding errors on the polygon coordinates.

@svrnwnsch
Copy link

This could be related to this matplotlib error: matplotlib/matplotlib#2935

@AlexanderZeilmann
Copy link

I was able to fix the overlap of the triangles in the interior by replacing https://github.com/marcharper/python-ternary/blob/master/ternary/heatmapping.py#L260

ax.fill(xs, ys, facecolor=color, edgecolor=color)

with

ax.fill(xs, ys, facecolor=color, edgecolor=color, linewidth=0.01)

I think the problem is, that the linewidth is too thick and this leads to a large overlap and misalignment of the triangles.
Setting the linewidth to 0.0 is however also not a good idea, because then a small white boundary is visible between the triangles.

At least for my small toy example setting the linewidth fixed the problem.

@fairliereese
Copy link

I was able to fix the overlap of the triangles in the interior by replacing https://github.com/marcharper/python-ternary/blob/master/ternary/heatmapping.py#L260

ax.fill(xs, ys, facecolor=color, edgecolor=color)

with

ax.fill(xs, ys, facecolor=color, edgecolor=color, linewidth=0.01)

I think the problem is, that the linewidth is too thick and this leads to a large overlap and misalignment of the triangles. Setting the linewidth to 0.0 is however also not a good idea, because then a small white boundary is visible between the triangles.

At least for my small toy example setting the linewidth fixed the problem.

Setting edgecolor=None without a linewidth argument does not result in any whitespace for me. Might be worth trying that. FWIW I'm using matplotlib ver. 3.2.2.

ax.fill(xs, ys, facecolor=color, edgecolor=None)

It's also worth noting that this also solves an issue that I encountered when trying to resize the PDF version of this plot in Adobe Illustrator. Illustrator does not resize lines by default but does resize shapes. So when you shrink the ternary heatmap, the stroke widths on the lines stay the same; exacerbating the non-overlapping shapes issue.

Zoomed-in shrunk in Illustrator example w/o any changes:
Screen Shot 2022-08-24 at 12 16 13 PM

Zoomed-in shrunk in Illustrator example w/ edgecolor=None:
Example w/ edgecolor=None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants