Black Lives Matter. Please consider donating to Black Girls Code today.

Wired behavior of contour plot

It looks like when the points are not given in a “square manner”, the 2d contour plot looks strange.
When 6 points (0,0) (1,0) (2,0) (0,1) (1,1) (2,1) are given, the plot looks perfect.


However, when the 6 points are (0,0) (1,0.001) (2,0) (0,1.001) (1,1) (2,2), the plot look much too different than the previous one.

Is it using linear interpolation to generate the contour plot?
Where does Plotly handle the plot contour algorithm?
I would appreciate any help you offer.
Thank you.

Here is the code I used:
3

@xutianqish To plot a contour with Plotly you have 2 options:

  1. either you give only the z values as a list of lists or a numpy array of shape (m,n);
  2. or you provide x, y as lists, numpy arrays, etc, and z as in the case 1

For details and examples see: https://plot.ly/python/reference/#contour and https://plot.ly/python/contour-plots/

In your example x, y and z are intended to be simple lists, but they aren’t because nodes is a list of lists, NOT a numpy array, and you cannot slice it in this way because it leads to the following error:

----> 1 nodes[:,0]

TypeError: list indices must be integers, not tuple
1 Like

Hi empet. Thank you for your detailed reply and references. However, the problem still exists after I converted the data type.
Basically, I’m trying to do the visualization of FEM analysis but the plot looks strange when the mesh is not grid-based. (e.g. triangular mesh or some random quadrangle mesh) In the txt file I have nodeID, X, Y, and value of the nodes.



I suspect that plotly doesn’t handle the data in the way I want, that’s why I would like to have a look into the algorithm.

The first image is what I get when the mesh type is not a perfect rectangle.
The second image is what I get when the mesh type is perfect rectangle, which matches the result of a commercial product.

Plotly works with rectangular meshes. The contour points are computed via marching squares algorithm, and they are interpolated by the centripetal Catmull-Rom method.

1 Like

Thanks a lot empet! I will look into plotly library or try some other plotting libraries instead.
Cheers,
Xutianqish