How to create plots using Julia

A plot is a graphical representation of data. It serves to extract actionable insights from data.

Julia's ecosystem encompasses many plotting packages like Gadfly, Vega-Lite, and Plotly.js.

Although there are various packages, Plots.jl stands out among others and is considered the standard data visualization and plotting tool in Julia. This open-source library is comprehensive, intuitive, lightweight, and very popular.

This plotting meta-package acts like a unified interface to a variety of plotting libraries (backends) like GR, Plotly, and Matplotlib (PyPlot).

After this introduction, let's create some plots using this library.

Plotting a trigonometric function

This example revolves around plotting a trigonometric function:

ENV["GKSwstype"] = "nul"
using Plots
x = range(0, 10, length=80)
y = cos.(x)
p = plot(x
, y
, title="Plotting A Trigonometric Function"
, label=["cos(x)"]
, linewidth=3
)

Now, let's explain the above code widget:

  • Line 1: We initialize the environment variable GKSwstype to minimize the loading and compilation time of the Plots.jl package.

  • Line 2: We load the Plots.jl module.

  • Line 3: We create a range from 0 to 10 with 80 elements representing the x coordinates.

  • Line 4: We create a vector that represents the y coordinates, where each element is evaluated to the cos(x) function.

  • Lines 5–10: We invoke the plot function while specifying the variables x and y as parameters, a title to add to the plot, and the line width to be drawn.

Plotting a line chart

This example focuses on plotting a line chart:

ENV["GKSwstype"] = "nul"
using Plots
x=1:5;
y=rand(5,1);
p=plot(x
,y
,title="Plotting a Line Chart"
)

Now, let's go over the above code widget:

  • Line 1: We initialize the environment variable GKSwstype to minimize the loading and compilation time of the Plots.jl package.

  • Line 2: We load the Plots.jl module.

  • Line 3: We create a range of elements from 1 to 5 representing the x coordinates.

  • Line 4: We create a single-dimensional vector of random values using the rand() function that represents the y coordinates.

  • Lines 5–10: We invoke the plot function while specifying the variables x and y as parameters and the title to add to the plot.

Plotting a bar chart

This example shows how to plot a bar chart:

ENV["GKSwstype"] = "nul"
using Plots
popularityIndex = [14.4, 10.1, 13.7];
languages = ["Python","Java","C++"];
p=plot(languages, popularityIndex,seriestype=:bar,title="Popularity of Programming Languages")
xlabel!("Programming Languages")
ylabel!("Index of Popularity")

Now, let's scrutinize the above code widget:

  • Line 1: We initialize the environment variable GKSwstype to minimize the loading and compilation time of the Plots.jl package.

  • Line 2: We load the Plots.jl module.

  • Line 3: We create a range of values representing the index of the popularity of some programming languages.

  • Line 4: We define a set of programming languages.

  • Line 5: We invoke the plot function while specifying the variables languages and popularityIndex as parameters and the title to add to the plot. The seriestype argument indicates the type of the generated chart.

  • Lines 6–7: We set appropriate labels to the x and y axes.

Copyright ©2024 Educative, Inc. All rights reserved