✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

Arbitrary orientation contour plots using 2D arrays to specify coordinates


Is it possible to generate contour plots where the X and Y values are specified in 2D arrays like those produced from meshgrid?

For example, this was done in Matplotlib:


Yes, we can rotate a Plotly heatmap or contour plot, via a scipy.ndimage transformation.
Here is an example: https://plot.ly/~empet/14044

Thanks very much empet. This is halfway towards what I’d like.

Sometimes I have contour data where the grid coordinates are not formed from an orthogonal basis. And so my grid might have an arbitrary orientation and additionally be skewed. This is easy to do in matplotlib since you can assign x and y to be 2D arrays using np.meshgrid() rather than 1D arrays/lists.

Is something like that possible?


If you have a 2d data set, given in an array of shape (2,N), and apply a rotation or other transformation to all its points, then the array of transformed points, tr_pts, with x, y=tr_pts, will generate a Histogram2dcontour that is included in the rectangle [xmin, xmax] x [ymin, ymax],

not in the rotated rectangle that included the initial points.

If we could collect the z values, that are displayed on hover over the plot of the Histogram2dcontour, then we would be able to apply a scipy.ndimage transformation to these z-values . But as far as I know, we cannot access the contour data computed from the given set of points.

I don’t think using a histogram really solves my problem, since I may have negative z values. My issue is really about being able to use np.meshgrid() to supply (x,y) coordinates to the contour plot - in this way any transformation can be applied to the data. As far as I can see, this is not possible in Plotly.

Thanks for your suggestions anyway.

Has anyone figured this out?
I have the same problem, trying to generate a contour plot where the X and Y values are specified in 2D arrays. Any help would be greatly appreciated.


Mean time Plotly introduced new axes attributes, that improved the appearance of linearly transformed heatmaps and contours.

To get such a contour replace in this notebook:


fig1 = ..., by:

fig1 = go.Figure(go.Contour(x=x[0], y=y[:, 0], z=z,

and fig2 = ....

fig2 = go.Figure(go.Contour(z=transformed_image,
                            hovertemplate='z: %{z:.3f%}<extra></extra>',