Tal*_*ili 23 python r colors matplotlib viridis
我有兴趣拥有一个"好"的发散颜色调色板.显然可以使用红色,白色和蓝色:
img <- function(obj, nam) {
image(1:length(obj), 1, as.matrix(1:length(obj)), col=obj,
main = nam, ylab = "", xaxt = "n", yaxt = "n", bty = "n")
}
rwb <- colorRampPalette(colors = c("red", "white", "blue"))
img(rwb(100), "red-white-blue")
Run Code Online (Sandbox Code Playgroud)
自从我最近爱上了绿色调色板以来,我希望将绿色和岩浆结合起来形成这种不同的颜色(当然,色盲的人只能看到颜色的绝对值,但有时候还可以).
当我尝试将绿色和岩浆结合起来时,我发现它们并没有在同一个地方"结束"(或"开始"),所以我得到这样的东西(我正在使用R,但这可能是相同的python用户):
library(viridis)
img(c(rev(viridis(100, begin = 0)), magma(100, begin = 0)), "magma-viridis")
Run Code Online (Sandbox Code Playgroud)
我们可以看到,当接近零时,绿色是紫色的,而岩浆是黑色的.我希望他们两个(或多或少)开始在同一个地方,所以我尝试使用0.3作为起点:
img(c(rev(viridis(100, begin = 0.3)), magma(100, begin = 0.3)), "-viridis-magma(0.3)")
Run Code Online (Sandbox Code Playgroud)
这确实更好,但我想知道是否有更好的解决方案.
(我也在"标记"python用户,因为viridis最初来自matplotlib
,所以使用它的人可能知道这样的解决方案)
谢谢!
Ach*_*eis 17
已经有一些好的和有用的建议,但我要补充一些评论:
因此,结合岩浆和绿蝇不起作用.你可以让它们偏离类似的黄色,但你会偏离类似的蓝色.此外,随着色调的变化,判断调色板的哪一个手臂变得更加困难.
正如其他人所提到的,ColorBrewer.org提供了很好的分散调色板.莫兰德的方法也很有用.另一个通用的解决方案是我们diverging_hcl()
在colorspace
包装中的功能.CSDA论文(http://dx.doi.org/10.1016/j.csda.2008.11.033)对此进行了描述,并且在BAMS论文中提供了针对气象的更多建议,但适用范围更广,http:// dx.doi.org/10.1175/BAMS-D-13-00155.1).
我们在HCL空间(色调 - 色度 - 亮度)中的解决方案的优点是您可以相对容易地解释坐标.它需要一些练习但不像其他解决方案那样不透明.我们还提供了一个GUI hclwizard()
(见下文),有助于理解不同坐标的重要性.
如果适当地diverging_hcl()
选择两个色调(参数h
),最大色度(c
)和最小/最大亮度(l
),则问题中的大多数调色板和其他答案可以相当紧密地匹配.此外,人们可能不得不调整power
分别控制色度和亮度增加速度的论据.通常,色度增加得相当快(power[1] < 1
),而亮度增加得更慢(power[2] > 1
).
例如,Moreland的"酷温"调色板使用蓝色(h = 250
)和红色(h = 10
)色调,但亮度对比度相对较小(l = 37
相对l = 88
):
coolwarm_hcl <- colorspace::diverging_hcl(11,
h = c(250, 10), c = 100, l = c(37, 88), power = c(0.7, 1.7))
Run Code Online (Sandbox Code Playgroud)
看起来很相似(见下文):
coolwarm <- Rgnuplot:::GpdivergingColormap(seq(0, 1, length.out = 11),
rgb1 = colorspace::sRGB( 0.230, 0.299, 0.754),
rgb2 = colorspace::sRGB( 0.706, 0.016, 0.150),
outColorspace = "sRGB")
coolwarm[coolwarm > 1] <- 1
coolwarm <- rgb(coolwarm[, 1], coolwarm[, 2], coolwarm[, 3])
Run Code Online (Sandbox Code Playgroud)
相比之下,ColorBrewer.org的BrBG调色板具有更高的亮度对比度(l = 20
vs. l = 95
):
brbg <- rev(RColorBrewer::brewer.pal(11, "BrBG"))
brbg_hcl <- colorspace::diverging_hcl(11,
h = c(180, 50), c = 80, l = c(20, 95), power = c(0.7, 1.3))
Run Code Online (Sandbox Code Playgroud)
下面将得到的调色板与原始调色板下面的基于HCL的版本进行比较.你看,这些并不完全相同,而是非常接近.在右侧,我还将绿色和血浆与基于HCL的调色板相匹配.
无论您喜欢酷温还是BrBG调色板,都可能取决于您的个人品味,但更重要的是,您想要在可视化中带来什么.如果偏差的符号最重要,则冷暖的低亮度对比将更有用.如果要显示(极端)偏差的大小,高亮度对比度将更有用.更为实用的指导在BAMS论文中,而计算在CSDA论文中有更详细的解释.
上图的其余复制代码是:
viridis <- viridis::viridis(11)
viridis_hcl <- colorspace::sequential_hcl(11,
h = c(300, 75), c = c(35, 95), l = c(15, 90), power = c(0.8, 1.2))
plasma <- viridis::plasma(11)
plasma_hcl <- colorspace::sequential_hcl(11,
h = c(-100, 100), c = c(60, 100), l = c(15, 95), power = c(2, 0.9))
pal <- function(col, border = "transparent") {
n <- length(col)
plot(0, 0, type="n", xlim = c(0, 1), ylim = c(0, 1),
axes = FALSE, xlab = "", ylab = "")
rect(0:(n-1)/n, 0, 1:n/n, 1, col = col, border = border)
}
par(mar = rep(0, 4), mfrow = c(4, 2))
pal(coolwarm)
pal(viridis)
pal(coolwarm_hcl)
pal(viridis_hcl)
pal(brbg)
pal(plasma)
pal(brbg_hcl)
pal(plasma_hcl)
Run Code Online (Sandbox Code Playgroud)
您可以在闪亮的应用程序中以交互方式探索我们提出的颜色:http://hclwizard.org: 64230/hclwizard /.对于R的用户,您还可以在计算机上本地启动闪亮的应用程序(运行速度比从我们的服务器运行得快一些),或者您可以运行它的Tcl/Tk版本(甚至更快):
colorspace::sequential_hcl(11, "viridis")
grDevices::hcl.colors(11, "viridis")
Run Code Online (Sandbox Code Playgroud)
如果您想了解调色板的路径在RGB和HCL坐标中的样子,那么colorspace
这很有用.例如,参见hcl.colors()
.
jan*_*glx 11
我发现Kenneth Moreland的建议非常有用.它是在Rgnuplot
包中实现的(install.packages("Rgnuplot")
足够了,你不需要安装GNU plot).要像通常的颜色映射一样使用它,你需要像这样转换它:
cool_warm <- function(n) {
colormap <- Rgnuplot:::GpdivergingColormap(seq(0,1,length.out=n),
rgb1 = colorspace::sRGB( 0.230, 0.299, 0.754),
rgb2 = colorspace::sRGB( 0.706, 0.016, 0.150),
outColorspace = "sRGB")
colormap[colormap>1] <- 1 # sometimes values are slightly larger than 1
colormap <- grDevices::rgb(colormap[,1], colormap[,2], colormap[,3])
colormap
}
img(red_blue_diverging_colormap(500), "Cool-warm, (Moreland 2009)")
Run Code Online (Sandbox Code Playgroud)
这就是它与插值的RColorBrewer"RdBu"相比在行动中的样子:
库RColorBrewer
提供= <13种颜色的漂亮调色板.例如,调色板BrBG
显示从棕色到绿色的不同颜色.
library(RColorBrewer)
display.brewer.pal(11, "BrBG")
Run Code Online (Sandbox Code Playgroud)
通过创建中点颜色和从中点颜色创建调色板,可以扩展到信息量较少的调色板.
brbg <- brewer.pal(11, "BrBG")
cols <- c(colorRampPalette(c(brbg[1], brbg[6]))(51),
colorRampPalette(c(brbg[6], brbg[11]))(51)[-1])
Run Code Online (Sandbox Code Playgroud)
类似地,使用您选择的viridis
和magma
调色板,您可以尝试找到它们之间的相似性.这可能是一个点,在哪里连接调色板背靠背.
select.col <- function(cols1, cols2){
x <- col2rgb(cols1)
y <- col2rgb(cols2)
sim <- which.min(colSums(abs(x[,ncol(x)] - y)))
message(paste("Your palette will be", sim, "colors shorter."))
cols.x <- apply(x, 2, function(temp) rgb(t(temp)/255))
cols.y <- apply(y[,sim:ncol(y)], 2, function(temp) rgb(t(temp)/255))
return(c(cols.x,cols.y))
}
img(select.col(rev(viridis(100,0)),magma(100,0)), "")
# Your palette will be 16 colors shorter.
Run Code Online (Sandbox Code Playgroud)
该scico
软件包(基于科学色彩图的R调色板)有几个良好的分散调色板,感知统一和色盲安全.
也可用于Python,MATLAB,GMT,QGIS,Plotly,的Paraview,参观,数学,冲浪,D3等在这里
论文:Crameri,F.(2018),地球动力学诊断,科学可视化和StagLab 3.0,Geosci.Model Dev.,11,2541-2562,doi:10.5194/gmd-11-2541-2018
编辑:vik
现在是CRAN.运行roma
安装
# install.packages("devtools")
# devtools::install_github("thomasp85/scico")
library(scico)
scico_palette_show(palettes = c("broc", "cork", "vik",
"lisbon", "tofino", "berlin",
"batlow", "roma"))
Run Code Online (Sandbox Code Playgroud)
另一个很棒的包是cmocean(Python).它的色彩图可通过berlin
包装或oce包装在R中获得.
论文:Thyng,KM,Greene,CA,Hetland,RD,Zimmerle,HM,&DiMarco,SF(2016).海洋学的真实色彩.海洋学,29(3),10,http://dx.doi.org/10.5670/oceanog.2016.66.
Talk:PLOTCON 2016:Kristen Thyng,为您的领域定制Colormaps.
### install.packages("devtools")
### devtools::install_github("kwstat/pals")
library(pals)
pal.bands(ocean.balance, ocean.delta, ocean.curl, main = "cmocean")
Run Code Online (Sandbox Code Playgroud)
由reprex包创建于2018-10-15 (v0.2.1.9000)
归档时间: |
|
查看次数: |
7216 次 |
最近记录: |