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

Issues with traces types that accepts matrices as inputs #12

Closed
disberd opened this issue Mar 17, 2022 · 9 comments
Closed

Issues with traces types that accepts matrices as inputs #12

disberd opened this issue Mar 17, 2022 · 9 comments

Comments

@disberd
Copy link

disberd commented Mar 17, 2022

Some plotly trace types accepts input data in matrix format and the behavior of the trace changes whether the data is specified as a matrix or as a play 1D vector.

The issue comes from the serialization of JSON3 that by defaults serializes any ND-Array as its 1D content.

This can create issues that are different depending on the specific trace affected. I could identify the following problems in these traces, with some details in the examples below:

  • heatmap
  • surface
  • contour

Heatmap Example

The heatmap trace can accept the z values as a matrix, in which case the x and y values can be provided as the vectors generating the underlying grid.
If z is provided as a vector, x and y also has to be provided as 1D vectors containing the x/y values of each single point in z.
This creates an issue when trying to provide z as matrix and x,y as generating vectors in PlolyLight:

image

Surface Example

A different problem happens in surface still when trying to provide x and y as generating vectors, where no Plot is produced with PlotlyLight:

image

Contour Example

Finally, something very weird that I don't understand happens with contour. The plot is rendered but the resulting contour is not the correct one:

image

@disberd
Copy link
Author

disberd commented Mar 17, 2022

Some relevant issues I just found:
plotly/Dash.jl#161
quinnj/JSON3.jl#196

@joshday
Copy link
Member

joshday commented Mar 17, 2022

Thanks for the issue and the links!

An ugly workaround is collect(eachrow(z)) (or maybe it's eachcol?).

I'd prefer that PlotlyLight didn't need to do special processing on its inputs, but that may be unavoidable.

@disberd
Copy link
Author

disberd commented Mar 17, 2022

Yeah, it would be best if arrays were serialized like that directly in JSON3, but I think until then some pre-processing of inputs inside the package is the best way to provide a reliable experience with PlotlyLight

I believe it should be eachcol

@disberd
Copy link
Author

disberd commented Mar 18, 2022

Another option apart from what discussed in JuliaComputing/EasyConfig.jl#6 could be to have a processing function here before writing the Plot object to JSON like in PlotlyBase: https://github.com/sglyon/PlotlyBase.jl/blob/2a5c8b87679ced5af7ac3894950ef0ea845eaa61/src/json.jl#L6-L11

Though that is probably what you had in mind with the processor thing in JuliaComputing/EasyConfig.jl#6

@joshday
Copy link
Member

joshday commented Mar 18, 2022

I've added a utility function (looking at the examples in https://plotly.com/javascript/heatmaps/, it seems rows is correct):

collectrows(x::AbstractMatrix) = collect.(eachrow(x))

and docs in the README as to why this is necessary.

I think I'm ready to close this issue, as this is just an unfortunate consequence of JSON not having multidimensional arrays. If additional edge cases crop up, we can go the processing function route.

@disberd
Copy link
Author

disberd commented Mar 18, 2022

Yeah eachrow seems indeed to be the correct one. I am also closing this as you mentioned

@disberd disberd closed this as completed Mar 18, 2022
@StreetLevel
Copy link

Hello there,

you could add the workaround for PlotlyJS.table, as well. Same issue with Matrices appears here, too.

Greetz Max

@StreetLevel
Copy link

Funnily, for me

collectcols(x::AbstractMatrix) = collect.(eachcol(x))

works for tables...but better doublecheck...

@joshday
Copy link
Member

joshday commented Sep 21, 2022

Yeah, looks like it works out of the box as long as the table implements eachcol!

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

3 participants