I have a list of rectangles (coordinates as (x0, y0, x1, y1)) that overlap in certain areas. I want to draw them with their overlaps, with an extra twist: the colors of each overlap area should be like a heatmap: they should get darker (or lighter, or redder - that doesn't matter) the more rectangles are overlapping in that area.
This can be done easily with Pandas, but it takes O(N_pixels) which is way too much. There should be a way of doing this whose cost depends on the number of rectangles instead, thus speeding me up thousands of times.
Example (in Pandas):
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
coords = [(1, 1, 4, 4), (2, 3, 6, 6), (2, 1, 3,2), (5, 3, 6, 7), (4, 3, 6, 7), (8, -5, 10, -2), (6, 0, 8, 3)]
heatmap = pd.DataFrame()
for box in coords:
area = pd.DataFrame(1, index=range(box[0], box[2]), columns=range(box[1], box[3]))
heatmap = heatmap.add(area, fill_value=0)
heatmap = heatmap.fillna(0).astype(int)
with sns.axes_style('white'):
plt.figure(figsize=(10,10))
ax = sns.heatmap(heatmap, cmap=plt.cm.jet, xticklabels=100, yticklabels=100)
ax.set(title="Heatmap of overlapping rectangles")
plt.show()
Displays this: