# Set a minimum level in contour plots

**URL:** <https://discourse.julialang.org/t/set-a-minimum-level-in-contour-plots/117907>\
**Category:** Visualization\
**Tags:** question, plots, pythonplot, contouring\
**Created:** [August 7, 2024, 4:12am UTC](https://discourse.julialang.org/t/set-a-minimum-level-in-contour-plots/117907 "2024-08-07T04:12:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sh\_166](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sh_166/32/43965_2.png) [@Sh\_166](https://discourse.julialang.org/u/Sh_166)\
**Post date:** [August 7, 2024, 4:12am UTC](https://discourse.julialang.org/t/set-a-minimum-level-in-contour-plots/117907/1 "2024-08-07T04:12:58Z")

</div>

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

MWE:

```julia
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:

 ![Screenshot 2024-08-07 120007](https://global.discourse-cdn.com/julialang/original/3X/b/8/b8eb7efa0f7679365838c312e54024ef08773d28.jpeg)

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:

 ![Screenshot 2024-08-07 130945](https://global.discourse-cdn.com/julialang/original/3X/f/2/f2f3eec871c5d9d2d17a099ba31bc6495ad125f2.jpeg)

The code for this is:

```julia
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!

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 7, 2024, 6:54am UTC](https://discourse.julialang.org/t/set-a-minimum-level-in-contour-plots/117907/2 "2024-08-07T06:54:16Z")

</div>

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

```julia
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))

```

---

<div class="post-metadata">

**Author:** ![Sh\_166](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sh_166/32/43965_2.png) [@Sh\_166](https://discourse.julialang.org/u/Sh_166)\
**Post date:** [August 7, 2024, 8:02am UTC](https://discourse.julialang.org/t/set-a-minimum-level-in-contour-plots/117907/3 "2024-08-07T08:02:37Z")

</div>

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

 ![Screenshot 2024-08-07 170140](https://global.discourse-cdn.com/julialang/original/3X/3/8/38465c15817cb32a645550db4e7b1a00986f0ccc.jpeg)

---

<div class="post-metadata">

**Author:** ![Sh\_166](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sh_166/32/43965_2.png) [@Sh\_166](https://discourse.julialang.org/u/Sh_166)\
**Post date:** [August 7, 2024, 8:10am UTC](https://discourse.julialang.org/t/set-a-minimum-level-in-contour-plots/117907/4 "2024-08-07T08:10:14Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 7, 2024, 8:11am UTC](https://discourse.julialang.org/t/set-a-minimum-level-in-contour-plots/117907/5 "2024-08-07T08:11:20Z")

</div>

I get this:  
 ![Plots_python_plot_specific_contour_levels_legend](https://global.discourse-cdn.com/julialang/original/3X/f/b/fb2395666fc2aa88551a21d890627c66ba5ed2dc.png)

with this code:

```julia
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))

```

---

<div class="post-metadata">

**Author:** ![Sh\_166](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sh_166/32/43965_2.png) [@Sh\_166](https://discourse.julialang.org/u/Sh_166)\
**Post date:** [August 7, 2024, 8:16am UTC](https://discourse.julialang.org/t/set-a-minimum-level-in-contour-plots/117907/6 "2024-08-07T08:16:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 7, 2024, 9:56am UTC](https://discourse.julialang.org/t/set-a-minimum-level-in-contour-plots/117907/7 "2024-08-07T09:56:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sh\_166](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sh_166/32/43965_2.png) [@Sh\_166](https://discourse.julialang.org/u/Sh_166)\
**Post date:** [August 7, 2024, 10:43am UTC](https://discourse.julialang.org/t/set-a-minimum-level-in-contour-plots/117907/8 "2024-08-07T10:43:08Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 7, 2024, 10:55am UTC](https://discourse.julialang.org/t/set-a-minimum-level-in-contour-plots/117907/9 "2024-08-07T10:55:36Z")

</div>

It works fine as follows:

```julia
savefig("figurename.png")

```

---

<div class="post-metadata">

**Author:** ![Sh\_166](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sh_166/32/43965_2.png) [@Sh\_166](https://discourse.julialang.org/u/Sh_166)\
**Post date:** [August 7, 2024, 11:42am UTC](https://discourse.julialang.org/t/set-a-minimum-level-in-contour-plots/117907/10 "2024-08-07T11:42:15Z")

</div>

This worked for me:

```julia
PythonPlot.savefig("figurename.pdf")

```
