logo

Adjusting the Position of the Legend in Julia Plots 📂Julia

Adjusting the Position of the Legend in Julia Plots

Overview 1

The position of the legend can be freely adjusted with the legend option of the plot() function. Giving a 2-tuple comprised of values between $0$ and $1$ will exactly place it at that location, otherwise, it can be controlled by symbols.

Symbols combine top/bottom and left/right in order. Adding an outer at the very beginning places the legend outside of the plot. Examples of symbols that can be created through combinations include:

  • :bottom
  • :left
  • :bottomleft
  • :outertopright

Since the order must be connected, symbols like :leftbottom or :toprightouter are not allowed.

Code

data = randn(100, 2)
plot(data)
plot(data, legend = (0.5, 0.7)); png("tuple")
Symbols = [:none, :bottom, :left, :bottomleft, :outertopright, :inline]
for symbol ∈ Symbols
    plot(data, legend = symbol)
    png(string(symbol))
end

Specifying exact location legend = (0.5, 0.7)

tuple.png

The tuple (0.5, 0.7) places the legend at about 50% of the horizontal axis and 70% of the vertical height.

Removing the legend :none

none.png

Top, Bottom, Left, Right :bottom, :left

bottom.png

left.png

Combination :bottomleft

bottomleft.png

Outside :outertopright

outertopright.png

The legend is moved outside of the plot. Note that this may distort the figure.

End-of-line :inline

inline.png

Useful when there are many lines that are hard to distinguish by color, or when the last value is particularly important.

Environment

  • OS: Windows
  • julia: v1.6.3