在matplotlib中的白色背景上计算alpha值为0.5的基色的RGB等效值 [英] calculate RGB equivalent of base colors with alpha of 0.5 over white background in matplotlib
问题描述
我希望能够在白色背景上以0.5的alpha值在matplotlib中复制原色( r, g或 b)的外观,同时将alpha值保持为1。 / p>
下面是一个示例,通过手动实验,我发现了alpha值为1的RGB值,看起来类似于带有0.5的matplotlib默认颜色。 / p>
我想知道是否有人能够自动实现这一目标。
将matplotlib.pyplot导入为plt
s = 1000
plt.xlim([4,8])
plt.ylim([0,10])
红色=(1,0.55,0.55)
蓝色=(0.55,0.55,1)
绿色=(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 = blu e,edgecolors ='none',s = s,marker ='s')
plt.scatter([7],[5.915],c = green,edgecolors ='none',s = s,marker = 's')
编辑:您可以使用
I would like to be able to replicate the look of a primary color ('r','g' or 'b') in matplotlib with an alpha of 0.5 over a white background, while keeping the alpha at 1.
Here is an example below, where through manual experimentation I've found the RGB values that with an alpha of 1, look similar to matplotlib default colors with an alpha 0.5.
I was wondering if someone had an automated way of achieving this.
import matplotlib.pyplot as 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: you can use the formula from this answer
Converted to Python, it looks like this:
def make_rgb_transparent(rgb, bg_rgb, alpha):
return [alpha * c1 + (1 - alpha) * c2
for (c1, c2) in zip(rgb, bg_rgb)]
So you 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 this function now, we can create a plot that confirms this works:
from matplotlib import colors
import matplotlib.pyplot as plt
alpha = 0.5
kwargs = dict(edgecolors='none', s=3900, marker='s')
for 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)
这篇关于在matplotlib中的白色背景上计算alpha值为0.5的基色的RGB等效值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!