JuliaPlots / PlotlyJS.jl

Julia library for plotting with plotly.js
Other
418 stars 77 forks source link

`make_subplots` raises error on 2x1 plots with `specs` #430

Open Mo-Gul opened 2 years ago

Mo-Gul commented 2 years ago

When creating a 2x1 make_subplots with specs like

p = make_subplots(
    rows=2, cols=1,
    shared_xaxes=true,
    specs=[
        Spec()
        Spec()
    ]
)

the error stack

ERROR: LoadError: MethodError: no method matching (Matrix{<:Union{Missing, Spec}})(::Vector{Spec})
Stacktrace:
 [1] convert(#unused#::Type{Matrix{<:Union{Missing, Spec}}}, a::Vector{Spec})
   @ Base .\array.jl:554
 [2] Subplots(rows::Int64, cols::Int64, shared_xaxes::Bool, shared_yaxes::Bool, start_cell::String, subplot_titles::Missing, column_widths::Missing, row_heights::Missing, specs::Vector{Spec}, insets::Missing, column_titles::Missing, row_titles::Missing, x_title::Missing, y_title::Missing, grid_ref::Matrix{Vector{PlotlyBase.SubplotRef}}, has_secondary_y::Bool, horizontal_spacing::Float64, vertical_spacing::Float64, max_width::Float64, _widths::Vector{Float64}, _heights::Vector{Float64})
   @ PlotlyBase <path_to_julia_installation>\.julia\packages\Parameters\MK0O4\src\Parameters.jl:505
 [3] Subplots(; rows::Int64, cols::Int64, shared_xaxes::Bool, shared_yaxes::Bool, start_cell::String, subplot_titles::Missing, column_widths::Missing, row_heights::Missing, specs::Vector{Spec}, insets::Missing, column_titles::Missing, row_titles::Missing, x_title::Missing, y_title::Missing, grid_ref::Matrix{Vector{PlotlyBase.SubplotRef}}, has_secondary_y::Bool, horizontal_spacing::Float64, vertical_spacing::Float64, max_width::Float64, _widths::Vector{Float64}, _heights::Vector{Float64})
   @ PlotlyBase <path_to_julia_installation>\.julia\packages\Parameters\MK0O4\src\Parameters.jl:493
 [4] make_subplots(; kwargs::Base.Pairs{Symbol, Any, NTuple{4, Symbol}, NamedTuple{(:rows, :cols, :shared_xaxes, :specs), Tuple{Int64, Int64, Bool, Vector{Spec}}}})
   @ PlotlyJS <path_to_julia_installation>\.julia\packages\PlotlyJS\4jzLr\src\PlotlyJS.jl:40
 [5] top-level scope
   @ <path_to_julia_file>\test.jl:76
in expression starting at <path_to_julia_file>\test.jl:76

The error doesn't raise when

Version info

The output of versioninfo() is

Julia Version 1.7.1
Commit ac5cc99908 (2021-12-22 19:35 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, tigerlake)
Environment:
  JULIA_EDITOR = code.cmd
  JULIA_NUM_THREADS =

and the output of using Pkg; pkg"status" is

      Status `<path_to_julia_file/environment>\Project.toml`
  [a93c6f00] DataFrames v1.3.1
  [f0f68f2c] PlotlyJS v0.18.8
  [fdbf4ff8] XLSX v0.7.8
  [10745b16] Statistics
empet commented 2 years ago

specs defined like this:

specs =  [
        Spec(kind="xy")
        Spec(kind="scene")]

is a 2-element Vector{Spec}, but specs must be a Matrix: https://github.com/sglyon/PlotlyBase.jl/blob/master/src/subplot_utils.jl#L155; Hence you should reshape it :

fig = make_subplots(rows=2, cols=1, shared_xaxes=true,
                    specs=reshape([Spec()
                                   Spec()], 2, 1) )
relayout!(fig, width=500, height=500)
add_trace!(fig, scatter(x=1:3, y=rand(2:7,3)), row=1, col=1)
add_trace!(fig, scatter(x=1:3, y=-1 .+ 3*rand(3)), row=2, col=1)
display(fig)
Mo-Gul commented 2 years ago

@empet, thank you for the hint how to make it work. A minor improvement to your solution to use reshape(..., :, 1) (which is mentioned at https://stackoverflow.com/questions/34595122/the-best-way-to-convert-vector-into-matrix-in-julia#comment98245161_34599502), that is

fig = make_subplots(rows=2, cols=1, shared_xaxes=true,
                    specs=reshape([Spec()
                                   Spec()], :, 1) )

Hopefully you agree that this is a workaround rather a real solution. So hopefully the definition can adjusted to take care of that "special case".