8

Is there a way to have the second dataset plot on a separate axis, overlaid on the first plot?

using Plots; gadfly(size=(800,400))

plot(Vector[randn(100)], line = ([:green], :step))

plot!(Vector[randn(100)], line = ([:red], :step))
BAR
  • 15,909
  • 27
  • 97
  • 185

4 Answers4

12

It is now done by adding a twinx() argument:

plot(rand(10))
plot!(twinx(),100rand(10))

enter image description here

There is, however, some unintentional axis and label behavior:

  1. Labels are plotted on top of each other by default
  2. xticks are plotted on top of the already existing plot
  3. Default colors are not correlated to the total number of series in the subplot

Therefore, I suggest adding some additional arguments:

plot(rand(10),label="left",legend=:topleft)
plot!(twinx(),100rand(10),color=:red,xticks=:none,label="right")

enter image description here

There still seems to be an issue correlating all series associated with the subplot at the moment.

DisabledWhale
  • 771
  • 1
  • 7
  • 15
5

It's easy, but doesn't work with Gadfly. It should work fine with PyPlot and GR. Here's an example:

enter image description here

Tom Breloff
  • 1,782
  • 9
  • 15
  • Tom, what if I want 2 axis on right and another 2 on left? – BAR Mar 24 '16 at 20:22
  • 1
    It's not supported directly through Plots, but it could be for backends that support it. Feel free to open an issue, or even better: submit a PR! – Tom Breloff Mar 25 '16 at 15:07
  • 8
    If anyone found this like me: This key is no longer supported. Currently you can achieve this behaviour through twinx(): https://github.com/tbreloff/Plots.jl/issues/378 – Tyde Oct 27 '16 at 12:37
1

If you have multiple series you want to plot and want to add your data incrementally, you can do it like this:

p = plot()
p_twin = twinx(p)

plot!(p,x,y)
plot!(p_twin,x,y_twin)
-3

I can confirm (for GR) using Plots; gr()

screenshot

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77