1

I'm trying to overlay two contour plots in Julia using Plots with PyPlot backend. Is it possible, and, if so, how?

An MWE could look something like this:

using Plots
pyplot()
a = rand(50,50)
b = rand(50,50)
p1 = contour(a,seriescolor=:blues)
p2 = contour(b,seriescolor=:reds)
plot(p1,p2,layout=1)

(This code generates ERROR: When doing layout, n (1) != n_override (2). I do understand the error, but I do not know how to get around it.)

anothernode
  • 5,100
  • 13
  • 43
  • 62

1 Answers1

2

Solution

Use contour!:

using Plots
pyplot()
a = rand(50,50)
b = rand(50,50)
contour(a,seriescolor=:blues)
contour!(b,seriescolor=:reds)

The first contour plots a. The second contour! plots b on the same canvas where a is plotted.

Why the !?

The ! is a convention idiomatic to Julia (and some other languages). Julia doesn't give any special meaning to but, but developers do: The convention is to append a ! to a method declaration when the method changes existing state, e.g. modifies one of its arguments. In this case, contour! is used to modify an existing plot by overlaying it with another plot.

Robert Hönig
  • 645
  • 1
  • 8
  • 19
  • Thanks! And extra special thanks for the exclamation mark use in this case. The main PyPlot examples page mentions some functions with the exclamation mark as "shorthand functions" but does not really dwell on what they do. – Arseniy Tsipenyuk Jun 19 '18 at 09:10
  • You probably mean the main `Plots` example page? The Julia Plots package is backend agnostic, PyPlot is just one of several backends you can use with it. Personally, I prefer the Plotly backend :) – Robert Hönig Jun 19 '18 at 12:46