matplotlib 的平滑、插值的三级或四级色标(例如 r、g、b 三角形)?
Smooth, interpolated tertiary or quaternary colour scales (e.g. r, g, b triangle) for matplotlib?
假设我想为 matplotlib 图中的点创建一个配色方案,每个点都有一定比例的属性 A
、B
和 C
(每个都可以取值从 0 到 1)。一个简单的方法是使用 rgb triangle,这样点的红色值由 A
给出,它的绿色值由 B
给出,它的蓝色值由 [= 给出12=].
但是,看起来像 this 这样的替代三角形可能会提供更多 'dynamic range',即更容易看到每个点在三角形中的位置。但是,我不知道如何编写特定三角形的代码(即给定 A
、B
和 C
、return 一个 (r,g,b)
元组)。它还有一个额外的问题,就是中间有白色,在白色背景下看起来不太好。
我的问题:对于这样的问题,什么是好的三角形色标?还有这种东西叫什么,因为我发现很难搜索?
此外,如果我们想将其扩展到四个属性,A
、B
、C
和 D
,并且有一个 "colour square," 什么四个角的颜色是合适的,你将如何编写一个函数从 A
、B
、C
和 D
和 return a (r,g,b)
元组?
看起来像这样的配色方案很理想 (a) 到处都有独特的颜色,(b) 没有白色,并且 (c) 理想情况下有很大的变化,以便更容易看到色标你是通过看它。更好的是色盲安全的东西,但在这种情况下,这可能要求太高了。
非常感谢您的建议。我的最终目标是制作类似 this 的东西,但使用其中一种配色方案。谢谢。
编辑:添加重要约束 A+B+C=1
使这在技术上成为可能。举个例子,假设我想提出一个配色方案,以便将独特的颜色映射到 ternary phase diagram such as this 中的特定构图。 'obvious'的情况是使用RGB三角形,但是有没有更好的解决办法呢?我将如何在 Python 中编码?
试一试这样的东西,看看如何表示您自己的数据:
import matplotlib.pyplot as plt
from numpy import random
count = 300
space = random.random((count,2))
hue = random.uniform(0,1,(count,3))
def to_rgb(triplet):
'''convert a triplet of values in the range [0,1]
to a RGB triplet of values in the range [0, 255] (integers);
blue is kept to a smaller range so the RGB never turns white.'''
red = triplet[0]
green = 1-triplet[1]
blue = triplet[2]*0.75
return [red, green, blue]
plt.scatter(space[:,0], space[:,1], c=map(to_rgb, hue), s=60)
plt.show()
您可以使用 ColorBrewer 讨论我们如何解释单色或发散或任何配色方案来确定您希望如何表示三个 "color" 变量中的每一个。 (很难组合表示其中的三个。更难的是用一种即使是最常见的色盲也能起作用的方式。)
好的,我试过了。它并不完美,而且有点乱七八糟,因此 improvements/suggested 非常受欢迎。我很乐意接受更好的答案(特别是对 abc_to_rgb
的改进,以消除错误、改善动态范围,或者只是为色盲提供更好的配色方案)。我正在考虑可能使用 YIQ colour space with Y=0.5 作为等效的四元配色方案(方形图例)。此外,这是我第一次使用重心坐标,所以我绘制的图例可能并不理想(例如,更好的图例可能不会 'bleed' 超出边缘,或者需要这么多点).
import matplotlib.pyplot as plt
import numpy as np
import math
def abc_to_rgb(A=0.0,B=0.0,C=0.0):
''' Map values A, B, C (all in domain [0,1]) to
suitable red, green, blue values.'''
return (min(B+C,1.0),min(A+C,1.0),min(A+B,1.0))
def plot_legend():
''' Plots a legend for the colour scheme
given by abc_to_rgb. Includes some code adapted
from
# Basis vectors for triangle
basis = np.array([[0.0, 1.0], [-1.5/np.sqrt(3), -0.5],[1.5/np.sqrt(3), -0.5]])
fig = plt.figure()
ax = fig.add_subplot(111,aspect='equal')
# Plot points
a, b, c = np.mgrid[0.0:1.0:50j, 0.0:1.0:50j, 0.0:1.0:50j]
a, b, c = a.flatten(), b.flatten(), c.flatten()
abc = np.dstack((a,b,c))[0]
#abc = filter(lambda x: x[0]+x[1]+x[2]==1, abc) # remove points outside triangle
abc = map(lambda x: x/sum(x), abc) # or just make sure points lie inside triangle ...
data = np.dot(abc, basis)
colours = [abc_to_rgb(A=point[0],B=point[1],C=point[2]) for point in abc]
ax.scatter(data[:,0], data[:,1],marker=',',edgecolors='none',facecolors=colours)
# Plot triangle
ax.plot([basis[_,0] for _ in range(3) + [0,]],[basis[_,1] for _ in range(3) + [0,]],**{'color':'black','linewidth':3})
# Plot labels at vertices
offset = 0.25
fontsize = 32
ax.text(basis[0,0]*(1+offset), basis[0,1]*(1+offset), '$A$', horizontalalignment='center',
verticalalignment='center', fontsize=fontsize)
ax.text(basis[1,0]*(1+offset), basis[1,1]*(1+offset), '$B$', horizontalalignment='center',
verticalalignment='center', fontsize=fontsize)
ax.text(basis[2,0]*(1+offset), basis[2,1]*(1+offset), '$C$', horizontalalignment='center',
verticalalignment='center', fontsize=fontsize)
ax.set_frame_on(False)
ax.set_xticks(())
ax.set_yticks(())
plt.show()
我们的图例如下:
然后 abc_to_rgb
可用于为散点图或折线图中的给定点获取合适的颜色...
假设我想为 matplotlib 图中的点创建一个配色方案,每个点都有一定比例的属性 A
、B
和 C
(每个都可以取值从 0 到 1)。一个简单的方法是使用 rgb triangle,这样点的红色值由 A
给出,它的绿色值由 B
给出,它的蓝色值由 [= 给出12=].
但是,看起来像 this 这样的替代三角形可能会提供更多 'dynamic range',即更容易看到每个点在三角形中的位置。但是,我不知道如何编写特定三角形的代码(即给定 A
、B
和 C
、return 一个 (r,g,b)
元组)。它还有一个额外的问题,就是中间有白色,在白色背景下看起来不太好。
我的问题:对于这样的问题,什么是好的三角形色标?还有这种东西叫什么,因为我发现很难搜索?
此外,如果我们想将其扩展到四个属性,A
、B
、C
和 D
,并且有一个 "colour square," 什么四个角的颜色是合适的,你将如何编写一个函数从 A
、B
、C
和 D
和 return a (r,g,b)
元组?
看起来像这样的配色方案很理想 (a) 到处都有独特的颜色,(b) 没有白色,并且 (c) 理想情况下有很大的变化,以便更容易看到色标你是通过看它。更好的是色盲安全的东西,但在这种情况下,这可能要求太高了。
非常感谢您的建议。我的最终目标是制作类似 this 的东西,但使用其中一种配色方案。谢谢。
编辑:添加重要约束 A+B+C=1
使这在技术上成为可能。举个例子,假设我想提出一个配色方案,以便将独特的颜色映射到 ternary phase diagram such as this 中的特定构图。 'obvious'的情况是使用RGB三角形,但是有没有更好的解决办法呢?我将如何在 Python 中编码?
试一试这样的东西,看看如何表示您自己的数据:
import matplotlib.pyplot as plt
from numpy import random
count = 300
space = random.random((count,2))
hue = random.uniform(0,1,(count,3))
def to_rgb(triplet):
'''convert a triplet of values in the range [0,1]
to a RGB triplet of values in the range [0, 255] (integers);
blue is kept to a smaller range so the RGB never turns white.'''
red = triplet[0]
green = 1-triplet[1]
blue = triplet[2]*0.75
return [red, green, blue]
plt.scatter(space[:,0], space[:,1], c=map(to_rgb, hue), s=60)
plt.show()
您可以使用 ColorBrewer 讨论我们如何解释单色或发散或任何配色方案来确定您希望如何表示三个 "color" 变量中的每一个。 (很难组合表示其中的三个。更难的是用一种即使是最常见的色盲也能起作用的方式。)
好的,我试过了。它并不完美,而且有点乱七八糟,因此 improvements/suggested 非常受欢迎。我很乐意接受更好的答案(特别是对 abc_to_rgb
的改进,以消除错误、改善动态范围,或者只是为色盲提供更好的配色方案)。我正在考虑可能使用 YIQ colour space with Y=0.5 作为等效的四元配色方案(方形图例)。此外,这是我第一次使用重心坐标,所以我绘制的图例可能并不理想(例如,更好的图例可能不会 'bleed' 超出边缘,或者需要这么多点).
import matplotlib.pyplot as plt
import numpy as np
import math
def abc_to_rgb(A=0.0,B=0.0,C=0.0):
''' Map values A, B, C (all in domain [0,1]) to
suitable red, green, blue values.'''
return (min(B+C,1.0),min(A+C,1.0),min(A+B,1.0))
def plot_legend():
''' Plots a legend for the colour scheme
given by abc_to_rgb. Includes some code adapted
from
# Basis vectors for triangle
basis = np.array([[0.0, 1.0], [-1.5/np.sqrt(3), -0.5],[1.5/np.sqrt(3), -0.5]])
fig = plt.figure()
ax = fig.add_subplot(111,aspect='equal')
# Plot points
a, b, c = np.mgrid[0.0:1.0:50j, 0.0:1.0:50j, 0.0:1.0:50j]
a, b, c = a.flatten(), b.flatten(), c.flatten()
abc = np.dstack((a,b,c))[0]
#abc = filter(lambda x: x[0]+x[1]+x[2]==1, abc) # remove points outside triangle
abc = map(lambda x: x/sum(x), abc) # or just make sure points lie inside triangle ...
data = np.dot(abc, basis)
colours = [abc_to_rgb(A=point[0],B=point[1],C=point[2]) for point in abc]
ax.scatter(data[:,0], data[:,1],marker=',',edgecolors='none',facecolors=colours)
# Plot triangle
ax.plot([basis[_,0] for _ in range(3) + [0,]],[basis[_,1] for _ in range(3) + [0,]],**{'color':'black','linewidth':3})
# Plot labels at vertices
offset = 0.25
fontsize = 32
ax.text(basis[0,0]*(1+offset), basis[0,1]*(1+offset), '$A$', horizontalalignment='center',
verticalalignment='center', fontsize=fontsize)
ax.text(basis[1,0]*(1+offset), basis[1,1]*(1+offset), '$B$', horizontalalignment='center',
verticalalignment='center', fontsize=fontsize)
ax.text(basis[2,0]*(1+offset), basis[2,1]*(1+offset), '$C$', horizontalalignment='center',
verticalalignment='center', fontsize=fontsize)
ax.set_frame_on(False)
ax.set_xticks(())
ax.set_yticks(())
plt.show()
我们的图例如下:
然后 abc_to_rgb
可用于为散点图或折线图中的给定点获取合适的颜色...