问题
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.)
回答1:
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.
来源:https://stackoverflow.com/questions/50907491/how-to-overlay-contour-plots-in-julia-using-plots-with-pyplot-backend