Set a minimum level in contour plots

I want to make a contour plot but I only want specific contours - or levels above a certain range.

MWE:

Pkg.add("Symbolics")
Pkg.add("Plots")
Pkg.add("LaTeXStrings")
Pkg.add("Measures")
Pkg.add("PlotThemes")

using Symbolics, Plots, LaTeXStrings, Measures, PlotThemes

mₕ = 125
v = 246
mₜ = 172.76
Nₜ = 3

k = (pi*mₕ*v)
s = 1 - (Nₜ/(3*k^2))mₜ^4

f(X, Y) = (100/(3*s*k^2))*(((1-(X/Y)^2)^3))*(Y^4)

X = range(0, 200, length=1000)
Y = range(0, 500, length=2000)
Z = @. f(X', Y)

contour(X, Y, Z)

This function takes values in a broad range and the output from this code is this:

It is clear from the colour bar that the contours chosen to be plotted are extremely small - what I want, however, are contours with z = 5, 10, 30, 50, 70, 100 - so, I don’t really need these particular contours.

However, I am not sure what exactly to do here to get what I want. As an example of what I want to plot, this is the same function plotted in Mathematica:

The code for this is:

f[x_, y_] := ((1/(3*Pi^2*125^2*246^2))*
    y^4*(1 - x^2/y^2)^3)/(1 - ((172.76)^4)/(Pi^2*125^2*246^2))

ContourPlot[{f[x, y] == 0.05, f[x, y] == 0.1, f[x, y] == 0.3, 
  f[x.y] == 0.5, f[x, y] == 0.7, f[x, y] == 1}, {x, 0, 200}, {y, 150, 
  480}, Frame -> True, PlotTheme -> "Scientific", 
 PlotLegends -> {"5 %", "10 %", "30 %", "50 %", "70 %", "100 %"}, 
 ContourStyle -> Dashed, GridLines -> Automatic]

Any help would be appreciated!

Hi, one idea using the keyword colorbar_ticks from the pythonplot() backend:

using Plots; pythonplot(dpi=100)
...

zc = [5, 10, 30, 70, 100]
contour(X, Y, Z, levels=zc, lw=2, colorbar_ticks=(zc,string.(zc) .* "%"), clims=extrema(zc))

Thanks for the response @rafael.guerra, but this doesn’t seem to be working. My output is this:

Ah, sorry, forgot to load the PythonPlot backend that time. But with the package loaded, I get an error:

UndefVarError: `pyisnone` not defined

Stacktrace:
  [1] getproperty
    @ .\Base.jl:31 [inlined]
  [2] _py_bbox(obj::PythonCall.Core.Py)
    @ Plots C:\Users\shiha\.julia\packages\Plots\rz1WP\src\backends\pythonplot.jl:219
  [3] _update_min_padding!(sp::Plots.Subplot{Plots.PythonPlotBackend})
    @ Plots C:\Users\shiha\.julia\packages\Plots\rz1WP\src\backends\pythonplot.jl:1253
  [4] iterate
    @ .\generator.jl:47 [inlined]
  [5] _collect(c::Matrix{AbstractLayout}, itr::Base.Generator{Matrix{AbstractLayout}, typeof(Plots._update_min_padding!)}, #unused#::Base.EltypeUnknown, isz::Base.HasShape{2})
    @ Base .\array.jl:802
  [6] collect_similar
    @ .\array.jl:711 [inlined]
  [7] map
    @ .\abstractarray.jl:3261 [inlined]
  [8] _update_min_padding!(layout::Plots.GridLayout)
    @ Plots C:\Users\shiha\.julia\packages\Plots\rz1WP\src\layouts.jl:277
  [9] prepare_output(plt::Plots.Plot{Plots.PythonPlotBackend})
    @ Plots C:\Users\shiha\.julia\packages\Plots\rz1WP\src\plot.jl:239
 [10] show
    @ C:\Users\shiha\.julia\packages\Plots\rz1WP\src\output.jl:231 [inlined]
 [11] #348
    @ C:\Users\shiha\.julia\packages\Plots\rz1WP\src\output.jl:27 [inlined]
 [12] open(::Plots.var"#348#349"{Plots.Plot{Plots.PythonPlotBackend}}, ::String, ::Vararg{String}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Base .\io.jl:395
 [13] open
    @ .\io.jl:392 [inlined]
 [14] pdf(plt::Plots.Plot{Plots.PythonPlotBackend}, fn::String)
    @ Plots C:\Users\shiha\.julia\packages\Plots\rz1WP\src\output.jl:27
 [15] savefig(plt::Plots.Plot{Plots.PythonPlotBackend}, fn::String)
    @ Plots C:\Users\shiha\.julia\packages\Plots\rz1WP\src\output.jl:149
 [16] savefig(fn::String)
    @ Plots C:\Users\shiha\.julia\packages\Plots\rz1WP\src\output.jl:154
 [17] top-level scope
    @ In[99]:4

I get this:
Plots_python_plot_specific_contour_levels_legend

with this code:

using Plots; pythonplot(dpi=100)

f(X, Y) = (100/(3*s*k^2))*(((1-(X/Y)^2)^3))*(Y^4)

mₕ, v, mₜ, Nₜ = 125, 246, 172.76, 3
k= pi*mₕ*v
s = 1 - (Nₜ/(3*k^2))mₜ^4
X, Y = range(0, 200, 1000), range(0, 500, 2000)
Z = @. f(X', Y)
zc = [5, 10, 30, 70, 100]
contour(X, Y, Z, levels=zc, lw=2, colorbar_ticks=(zc,string.(zc) .* "%"), clims=extrema(zc))

I copied your code into a fresh notebook and ran it but I got no output. If I try to save the non-existent figure, I get the error I posted earlier. I am not sure what’s wrong.

I don’t use notebooks, so I cannot help further.
Perhaps try putting the contour command inside the display() function.

This worked! Thanks - I didn’t know there was a display() function too. However, my saving issue hasn’t gone - if I use savefig(), that earlier error crops up. This isn’t unique to Jupyter notebooks either - occurs in the REPL too. Any workaround?

It works fine as follows:

savefig("figurename.png")

This worked for me:

PythonPlot.savefig("figurename.pdf")