Julia 101

Lesson 09: Data Visualization in Julia

MUHAMMAD SALMAN KABIR
2 min readSep 8, 2023

Data visualization is a crucial part of data analysis and interpretation. In Julia, the Plots library stands as a versatile tool for creating visualizations. In this article, we explore data visualization in Julia using the Plots library.

Installing Plots

We begin by installing the Plots library. Let’s open our Julia REPL and execute the following command

using Pkg
Pkg.add("Plots")

Once Plots is installed, we can start creating visualizations by importing the Plots module

using Plots

Our First Plot

Let’s begin with a simple line plot. Imagine we want to visualize cosine function

x = 0:0.01:4*pi
y = cos.(x)
plot(x,y)
xlabel!("x")
ylabel!("y")
title!("cos(x)")

Customizing Our Plot

Plots offers various customization options for enhanced plots. We can customize line colors, line style, line width and legend.

x = 0:0.01:4*pi
y = cos.(x)
plot(x,y,label="cos(x)",linestyle=:dash,linecolor="red",linewidth=5,grid=true)
xlabel!("x")
ylabel!("y")
title!("cos(x)")

Similarly, in scatter plot we can customize marker colors, marker style and marker width

x = 0:0.1:4*pi
y = cos.(x)
plot(x,y,label="cos(x)",marker=:diamond,markercolor="red",markerwidth=5,grid=false)
xlabel!("x")
ylabel!("y")
title!("cos(x)")

We can turn off the grid by setting grid=false

Multiple Plots

We can plot multiple lines in the same figure

x = 0:0.01:4*pi
plot(x,[cos.(x) sin.(x)],label=["cos(x)" "sin(x)"],linewidth=3)
xlabel!("x")
ylabel!("y")

or we can use layout to divide them into subplots.

x = 0:0.01:4*pi
plot(x,[cos.(x) sin.(x)],label=["cos(x)" "sin(x)"],layout(2,1),linewidth=3)
xlabel!("x")
ylabel!("y")

Using the layout , we created two subplots in a single figure, specifying two rows and one column.

Saving Plot

After creating our plot, we can save it as an image file using the savefig() command

savefig("plot.png")

This line saves the current plot as “plot.png” in the current working directory.

For more advanced features, it’s highly recommended to refer to the official documentation of Plots library.

--

--

MUHAMMAD SALMAN KABIR

Electrical Engineer | Signal Processing & Machine Learning Enthusiast