How to overlay contour plots in Julia (using Plots with PyPlot backend)

偶尔善良 提交于 2019-12-11 15:15:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!