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 Plotsx = 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
GKSwstypeto minimize the loading and compilation time of thePlots.jlpackage.Line 2: We load the
Plots.jlmodule.Line 3: We create a range from
0to10with80elements representing thexcoordinates.Line 4: We create a vector that represents the
ycoordinates, where each element is evaluated to thecos(x)function.Lines 5–10: We invoke the
plotfunction while specifying the variablesxandyas 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 Plotsx=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
GKSwstypeto minimize the loading and compilation time of thePlots.jlpackage.Line 2: We load the
Plots.jlmodule.Line 3: We create a range of elements from
1to5representing thexcoordinates.Line 4: We create a single-dimensional vector of random values using the
rand()function that represents theycoordinates.Lines 5–10: We invoke the
plotfunction while specifying the variablesxandyas 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 PlotspopularityIndex = [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
GKSwstypeto minimize the loading and compilation time of thePlots.jlpackage.Line 2: We load the
Plots.jlmodule.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
plotfunction while specifying the variableslanguagesandpopularityIndexas parameters and the title to add to the plot. Theseriestypeargument indicates the type of the generated chart.Lines 6–7: We set appropriate labels to the
xandyaxes.
Free Resources