Plotting#

Learning Objectives#

  • Understand the basic capabilities of plotting libraries in Julia

  • Install and use Plots.jl package for creating visualisations

  • Create and customize various types of plots, including line plots, scatter plots, and bar plots

  • Add titles, labels, and legends to plots

  • Save plots in different formats such as PNG

Overview of Plotting Capabilities#

Julia offers powerful plotting libraries that cater to both simple and complex data visualization needs. Two popular libraries are plots.jl and Gadfly.jl.

Basic Plotting with Plots.jl#

Plotting in Julia is primarily done through the use of Plots.jl, a versatile and popular plotting library in Julia.

Installation#

We installed the package before, but it is a good idea to double check that it is installed, and then import the package into this session.

using Pkg
Pkg.add("Plots")
using Plots
   Resolving package versions...
  No Changes to `~/Documents/CfRR/Website_Build/CfRR_Courses/Project.toml`
  No Changes to `~/Documents/CfRR/Website_Build/CfRR_Courses/Manifest.toml`

Line Plot#

x = 1:10
y = rand(10)
Plots.plot(x, y, title="Line Plot", xlabel="X-axis", ylabel="Y-axis")

Scatter Plot#

Plots.scatter(x, y, title="Scatter Plot", xlabel="X-axis", ylabel="Y-axis")

Bar Plot#

Plots.bar(x, y, title="Bar Plot", xlabel="X-axis", ylabel="Y-axis")

Customizing Plots#

There are a range of different methods for customizing plots within Julia, some of which are covered below:

Adding Titles and Labels#

plot(x, y, title="Custom Line Plot Title", xlabel="Custom X-axis", ylabel="Custom Y-axis") 

Changing Lines Styles and Colors#

plot(x, y, linestyle=:dash, linecolor=:red, linewidth=2)

Adding Legends#

plot(x, y, label="Random Data", legend=:topright)

Saving Plots#

Once you’ve created a plot in Julia using the Plots.jl package, you may want to save it as an image file for use in reports, presentations, or other documents. The Plots.jl package makes it straightforward to export your plots to various formats, including .png.

Steps to Export a Plot to .png#

Saving a plot to a .png file can be achieved with the Plots.savefig() function.

# Create the plot
x = 1:10
y = rand(10)
Plots.plot(x, y, title="Sample Plot", xlabel="X-axis", ylabel="Y-axis")

# Save the plot as a .png file
#Plots.savefig("sample_plot.png")

Further Functionality#

Plots.jl has a range of functionality that is outside the scope of this course to cover, but below are some of the main sections of Plots.jl functionality that is likely to be of interest.

Composing Plots#

Advanced Plotting#

Animations#

For more detailed information and advanced usage, refer to the official Plots.jl documentation. This resource provides comprehensive guides and examples to help you master plotting in Julia using Plots.jl.