LogoLogo
Computing Skills
Computing Skills
  • Introduction
  • File Directory Structures
  • Text Editors
  • GitHub
  • Unix
  • Julia
    • Installation
    • REPL
    • Basic Syntax
    • Numbers and Math
    • Strings and Characters
    • Regular Expressions
    • Control Flow
    • Collections and Data Structures
    • File Input/Output
    • Packages
    • DataFrames
    • JuliaPlots
    • ScikitLearn.jl
    • JuliaStats
    • Exercises
  • Python
    • Installation
    • REPL
    • Basic Syntax
    • Numbers and Math
    • Strings and Characters
    • Regular Expressions
    • Control Flow
    • Collections and Data Structures
    • File Input/Output
    • Packages
    • Data Frames and Data Manipulation
  • R
    • Installation
    • REPL
    • Basic Syntax
    • Numbers and Math
    • Strings and Characters
    • Regular Expression
    • Control Flow
    • Collections and Data Structures
    • File Input/Output
    • Packages
    • DataFrames
    • Data Analysis and Manipulation
Powered by GitBook
On this page
  • Installation & Setup
  • Creating a Plot
  • Adding/Modifying Plot Attributes
  • Saving Plots
  • Resources
Export as PDF
  1. Julia

JuliaPlots

JuliaPlots is one of the most popular data visualization packages for Julia as it is easy to use and interfaces with many other Julia packages.

Installation & Setup

To begin, import the "Plots" package and initialize it with the following code.

import Pkg
Pkg.add("Plots")

using Plots

Creating a Plot

Use plot to create a new plot, and plot! to add to an existing plot

# Create a new plot
plot(arguments)
# Add to current plot using plot!
plot!(arguments)
# Add to plot (not necessarily current) using plt
plot!(plt, arguments)

To create a first plot of sin(x), we will assign two variables and use the plot function to visualize them.

x = range(0, 10, length = 100)
y = sin.(x)
plot(x, y)

Output

Adding/Modifying Plot Attributes

There are many attributes you can modify to incorporate additional detail and/or change the style of a plot, such as titles, axis labels, line width, and legends, to name a few. In Plots, changing the modifier is as easy as typing the name of the attribute followed by an exclamation point (xlabel!). Below are some examples of attribute addition and modification.

The default for Plots is modifying the current plot. To modify the attribute of a plot other than the current one, include the plot name following the attribute. For example, to change the x-axis label of a plot called "plotname", you would write: xlabel!(plotname, "x")

# Plot data
x = range(0, 10, length = 100)
y1 = sin.(x)
y2 = cos.(x)

# Add labels to each y in the legend
plot(x, [y1 y2], label = ["sin(x)" "cos(x)"])

# Add attribute labels
xlabel!("x") # X-axis label
ylabel!("y") # Y-axis label
xlims!(0, 2pi) # Modifies the x-axis limits (previously 0-10)
plot!(legend=:outerbottom, legendcolumns = 2) # Moves legend outside of plot
title!("Visualizing Sine and Cosine Waves") # Add chart title

Output

Saving Plots

To save your plots from the Plots package, there are a few options depending on whether you want the plot to save as a .png or .pdf.

# Save as .png
savefig("plotname.png")
png("plotname")

# Save as .pdf
savefig(plotname, "plotname.pdf")
Plots.pdf(plotname, "plotname")

Resources

PreviousDataFramesNextScikitLearn.jl

Last updated 2 months ago

JuliaPlots documentation:

JuliaPlots documentation:

JuliaPlots documentation:

installation
creating a plot
changing plot attributes