logo

How to Use Color Gradients in Julia Plots 📂Julia

How to Use Color Gradients in Julia Plots

Description

A color gradient is one of the two color schemes supported by Julia’s visualization package Plots.jl (the other is palette), which is what we commonly refer to as gradation. Simply put, a type that implements gradation is ColorGradient.

Gradients are used to draw charts such as heatmap(), surface(), contour(). If you want to differentiate the colors of various graphs, use a palette instead of a gradient.

Code

Symbol

It can be used with cgrad(symbol). The default gradient is cgrad(:inferno), and the colors are as follows.

using Plots

cgrad(:inferno)

heatmap(reshape(1:25, (5, 5)))

Pre-defined palettes and gradients in Plots.jl can be found in the official documentation (More diverse palettes and gradients can be found in the official documentation of the package ColorSchemes.jl here).

In Python’s matplotlib, the default colormap for imshow that resembles a gradient is :viridis.

heatmap(reshape(1:25, (5, 5)), fillcolor = cgrad(:viridis))

Custom Definition

A palette can be defined directly with cgrad([start color, end color]). To set the points of color transformation, input a vector containing values between $0$ and $1$ as an optional argument.

cgrad([:blue, :orange])

cgrad([:blue, :orange], [0.1, 0.9])

cgrad([:blue, :orange], [0.5, 0.50001])

Keywords

rev

Entering the keyword argument rev = true will reverse the order.

cgrad(:darktest)

cgrad(:darktest, rev = true)

scale

The keyword scale specifies the scale of the gradient. You can input :log or :exp.

cgrad(:rainbow)

cgrad(:rainbow, scale = :log)

cgrad(:rainbow, scale = :exp)

Environment

  • OS: Windows11
  • Version: Julia 1.9.4, Plots v1.39.0

See Also