Basic Concepts
Use plot
to create a new plot object, and plot!
to add to an existing one:
plot(args...; kw...) # creates a new Plot, and set it to be the `current`
plot!(args...; kw...) # modifies Plot `current()`
plot!(plt, args...; kw...) # modifies Plot `plt`
The graphic is not shown implicitly, only when "displayed". This will happen automatically when returned to a REPL prompt or to an IJulia cell. There are many other options as well.
Input arguments can take many forms. Some valid examples:
plot() # empty Plot object
plot(4) # initialize with 4 empty series
plot(rand(10)) # 1 series... x = 1:10
plot(rand(10,5)) # 5 series... x = 1:10
plot(rand(10), rand(10)) # 1 series
plot(rand(10,5), rand(10)) # 5 series... y is the same for all
plot(sin, rand(10)) # y = sin.(x)
plot(rand(10), sin) # same... y = sin.(x)
plot([sin,cos], 0:0.1:π) # 2 series, sin.(x) and cos.(x)
plot([sin,cos], 0, π) # sin and cos on the range [0, π]
plot(1:10, Any[rand(10), sin]) # 2 series: rand(10) and map(sin,x)
@df dataset("Ecdat", "Airline") plot(:Cost) # the :Cost column from a DataFrame... must import StatsPlots
Keyword arguments allow for customization of the plot, subplots, axes, and series. They follow consistent rules as much as possible, and you’ll avoid common pitfalls if you read this section carefully:
-
Many arguments have aliases which are replaced during preprocessing.
c
is the same ascolor
,m
is the same asmarker
, etc. You can choose a verbosity that you are comfortable with. -
There are some special arguments which magically set many related things at once.
-
If the argument is a "matrix-type", then each column will map to a series, cycling through columns if there are fewer columns than series. In this sense, a vector is treated just like an "nx1 matrix".
-
Many arguments accept many different types… for example the color (also markercolor, fillcolor, etc) argument will accept strings or symbols with a color name, or any Colors.Colorant, or a ColorScheme, or a symbol representing a ColorGradient, or an AbstractVector of colors/symbols/etc…
Useful Tips
A common error is to pass a Vector when you intend for each item to apply to only one series. Instead of an n-length Vector, pass a 1xn Matrix. |
You can update certain plot settings after plot creation:
|
With supported backends, you can pass a |
You can see the default value for a given argument with |
Call |