Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in examples of heatmap and contour. z-array should be transposed #355

Closed
empet opened this issue Oct 27, 2020 · 1 comment
Closed

Bug in examples of heatmap and contour. z-array should be transposed #355

empet opened this issue Oct 27, 2020 · 1 comment

Comments

@empet
Copy link

empet commented Oct 27, 2020

All examples of heatmaps and contour plots should be modified if their z-array is of size (m,n), eventually with m=n, because
the plots do not display the right information I discovered a bug within heatmap1(), heatmap2(), and contour(s).
To illustrate it I take an example of heatmap, but not the one in the function heatmap1(), because there the z-array exhibits some symmetry.
First the plot of a heatmap with plotly.py

import plotly.graph_objects as go
fig= go.Figure(go.Heatmap(z=[[1, 24, 30], [28, 18, 40], [ 36, 54, 7]]))
fig.update_layout(width=400, height=400)

heatmap_pl

Plot the same heatmap with PlotlyJS.jl:

  
using PlotlyJS
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"]] 
plot(heatmap(z=[1 24 30; 28 18 40; 36 54  7], colorscale=plasma))

heatmap_ju
The last image is not the right heatmap. But running with the transposed array:

plot(heatmap(z=[1 24 30; 28 18 40; 36 54  7]', colorscale=plasma))

we get the right one.
Update
Writing fig to json, from plotly.py:

import plotly.io as pio
fig.write_json("plpy.json", pretty=True)

fig.data.z is listed as follows:

      "z": [
        [
          1,
          24,
          30
        ],
        [
          28,
          18,
          40
        ],
        [
          36,
          54,
          7
        ]
      ]

while the same portion of json file created by plotlyJS.jl is different, because Julia has column major order:

 "z": [
        [
          1,
          28,
          36
        ],
        [
          24,
          18,
          54
        ],
        [
          30,
          40,
          7
        ]
      ]

Therefore plotly.js interprets the Julia array columns as rows, and this leads to a wrong heatmap, contour or surface plot.
end update
For both, heatmap and contour it is more obvious that the z-array must be transposed, when we are setting not only z, but also, x, and y of different lengths:

a, b = -1., 1.
c, d= 0., 1.
nx, ny= 200, 100
hx = (b-a)/(nx-1)
x = a:hx:b
hy = (d-c)/(ny-1)
y=c:hy:d
X = x' .* ones(length(y))
Y = ones(length(x))'.*y
Z= sin.(pi*pi*(X.*X+Y.*Y));
print(size(Z))
plot(contour(x=x, y=y, z=Z), Layout(width=650, height=350))

The corresponding plot:
contour_z
has the xaxis range different from [-1,1], and yaxis range is not [0,1]. Running the same code with z=Z' we get the right contour plot:
contour_zt

PlotlyJS.jl version 0.13.1

@empet
Copy link
Author

empet commented Apr 12, 2022

This issue has been fixed. By default, the json representation of the pl=Plot(heatmap(....)) contains the key-value: "transpose":true.

@empet empet closed this as completed Apr 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant