# 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.

```julia
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

```julia
# 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.

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

**Output**

<figure><img src="https://3954978562-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fv4bMBkQ3ZBxkXjskIqBa%2Fuploads%2FejdvUsm8yoJDCbNzvBv9%2FScreenshot%202025-03-10%20at%204.38.37%E2%80%AFPM.png?alt=media&#x26;token=be7e6560-b19b-41f9-ae6b-aa95fdf09a2c" alt=""><figcaption></figcaption></figure>

## 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")`

```julia
# 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**

<figure><img src="https://3954978562-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fv4bMBkQ3ZBxkXjskIqBa%2Fuploads%2FSufPhVIcmrdLWAS9SA5N%2FScreenshot%202025-03-12%20at%2011.47.37%E2%80%AFAM.png?alt=media&#x26;token=09d2b500-ed87-49ee-a92c-19ba0009bc57" alt=""><figcaption></figcaption></figure>

## 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.&#x20;

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

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

## Resources

* JuliaPlots documentation: [installation](https://docs.juliaplots.org/stable/install/)
* JuliaPlots documentation: [creating a plot](https://docs.juliaplots.org/stable/tutorial/#Basic-Plotting:-Line-Plots)
* JuliaPlots documentation: [changing plot attributes](https://docs.juliaplots.org/stable/tutorial/#Plot-Attributes)
