# Contour Line Interpolation

**URL:** <https://community.plotly.com/t/contour-line-interpolation/1404>\
**Category:** Plotly R\
**Created:** [June 28, 2016, 12:42pm UTC](https://community.plotly.com/t/contour-line-interpolation/1404 "2016-06-28T12:42:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kngavl](https://avatars.discourse-cdn.com/v4/letter/k/dfb087/32.png) [@kngavl](https://community.plotly.com/u/kngavl)\
**Post date:** [June 28, 2016, 12:42pm UTC](https://community.plotly.com/t/contour-line-interpolation/1404/1 "2016-06-28T12:42:11Z")

</div>

Hi there,

I was curious, by what interpolation method are contour lines calculated in Plotly?  
I can find no documentation nor reproduce the results; for instance of the following code:  
p \<- plot\_ly(srtest, x=X, y=Y, z=O,type =“contour”)

One other issue. I have a csv with X Y and Z values. Now when initially run the command above, generally the code runs and I’m shown a contour plot of the data. Now if I delete a single row from the X Y and Z data in the csv and save, import the csv into R, and run the exact same command as above, plotly returns a graph labeled from 0 to 6 on the x axis and 0 to -4 on the y axis with absolutely no points in it. No error message is observed.

If I add the data point and resave the plot works again, and I can even add a new row with additional xyz data and it’ll include it in the interpretation. Any ideas as to what would cause this behavior?

**Edit**  
_In regards to the second question about the plot working and then not working, it seems sometimes when the CSV is imported into R there will be a couple extra empty rows added after the data. Simply deleting them appears to solve the problem, as whatever the interpolation algorithm is, it doesn’t appear to enjoy having NA’s. Now, if anyone knows how it goes interpolating the contours …?_

---

<div class="post-metadata">

**Author:** ![etienne](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/etienne/32/17_2.png) [@etienne](https://community.plotly.com/u/etienne)\
**Post date:** [June 29, 2016, 4:05pm UTC](https://community.plotly.com/t/contour-line-interpolation/1404/2 "2016-06-29T16:05:19Z")

</div>

This looks like a bug to me.

Would you mind sharing a reproducible code snippet?

---

<div class="post-metadata">

**Author:** ![kngavl](https://avatars.discourse-cdn.com/v4/letter/k/dfb087/32.png) [@kngavl](https://community.plotly.com/u/kngavl)\
**Post date:** [June 30, 2016, 1:26pm UTC](https://community.plotly.com/t/contour-line-interpolation/1404/3 "2016-06-30T13:26:32Z")

</div>

Sometimes it happens, sometimes it doesn’t while using read.csv, with that said, you can avoid the problem entirely by running  
data.omitted \<-na.omit(data). That has cleared up that issue.

But do you happen to know what is the algorithm the contour inter/extrapolation is based on?  
So far with fitting trend surfaces, idw, kriging and akima I haven’t been able to replicate it.

---

<div class="post-metadata">

**Author:** ![etienne](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/etienne/32/17_2.png) [@etienne](https://community.plotly.com/u/etienne)\
**Post date:** [June 30, 2016, 2:16pm UTC](https://community.plotly.com/t/contour-line-interpolation/1404/4 "2016-06-30T14:16:49Z")

</div>

We’re using the [marching squares](http://en.wikipedia.org/wiki/Marching_squares) algorithm.

You can check our complete implementation [here](https://github.com/plotly/plotly.js/blob/master/src/traces/contour/plot.js).

---

<div class="post-metadata">

**Author:** ![kngavl](https://avatars.discourse-cdn.com/v4/letter/k/dfb087/32.png) [@kngavl](https://community.plotly.com/u/kngavl)\
**Post date:** [July 2, 2016, 8:03am UTC](https://community.plotly.com/t/contour-line-interpolation/1404/5 "2016-07-02T08:03:34Z")

</div>

Thank you Etienne, and thanks for Plotly!
