Introduction to the viridis color maps (2025)

Introduction to the viridis color maps (1)

Bob Rudis, NoamRoss and Simon Garnier

2024-01-28

Source: vignettes/intro-to-viridis.Rmd

intro-to-viridis.Rmd

tl;dr

Use the color scales in this package to make plots that are pretty,better represent your data, easier to read by those with colorblindness,and print well in gray scale.

Install viridis like any R package:

install.packages("viridis")library(viridis)

For base plots, use the viridis() function to generate apalette:

x <- y <- seq(-8*pi, 8*pi, len = 40)r <- sqrt(outer(x^2, y^2, "+"))filled.contour(cos(r^2)*exp(-r/(2*pi)),  axes=FALSE, color.palette=viridis, asp=1)

Introduction to the viridis color maps (2)

For ggplot, use scale_color_viridis() andscale_fill_viridis():

library(ggplot2)ggplot(data.frame(x = rnorm(10000), y = rnorm(10000)), aes(x = x, y = y)) + geom_hex() + coord_fixed() + scale_fill_viridis() + theme_bw()

Introduction to the viridis color maps (3)

Introduction

viridis,and its companion package viridisLiteprovide a series of color maps that are designed to improve graphreadability for readers with common forms of color blindness and/orcolor vision deficiency. The color maps are also perceptually-uniform,both in regular form and also when converted to black-and-white forprinting.

These color maps are designed to be:

  • Colorful, spanning as wide a palette as possible soas to make differences easy to see,
  • Perceptually uniform, meaning that values close toeach other have similar-appearing colors and values far away from eachother have more different-appearing colors, consistently across therange of values,
  • Robust to colorblindness, so that the aboveproperties hold true for people with common forms of colorblindness, aswell as in grey scale printing, and
  • Pretty, oh so pretty

viridisLite provides the base functions for generatingthe color maps in base R. The package is meant to be aslightweight and dependency-free as possible for maximum compatibilitywith all the R ecosystem. viridisprovides additional functionalities, in particular bindings forggplot2.

The Color Scales

The package contains eight color scales: “viridis”, the primarychoice, and five alternatives with similar properties - “magma”,“plasma”, “inferno”, “civids”, “mako”, and “rocket” -, and a rainbowcolor map - “turbo”.

The color maps viridis, magma,inferno, and plasma were created by Stéfan vander Walt (@stefanv) and Nathaniel Smith (@njsmith). If you want to know more aboutthe science behind the creation of these color maps, you can watch thispresentation ofviridis by their authors at SciPy 2015.

The color map cividis is a corrected version of‘viridis’, developed by Jamie R. Nuñez, Christopher R. Anderton, andRyan S. Renslow, and originally ported to R by MarcoSciaini (@msciain). More info aboutcividis can be found in thispaper.

The color maps mako and rocket wereoriginally created for the Seaborn statistical datavisualization package for Python. More info about mako androcket can be found on the Seabornwebsite.

The color map turbo was developed by Anton Mikhailov toaddress the shortcomings of the Jet rainbow color map such as falsedetail, banding and color blindness ambiguity. More infor aboutturbo can be found here.

Introduction to the viridis color maps (4)

Comparison

Let’s compare the viridis and magma scales against these othercommonly used sequential color palettes in R:

  • Base R palettes: rainbow.colors,heat.colors, cm.colors
  • The default ggplot2 palette
  • Sequential colorbrewerpalettes, both default blues and the more viridis-likeyellow-green-blue

Introduction to the viridis color maps (5)

It is immediately clear that the “rainbow” palette is notperceptually uniform; there are several “kinks” where the apparent colorchanges quickly over a short range of values. This is also true, thoughless so, for the “heat” colors. The other scales are more perceptuallyuniform, but “viridis” stands out for its large perceptualrange. It makes as much use of the available color space aspossible while maintaining uniformity.

Now, let’s compare these as they might appear under various forms ofcolorblindness, which can be simulated using the dichromatpackage:

Green-Blind (Deuteranopia)

Introduction to the viridis color maps (6)

Red-Blind (Protanopia)

Introduction to the viridis color maps (7)

Blue-Blind (Tritanopia)

Introduction to the viridis color maps (8)

Desaturated

Introduction to the viridis color maps (9)

We can see that in these cases, “rainbow” is quite problematic - itis not perceptually consistent across its range. “Heat” washes out atbright colors, as do the brewer scales to a lesser extent. The ggplotscale does not wash out, but it has a low perceptual range - there’s notmuch contrast between low and high values. The “viridis” and “magma”scales do better - they cover a wide perceptual range in brightness inbrightness and blue-yellow, and do not rely as much on red-greencontrast. They do less well under tritanopia (blue-blindness), but thisis an extrememly rare form of colorblindness.

Usage

The viridis() function produces the viridiscolor scale. You can choose the other color scale options using theoption parameter or the convenience functionsmagma(), plasma(), inferno(),cividis(), mako(),rocket(), andturbo()`.

Here the inferno() scale is used for a raster of U.S.max temperature:

library(terra)library(httr)par(mfrow=c(1,1), mar=rep(0.5, 4))temp_raster <- "http://ftp.cpc.ncep.noaa.gov/GIS/GRADS_GIS/GeoTIFF/TEMP/us_tmax/us.tmax_nohads_ll_20150219_float.tif"try(GET(temp_raster, write_disk("us.tmax_nohads_ll_20150219_float.tif")), silent=TRUE)us <- rast("us.tmax_nohads_ll_20150219_float.tif")us <- project(us, y="+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs")image(us, col=inferno(256), asp=1, axes=FALSE, xaxs="i", xaxt='n', yaxt='n', ann=FALSE)

Introduction to the viridis color maps (10)

The package also contains color scale functions forggplot plots: scale_color_viridis() andscale_fill_viridis(). As with viridis(), youcan use the other scales with the option argument in theggplot scales.
Here the “magma” scale is used for a cloropleth map of U.S.unemployment:

library(maps)
## ## Attaching package: 'maps'
## The following object is masked from 'package:viridis':## ## unemp
library(mapproj)data(unemp, package = "viridis")county_df <- map_data("county", projection = "albers", parameters = c(39, 45))names(county_df) <- c("long", "lat", "group", "order", "state_name", "county")county_df$state <- state.abb[match(county_df$state_name, tolower(state.name))]county_df$state_name <- NULLstate_df <- map_data("state", projection = "albers", parameters = c(39, 45))choropleth <- merge(county_df, unemp, by = c("state", "county"))choropleth <- choropleth[order(choropleth$order), ]ggplot(choropleth, aes(long, lat, group = group)) + geom_polygon(aes(fill = rate), colour = alpha("white", 1 / 2), linewidth = 0.2) + geom_polygon(data = state_df, colour = "white", fill = NA) + coord_fixed() + theme_minimal() + ggtitle("US unemployment rate by county") + theme(axis.line = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), axis.title = element_blank()) + scale_fill_viridis(option="magma")

Introduction to the viridis color maps (11)

The ggplot functions also can be used for discrete scales with theargument discrete=TRUE.

p <- ggplot(mtcars, aes(wt, mpg))p + geom_point(size=4, aes(colour = factor(cyl))) + scale_color_viridis(discrete=TRUE) + theme_bw()

Introduction to the viridis color maps (12)

Gallery

Here are some examples of viridis being used in the wild:

James Curley uses viridis for matrix plots (Code):

Introduction to the viridis color maps (13)

Christopher Moore created these contour plots of potential in adynamic plankton-consumer model:

Introduction to the viridis color maps (14)

Introduction to the viridis color maps (2025)

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 5652

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.