python - calculate RGB equivalent of base colors with alpha of 0.5 over white background in matplotlib -
i able replicate of primary color ('r','g' or 'b') in matplotlib alpha of 0.5 on white background, while keeping alpha @ 1.
here example below, through manual experimentation i've found rgb values alpha of 1, similar matplotlib default colors alpha 0.5.
i wondering if had automated way of achieving this.
import matplotlib.pyplot plt s=1000 plt.xlim([4,8]) plt.ylim([0,10]) red=(1,0.55,0.55) blue=(0.55,0.55,1) green=(0.54,0.77,0.56) plt.scatter([5],[5],c='r',edgecolors='none',s=s,alpha=0.5,marker='s') plt.scatter([6],[5],c='b',edgecolors='none',s=s,alpha=0.5,marker='s') plt.scatter([7],[5],c='g',edgecolors='none',s=s,alpha=0.5,marker='s') plt.scatter([5],[5.915],c=red,edgecolors='none',s=s,marker='s') plt.scatter([6],[5.915],c=blue,edgecolors='none',s=s,marker='s') plt.scatter([7],[5.915],c=green,edgecolors='none',s=s,marker='s')
edit: can use formula this answer
converted python, looks this:
def make_rgb_transparent(rgb, bg_rgb, alpha): return [alpha * c1 + (1 - alpha) * c2 (c1, c2) in zip(rgb, bg_rgb)]
so can do:
red = [1, 0, 0] white = [1, 1, 1] alpha = 0.5 make_rgb_transparent(red, white, alpha) # [1.0, 0.5, 0.5]
using function now, can create plot confirms works:
from matplotlib import colors import matplotlib.pyplot plt alpha = 0.5 kwargs = dict(edgecolors='none', s=3900, marker='s') i, color in enumerate(['red', 'blue', 'green']): rgb = colors.colorconverter.to_rgb(color) rgb_new = make_rgb_transparent(rgb, (1, 1, 1), alpha) print(color, rgb, rgb_new) plt.scatter([i], [0], color=color, **kwargs) plt.scatter([i], [1], color=color, alpha=alpha, **kwargs) plt.scatter([i], [2], color=rgb_new, **kwargs)
Comments
Post a Comment