Plotting order matters for overlapping colors

When plotting potentially overlapping data points of two different kinds, if both data points have their alpha set to 0.5, I would hope that the resulting color of their overlap is the same regardless of which point was plotted first. This demonstrates that is not the case — instead the color deviates towards the most recently plotted one. This could be an artifact of color theory or something, I’m not sure, but I was wondering what people would have to say about this and possible work-arounds if a direct solution is not possible.

plotting_colors

import matplotlib
import numpy as np
from matplotlib.patches import Circle
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
fig, ax = plt.subplots()

patches = []
patches.append(Circle((0.2,0.8),0.1))
patches.append(Circle((0.3,0.8),0.1))
patches.append(Circle((0.6,0.2),0.1))
patches.append(Circle((0.7,0.2),0.1))
colors = [0,1,1,0]
p = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.5)
p.set_array(np.array(colors))
ax.add_collection(p)
ax.set_title('Plotting order matters for overlapping colors')
plt.colorbar(p)
plt.show()

Author: Fantashit

1 thought on “Plotting order matters for overlapping colors

  1. @WingTillDie That is probably because we are blending in to an int8 buffer (rather than a float buffer) which introduces truncation at each composite step (this has way worse issues when overlaying many very transparent objects). The fix is to change the internal agg buffers to use float types rather than ints and cast to int at the very end.

Comments are closed.