0

I'm working on a project at the moment plotting the route of MH370 based on satellite data and essentially it involves drawing patches which indicate the likely area at a given time. I have 3 sets of data per plot - one with a 100% confidence interval, a 95% one and then a 50% CI too. However, for each timestamp there are multiple likely points and they end up all running over one another and looking horrible. Is there a way to disable this overlay and make it such that the circles all form one contiguous area?

I've already checked and saw something similar here: Matplotlib: How to prevent transparent color overlay when curve overlaps? but it doesn't apply to patches and I can't think how to make it work in this instance. Unfortunately the resulting geometries are very irregular and can't be described by any shape.

Example:

fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(1, 1, 1)
ax.set_xlim([0,7])
ax.set_ylim([0,7])

ax.add_patch(mpatches.Circle(xy=[2.8, 2.8], radius= 1, color = 'red', alpha = 0.1))
ax.add_patch(mpatches.Circle(xy=[2.8, 2.8], radius= 1.5, color = 'green', alpha = 0.1))
plt.plot(2.8, 2.8, 'rx', markersize=9, markeredgewidth = 5)

ax.add_patch(mpatches.Circle(xy=[3.3, 3.5], radius= 1, color = 'red', alpha = 0.1))
ax.add_patch(mpatches.Circle(xy=[3.3, 3.5], radius= 1.5, color = 'green', alpha = 0.1))
plt.plot(3.3, 3.5, 'rx', markersize=9, markeredgewidth = 5)

ax.add_patch(mpatches.Circle(xy=[4.25, 4], radius= 1, color = 'red', alpha = 0.1))
ax.add_patch(mpatches.Circle(xy=[4.25, 4], radius= 1.5, color = 'green', alpha = 0.1))
plt.plot(4.25, 4, 'rx', markersize=9, markeredgewidth = 5)

plt.show()

Graph showing 3 overlapping circles highlighting the overlay problem discussed in the question

RedMoose
  • 23
  • 4
  • 2
    Why is `alpha = 1` not the answer to your question? – Mr. T Mar 10 '21 at 17:31
  • 2
    Maybe the [shapely](https://pypi.org/project/Shapely/) library can help to calculate the union of the overlapping shapes? – JohanC Mar 10 '21 at 17:41
  • @Mr.T apologies, I should have said in the question. These circles are overlaid onto a map; it's important to be able to see the background! – RedMoose Mar 10 '21 at 21:04
  • @JohanC thanks for the help, I'll check it out! – RedMoose Mar 10 '21 at 21:05
  • @JohanC that's the answer! Thank you so much, I'm so grateful! I'm going to answer my question with the code and the result in case anyone else comes across this page but I'll credit you! – RedMoose Mar 10 '21 at 21:43
  • Good to know you solved the problem. If you can post a stand-alone workable example as an answer, that would be nice. My participation is very tiny here. – JohanC Mar 10 '21 at 22:00

1 Answers1

0

Thanks to JohanC who recommended this package, that being "Shapely". I used a tutorial found on their site, namely this section: https://shapely.readthedocs.io/en/latest/manual.html#shapely.ops.unary_union

Here's the code and output:

from matplotlib import pyplot
from shapely.geometry import Point
from shapely.ops import unary_union
from descartes import PolygonPatch

poly1 = Point(2.8, 2.8).buffer(1)
poly2 = Point(3.3, 3.5).buffer(1)
poly3 = Point(4.25, 4).buffer(1)

poly4 = Point(2.8, 2.8).buffer(1.5)
poly5 = Point(3.3, 3.5).buffer(1.5)
poly6 = Point(4.25, 4).buffer(1.5)

polygons = [poly1, poly2, poly3]
polygons_outer = [poly4, poly5, poly6]

fig = pyplot.figure(1, figsize=(12, 5), dpi=90)
ax1 = fig.add_subplot(121)

u = unary_union(polygons)
u2 = unary_union(polygons_outer)
patch2b = PolygonPatch(u, fc='red', ec='red', alpha=0.2, zorder=2)
ax1.add_patch(patch2b)

patch2c = PolygonPatch(u2, fc='green', ec='green', alpha=0.1, zorder=1)
ax1.add_patch(patch2c)

ax1.set_xlim(0, 7)
ax1.set_ylim(0, 7)

pyplot.show()

Solved problem in original post

RedMoose
  • 23
  • 4