DEV Community

Cover image for Julia Plotting Cheat Sheet
AI Co-Founded
AI Co-Founded

Posted on

Julia Plotting Cheat Sheet

This cheat sheet provides an overview of the most commonly used plotting functions and attributes in Julia using the popular plotting library Plots.jl. To get started, make sure you have the Plots package installed by running:

using Pkg
Pkg.add("Plots")
Enter fullscreen mode Exit fullscreen mode

Then load the Plots library by running:

using Plots
Enter fullscreen mode Exit fullscreen mode

Basic Plotting Functions

  1. Line plot:
function linePlot()
    x = 1:0.1:10
    y = cos.(x)
    plot(x, y, label="cos(x)")
end
Enter fullscreen mode Exit fullscreen mode

Line plot in Julia

  1. Scatter plot:
function scatterPlot()
    x = 1:0.1:10
    y = cos.(x)
    scatter(x, y, label="cos(x)")
end
Enter fullscreen mode Exit fullscreen mode

Scatter plot in Julia

  1. Bar plot:
function barPlot()
    x = 1:0.1:10
    y = cos.(x)
    bar(x, y, label="cos(x)")
end
Enter fullscreen mode Exit fullscreen mode

Bar plot in Julia

  1. Histogram:
function histogramPlot()
    x = 1:0.1:10
    y = cos.(x)
    histogram(x, y, label="cos(x)")
end
Enter fullscreen mode Exit fullscreen mode

Histogram in Julia

  1. Box plot:
using StatsPlots
function boxPlot()
    x = 1:0.1:10
    y = cos.(x)
    boxplot(x, y, label="cos(x)")
end
Enter fullscreen mode Exit fullscreen mode

Boxplot in Julia

  1. Heatmap:
function heatmapPlot()
    data = rand(21, 100)
    heatmap(1:size(data, 1),
        1:size(data, 2), data,
        c=cgrad([:blue, :white, :red, :yellow]),
    )
end
Enter fullscreen mode Exit fullscreen mode

Heatmap in Julia

  1. 3D plot:
function plot3D()
    x = 1:0.1:10
    y = cos.(x)
    z = sin.(x)
    plot3d(x, y, z, label="cos(x)")
end
Enter fullscreen mode Exit fullscreen mode

3D Plot in Julia

Common Plot Attributes

Title, labels, and legend

  • title: Plot title
  • xlabel: x-axis label
  • ylabel: y-axis label
  • zlabel: z-axis label (for 3D plots)
  • legend: Legend position
    • :topleft
    • :topright
    • :bottomleft
    • :bottomright
    • :outertopleft
    • :outertopright
    • :outerbottomleft
    • :outerbottomright
    • :best
    • :none

Example:

plot(x, y, title="My Line Plot", xlabel="x-axis", ylabel="y-axis", legend=:topleft)
Enter fullscreen mode Exit fullscreen mode

Line style

  • ls: Line style
    • :solid
    • :dash
    • :dot
    • :dashdot
    • :dashdotdot
  • lw: Line width

Example:

plot(x, y, ls=:dash, lw=2)
Enter fullscreen mode Exit fullscreen mode

Marker style

Graph showing all possible marker shapes

  • m: Marker shape
    • :circle
    • :diamond
    • :cross
    • :xcross
    • :utriangle
    • :dtriangle
    • :rtriangle
    • :ltriangle
    • :pentagon
    • :hexagon
    • :heptagon
    • :octagon
    • :star4
    • :star5
    • :star6
    • :star7
    • :star8
    • :vline
    • :hline
    • :none)
  • ms: Marker size
  • mc: Marker color

Example:

scatter(x, y, m=:diamond, ms=5, mc=:red)
Enter fullscreen mode Exit fullscreen mode

Colors and styles

  • c: Line or marker color
  • fillcolor: Fill color (for bar plots, histograms, etc.)
  • fillalpha: Fill transparency
  • alpha: Line or marker transparency

Example:

bar(x, y, c=:blue, fillcolor=:orange, fillalpha=0.5, alpha=0.8)
Enter fullscreen mode Exit fullscreen mode

Axis limits and scales

  • xlims: x-axis limits
  • ylims: y-axis limits
  • zlims: z-axis limits (for 3D plots)
  • xscale: x-axis scale
    • :linear
    • :log10
    • :log2
    • :log
  • yscale: y-axis scale
    • :linear
    • :log10
    • :log2
    • :log
  • zscale: z-axis scale (for 3D plots)

Example:

plot(x, y, xlims=(0,10), ylims=(-1, 1), xscale=:log10, yscale=:linear)
Enter fullscreen mode Exit fullscreen mode

Grid, Ticks, and Background

  • grid: Grid visibility (:on, :off)
  • gridcolor: Grid color
  • gridalpha: Grid transparency
  • gridstyle: Grid style
    • :solid
    • :dash
    • :dot
    • :dashdot
    • :dashdotdot
  • xticks: x-axis tick marks (vector of tick positions, :auto, :none)
  • yticks: y-axis tick marks (vector of tick positions, :auto, :none)
  • zticks: z-axis tick marks (for 3D plots)
  • tickfontsize: Tick label font size
  • background_color: Plot background color
  • foreground_color: Plot foreground color

Example:

plot(x, y, grid=:on, gridcolor=:grey, gridalpha=0.5, gridstyle=:dash, xticks=0:2:10, yticks=:auto, tickfontsize=12, background_color=:white, foreground_color=:black)
Enter fullscreen mode Exit fullscreen mode

Subplots and Layouts

layout: Subplot layout (tuple of rows and columns, e.g. (2, 3) for 2 rows and 3 columns)
title: Titles for each subplot (vector of strings)
Example:

plot(x, [y1, y2, y3, y4, y5, y6], layout=(2, 3), title=["Plot 1" "Plot 2" "Plot 3" "Plot 4" "Plot 5" "Plot 6"])
Enter fullscreen mode Exit fullscreen mode

Plots in VS Code

If you are using VS Code and Julia, each new plot you create will appear in the "Plot Navigator" on the Julia tab of your workspace.

Julia Plot Navigator

With a plot selected, you can choose to either save or delete it using the icons in the upper right portion of the screen.

VS Code plot options

Conclusion

This is a brief overview of the most commonly used plotting functions and attributes in Julia using the Plots.jl library. For a more comprehensive overview, refer to the official Plots documentation and explore the Plots Tutorial

If youโ€™re new to Julia, check out the Learn Julia crash course ๐Ÿš€

Julia Crash Course

Top comments (0)