# Contour plots with varying locations for x-axis?

**URL:** https://discourse.julialang.org/t/contour-plots-with-varying-locations-for-x-axis/61686
**Category:** Visualization
**Tags:** plotting
**Created:** [May 23, 2021, 4:43pm UTC](https://discourse.julialang.org/t/contour-plots-with-varying-locations-for-x-axis/61686 "2021-05-23T16:43:57Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)
#### Post date: [May 23, 2021, 4:43pm UTC](https://discourse.julialang.org/t/contour-plots-with-varying-locations-for-x-axis/61686/1 "2021-05-23T16:43:57Z")

</div>

Hi, I have a set of 2-dimensional data, defined over an array of timesteps, but at each timestep the position x-values are different. I’m trying to get contour plots, but the plots come up blank, and each time I get the error message: `Arrays have incorrect length or dimension.` An example code model excerpt goes like:

```julia
using Plots

tNum = 4; xNum = 3; 
# These numbers will be >1000 in the real calc

FunctionForPlot = Array{Float64,2}(undef, tNum, xNum)
xValsArr = Array{Float64,2}(undef, tNum, xNum)
tValsArr = Array{Float64}(undef, tNum)

for i in 1:tNum
        tValsArr[i] = i
    for j in 1:xNum
        xValsArr[i,j] = i + j^2
        FunctionForPlot[i,j] = i*sin(35*j)
    end
end

contour([xValsArr[i,j] for i=1:tNum, j=1:xNum],
         [tValsArr[i] for i=1:tNum, j=1:xNum],
        [FunctionForPlot[i,j] for i=1:tNum, j=1:xNum], fill=true)

```

Any suggestions on how to make this work, or why I’m not getting the plot I expected, would be welcome. Thank you!

---

<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: [May 24, 2021, 2:43pm UTC](https://discourse.julialang.org/t/contour-plots-with-varying-locations-for-x-axis/61686/2 "2021-05-24T14:43:22Z")

</div>

To draw the contour plot you must interpolate from your initial data, x, y, z, that define a function f(x[i,j], y[i, j])=z[i,j], the value of f at a regular grid, defined by  
xi= 2:0.5:13, yi=1:4. I tried Dierckx.jl, but the Spline2D cannot be called because the flattened x-array contains the repeating value 5. Maybe Interpolations.jl provides an adequate function for 2D interpolation.

---

<div class="post-metadata">

### Author: ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)
#### Post date: [May 25, 2021, 7:34am UTC](https://discourse.julialang.org/t/contour-plots-with-varying-locations-for-x-axis/61686/3 "2021-05-25T07:34:10Z")

</div>

Hi empet, thanks for the response!

Thinking about it, it does seem likely that Julia cannot do contour plots (or similar 2D plots) on an irregularly-spaced grid (as defined by irregularly-spaced data), but that the data must somehow be interpolated to a regularly-spaced grid for contour (or similar) plots.

If that’s incorrect, and anyone does know how to extend contour plots to idiosyncratically-spaced data, please let me know…!

---

<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: [May 25, 2021, 8:00am UTC](https://discourse.julialang.org/t/contour-plots-with-varying-locations-for-x-axis/61686/4 "2021-05-25T08:00:08Z")

</div>

Since I was not able to use Dierckx.jl and have no experience with Interpolations.jl (I’m a Julia beginner) I tried with Python `scipy.interpolate` and your contour plot looks like this:  
 ![contour](https://global.discourse-cdn.com/julialang/original/3X/d/3/d3cf296ec11cbfba9e61e1f584a26b2e260862de.png)

The steps for getting a regular grid of values Xi, Yi, zi for which the Plots.jl contour could work:

```julia
x, y, z are the array passed by you as arguments for contour
xi = 2:0.5:13
yi = 1:0.5:4
fxy = scipy.interpolate.interp2d(x, y, z) #you should find the Interpolations.jl 
                                           #function that works like this scipy.interpolation function
zi = fxy(xi, yi)
Xi = xi' .* ones(length(yi))
Yi = ones(length(xi))'.*yi
contour(Xi, Yi, zi)

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [May 25, 2021, 8:28am UTC](https://discourse.julialang.org/t/contour-plots-with-varying-locations-for-x-axis/61686/5 "2021-05-25T08:28:16Z")

</div>

> [@CosmoProf](#):
>
> Julia cannot do contour plots (or similar 2D plots) on an irregularly-spaced grid (as defined by irregularly-spaced data)

The commonly used algorithm ([marching squares](https://en.wikipedia.org/wiki/Marching_squares)) requires a regular grid. There are other algorithms for eg triangle meshes, but for the purposes of plotting it is common to just approximate the surface and then compute on a regular grid.

---

<div class="post-metadata">

### Author: ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)
#### Post date: [May 25, 2021, 7:16pm UTC](https://discourse.julialang.org/t/contour-plots-with-varying-locations-for-x-axis/61686/6 "2021-05-25T19:16:02Z")

</div>

Hi Tamas, thanks for the clarification. I understand what I have to do a little better now.

Empet, thanks for the sample scipy code, I’m sure this will come in handy for interpolating the data, which I plan to do soon.

I think I know how to proceed, thanks again guys!

---

<div class="post-metadata">

### Author: ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)
#### Post date: [June 3, 2021, 2:38am UTC](https://discourse.julialang.org/t/contour-plots-with-varying-locations-for-x-axis/61686/7 "2021-06-03T02:38:30Z")

</div>

Hi again, I’m revisiting this attempt, trying to mimic what I want with a 2D scatterplot (with distinct x and y values for each point), where the value of the function for each point is indicated by its color, on a color scale. I’ve searched online and tried doing this with `PGFPlotsX` and `Gadfly`, but I can’t find enough info to get the syntax right. Does anyone know how to get a simple example like the following to work:

```julia
using Plots
xData = [100.0*rand(Float64) for i in 1:100]
yData = [50.0*rand(Float64) for j in 1:100]
zData = [(xData[i] + yData[j]) for i=1:100, j=1:100]

scatter(xData , yData , color = zData) # This does NOT work, simplest fix??

```

It would also be nice if I could get each (variously colored) point to be plotted as a filled square (instead of just a point), so it would kind of look like a heatmap or contour plot (with gaps I guess).

Thanks for any suggestions…!

---

<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: [June 7, 2021, 11:00am UTC](https://discourse.julialang.org/t/contour-plots-with-varying-locations-for-x-axis/61686/8 "2021-06-07T11:00:27Z")

</div>

@CosmoProf

I succeeded to define a plot following your requirement, using PlotlyJS.jl:

```julia
using PlotlyJS
m=75
n=100

xData = [100.0*rand(Float64) for i in 1:n]
yData = [50.0*rand(Float64) for j in 1:m]
X, Y = [x for y in yData, x in xData], [y for y in yData, x in xData]
zData = X+Y
trace = PlotlyJS.scatter(x=vec(X), y=vec(Y), mode="markers", 
                       marker=attr(symbol="square", color=vec(zData),
                                   showscale=true, colorbar_thickness=23))
pl = PlotlyJS.plot(trace, Layout(width=500, height=500, title_text="Your title",
        xaxis=attr(range=[minimum(X), maximum(X)], showgrid=false),
        yaxis=attr(range=[minimum(Y), maximum(Y)], showgrid=false)))

```

![scatter2](https://global.discourse-cdn.com/julialang/original/3X/c/1/c145d999be7fa935ef098b90f35efa7b8b0309dc.png)

The above “heatmap” was defined with the default size for the marker.  
Setting the marker attribute size=2.5, and the plasma colormap:

```julia
plasma=[[0.0, "#0c0786"],
 [0.1, "#40039c"],
 [0.2, "#6a00a7"],
 [0.3, "#8f0da3"],
 [0.4, "#b02a8f"],
 [0.5, "#cb4777"],
 [0.6, "#e06461"],
 [0.7, "#f2844b"],
 [0.8, "#fca635"],
 [0.9, "#fcce25"],
 [1.0, "#eff821"]]

```

the plot is as follows:  
 ![scatter2_5](https://global.discourse-cdn.com/julialang/original/3X/9/4/94428af8491dd25f944f723e26f64e561dc755b3.png)

Your intuition was good, and the heatmap with gaps is very interesting 🙂

---

<div class="post-metadata">

### Author: ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)
#### Post date: [June 7, 2021, 3:02pm UTC](https://discourse.julialang.org/t/contour-plots-with-varying-locations-for-x-axis/61686/9 "2021-06-07T15:02:07Z")

</div>

If I got you right, the following should work

```julia
using Plots
xData = [100.0*rand(Float64) for i in 1:100]
yData = [50.0*rand(Float64) for j in 1:100]
zData = [(xData[i] + yData[j]) for i=1:100, j=1:100]

scatter(xData , yData , marker_z = zData, markershape = :square)

```

---

<div class="post-metadata">

### Author: ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)
#### Post date: [June 8, 2021, 4:17am UTC](https://discourse.julialang.org/t/contour-plots-with-varying-locations-for-x-axis/61686/10 "2021-06-08T04:17:24Z")

</div>

Hello BeastyBlacksmith, I tried your suggestion, but it does not seem to produce a proper 2-dimensional plot. If you replace the `zData` function with something sinusoidally-varying instead, it varies in the x-direction but not in the y-direction (it should do both). See the following example, which does not produce the fully two-dimensionally varying result expected:

```julia
using Plots
xData = [100.0*rand(Float64) for i in 1:1000]
yData = [50.0*rand(Float64) for j in 1:1000]
zData = [(sin(xData[i]/5)*cos(yData[j]/3)) for i=1:1000, j=1:1000]

scatter(xData , yData , marker_z = zData, markershape = :square)

```

---

<div class="post-metadata">

### Author: ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)
#### Post date: [June 8, 2021, 5:57am UTC](https://discourse.julialang.org/t/contour-plots-with-varying-locations-for-x-axis/61686/11 "2021-06-08T05:57:07Z")

</div>

I also tried creating a hybrid solution by putting empet’s suggestions together with Beasty’s, and I’m getting the same problem (no variation in the y-direction):

```julia
using Plots
nPix = 50
xData = [(100.0*(i/50)) for i in 1:nPix, j in 1:nPix]
yData = [(50.0*(j/27)) for j in 1:nPix, j in 1:nPix]
zData = [(sin(xData[i]/5)*cos(yData[j]/3)) for i=1:nPix, j=1:nPix]

x2D = vec(xData)
y2D = vec(yData)
z2D = vec(zData)

scatter(x2D, y2D, marker_z = z2D, markershape = :square)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/9/e90a872a7894d8c292ff02e69e1ccb0dd7761931.png)

---

<div class="post-metadata">

### Author: ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)
#### Post date: [June 8, 2021, 6:09am UTC](https://discourse.julialang.org/t/contour-plots-with-varying-locations-for-x-axis/61686/13 "2021-06-08T06:09:40Z")

</div>

BTW, this is actually more of what I had in mind… but I still can’t get the proper sine/cosine variation in the y-direction, for some reason:

```julia
using Plots
nPix = 50
xData = [(100.0*(i/50))*(j^(1/4)) for i in 1:nPix, j in 1:nPix]
yData = [(50.0*(j/27))*(i^(1/4)) for i in 1:nPix, j in 1:nPix]
zData = [(sin(xData[i]/5)*cos(yData[j]/3)) for i=1:nPix, j=1:nPix]

x2D = vec(xData)
y2D = vec(yData)
z2D = vec(zData)

scatter(x2D, y2D, marker_z = z2D, markershape = :square)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/5/b59bd3cd80787f9b8aba8bba44a167f08ffb5349.jpeg)

---

<div class="post-metadata">

### Author: ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)
#### Post date: [June 8, 2021, 6:20am UTC](https://discourse.julialang.org/t/contour-plots-with-varying-locations-for-x-axis/61686/14 "2021-06-08T06:20:12Z")

</div>

Oops, found my mistake…

```julia

using Plots
nPix = 50
xData = [(100.0*(i/50)) for i in 1:nPix, j in 1:nPix]
yData = [(50.0*(j/27)) for i in 1:nPix, j in 1:nPix]
zData = [(sin(xData[i,j]/5)*cos(yData[i,j]/3)) for i=1:nPix, j=1:nPix]

x2D = vec(xData)
y2D = vec(yData)
z2D = vec(zData)

scatter(x2D, y2D, marker_z = z2D, markershape = :square)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/7/2752c77833af9887e5fb5ce0b05decb0bb7154e4.png)

---

<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: [June 8, 2021, 7:45am UTC](https://discourse.julialang.org/t/contour-plots-with-varying-locations-for-x-axis/61686/15 "2021-06-08T07:45:18Z")

</div>

Even if it was generated by a mistake, the previous one looks great.
