# Plots.contourf and PlotlyJS.contour behaviour with regard to the x,y,z input

**URL:** https://discourse.julialang.org/t/plots-contourf-and-plotlyjs-contour-behaviour-with-regard-to-the-x-y-z-input/122897
**Category:** Visualization
**Tags:** visualization, plots, plotlyjs
**Created:** [November 21, 2024, 1:35pm UTC](https://discourse.julialang.org/t/plots-contourf-and-plotlyjs-contour-behaviour-with-regard-to-the-x-y-z-input/122897 "2024-11-21T13:35:32Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [November 22, 2024, 7:17pm UTC](https://discourse.julialang.org/t/plots-contourf-and-plotlyjs-contour-behaviour-with-regard-to-the-x-y-z-input/122897/2 "2024-11-22T19:17:52Z")

</div>

With `ScatteredInterpolation.jl` we can get from the three vectors, x, y, z, a matrix zg, consisting in interpolated values at the points of a meshgrid associated to linear grids, xg and yg.  
As long as we don’t know what interpolation method is used by  
`plotly.js`, we cannot obtain exactly the same contours. Among different methods of interpolation from `ScatteredInterpolation.jl`, the radial basis function method with `Multiquadratic` basis function defines a `Plots.contourf` somehow similar to `PlotlyJS.contour`.

```julia
using ScatteredInterpolation, Plots, ColorSchemes
let 
    x = [1, 2, 3, 1]
    y = [1, 1, 2, 2]
    z = [1.0, 2, 3, 4]
    points = stack(zip(x,y)) 
    itp = interpolate(Multiquadratic(), points, z);

    xm, xM = extrema(x)
    ym, yM = extrema(y)
    n = 50
    m = 25
    xg = range(xm, xM, n)
    yg = range(ym, yM, m)
    X = [s for t in yg, s in xg] #size(X) is (m,n)
    Y = [t for t in yg, s in xg] # size(Y) is also (m,n)
    gridP = stack(vec([[x, y] for (x,y) in zip(X, Y)])) #2 x m*n - matrix; each column is a grid point
    interpolated = evaluate(itp, gridP)
    zg = reshape(interpolated, m, n)
    p = Plots.contourf(xg, yg, zg; levels=6, c=cgrad(ColorSchemes.plasma))
end    

```

 ![contourxyzpng](https://global.discourse-cdn.com/julialang/original/3X/9/4/94d29763de1388a25d9d3552da4aa9ac240daf34.png)

---

_[View the full topic](https://discourse.julialang.org/t/plots-contourf-and-plotlyjs-contour-behaviour-with-regard-to-the-x-y-z-input/122897)._
