Skip to content

ticks = {Matrix} scuttles plot drawing #550

@bc0n

Description

@bc0n

Under Julia 1.7.2, Plots v1.28.1, PyPlot v2.10.0, assigning a 1D {Matrix} to x/yticks prevents the plot from displaying. Jupyter fails silently while repl gives an error. This is frustrating because other parts of Julia encourage comma-less vector initialization. This can be worked around by xt = vec([1 3 7 10]).

using Plots
pyplot()
x = 1:1:10
y = 2 .*x
xt = [1, 3, 7, 10]
@show typeof(xt)
p1 = plot(x,y, xticks=(xt,xt), title="typeof(xt)=$(typeof(xt))")

xt = [1 3 7 10]
@show typeof(xt)
p2 = plot(x,y, xticks=(xt,xt), title="typeof(xt)=$(typeof(xt))")

plot(p1,p2)

No type error or warnings are given in vsCode.Jupyter.

Executing in the julia repl gives:

julia> using Plots

julia> pyplot()
Plots.PyPlotBackend()

julia> x = 1:1:10
1:1:10

julia> y = 2 .*x
2:2:20

julia> xt = [1, 3, 7, 10]
4-element Vector{Int64}:
  1
  3
  7
 10

julia> @show typeof(xt)
typeof(xt) = Vector{Int64}
Vector{Int64} (alias for Array{Int64, 1})

julia> p1 = plot(x,y, xticks=(xt,xt), title="typeof(xt)=$(typeof(xt))")

julia> xt = [1 3 7 10]
1×4 Matrix{Int64}:
 1  3  7  10

julia> @show typeof(xt)
typeof(xt) = Matrix{Int64}
Matrix{Int64} (alias for Array{Int64, 2})

julia> p2 = plot(x,y, xticks=(xt,xt), title="typeof(xt)=$(typeof(xt))")
Error showing value of type Plots.Plot{Plots.PyPlotBackend}:
ERROR: Invalid input for xticks: ([1 3 7 10], [1 3 7 10])
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:33
  [2] py_set_ticks(sp::Plots.Subplot{Plots.PyPlotBackend}, ax::PyCall.PyObject, ticks::Tuple{Matrix{Int64}, Matrix{Int64}}, letter::Symbol)
    @ Plots ~/.julia/packages/Plots/PGQ1Y/src/backends/pyplot.jl:925
  [3] _before_layout_calcs(plt::Plots.Plot{Plots.PyPlotBackend})
    @ Plots ~/.julia/packages/Plots/PGQ1Y/src/backends/pyplot.jl:1321
  [4] prepare_output(plt::Plots.Plot{Plots.PyPlotBackend})
    @ Plots ~/.julia/packages/Plots/PGQ1Y/src/plot.jl:217
  [5] display(#unused#::Plots.PlotsDisplay, plt::Plots.Plot{Plots.PyPlotBackend})
    @ Plots ~/.julia/packages/Plots/PGQ1Y/src/output.jl:145
  [6] display(x::Any)
    @ Base.Multimedia ./multimedia.jl:328
  [7] #invokelatest#2
    @ ./essentials.jl:716 [inlined]
  [8] invokelatest
    @ ./essentials.jl:714 [inlined]
  [9] print_response(errio::IO, response::Any, show_value::Bool, have_color::Bool, specialdisplay::Union{Nothing, AbstractDisplay})
    @ REPL ~/clones/julia-1.7.2/share/julia/stdlib/v1.7/REPL/src/REPL.jl:293
 [10] (::REPL.var"#45#46"{REPL.LineEditREPL, Pair{Any, Bool}, Bool, Bool})(io::Any)
    @ REPL ~/clones/julia-1.7.2/share/julia/stdlib/v1.7/REPL/src/REPL.jl:277
 [11] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
    @ REPL ~/clones/julia-1.7.2/share/julia/stdlib/v1.7/REPL/src/REPL.jl:510
 [12] print_response(repl::REPL.AbstractREPL, response::Any, show_value::Bool, have_color::Bool)
    @ REPL ~/clones/julia-1.7.2/share/julia/stdlib/v1.7/REPL/src/REPL.jl:275
 [13] (::REPL.var"#do_respond#66"{Bool, Bool, REPL.var"#77#87"{REPL.LineEditREPL, REPL.REPLHistoryProvider}, REPL.LineEditREPL, REPL.LineEdit.Prompt})(s::REPL.LineEdit.MIState, buf::Any, ok::Bool)
    @ REPL ~/clones/julia-1.7.2/share/julia/stdlib/v1.7/REPL/src/REPL.jl:846
 [14] (::REPL.var"#82#92"{Regex, Int64, Int64, Int64, REPL.LineEdit.Prompt, REPL.LineEdit.Prompt, REPL.LineEdit.Prompt})(::REPL.LineEdit.MIState, ::Any, ::Vararg{Any})
    @ REPL ~/clones/julia-1.7.2/share/julia/stdlib/v1.7/REPL/src/REPL.jl:1174
 [15] #invokelatest#2
    @ ./essentials.jl:716 [inlined]
 [16] invokelatest
    @ ./essentials.jl:714 [inlined]
 [17] (::REPL.LineEdit.var"#25#26"{REPL.var"#82#92"{Regex, Int64, Int64, Int64, REPL.LineEdit.Prompt, REPL.LineEdit.Prompt, REPL.LineEdit.Prompt}, String})(s::Any, p::Any)
    @ REPL.LineEdit ~/clones/julia-1.7.2/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:1466
 [18] prompt!(term::REPL.Terminals.TextTerminal, prompt::REPL.LineEdit.ModalInterface, s::REPL.LineEdit.MIState)
    @ REPL.LineEdit ~/clones/julia-1.7.2/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:2586
 [19] run_interface(terminal::REPL.Terminals.TextTerminal, m::REPL.LineEdit.ModalInterface, s::REPL.LineEdit.MIState)
    @ REPL.LineEdit ~/clones/julia-1.7.2/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:2488
 [20] run_frontend(repl::REPL.LineEditREPL, backend::REPL.REPLBackendRef)
    @ REPL ~/clones/julia-1.7.2/share/julia/stdlib/v1.7/REPL/src/REPL.jl:1232
 [21] (::REPL.var"#49#54"{REPL.LineEditREPL, REPL.REPLBackendRef})()
    @ REPL ./task.jl:423

It's good to have an error, but a type error would be more correct to throw. Why isn't this error passed through to Jupyter?

More generally, can types be added to the documentation display? There's more information in the doc url than in the page
https://docs.juliaplots.org/latest/api/#Plots.xticks!-Tuple{Union{Symbol,%20Tuple{AbstractVector{T},%20AbstractVector{S}},%20AbstractVector{T}}%20where%20{T%3C:Real,%20S%3C:AbstractString}}

image

image

#2337 may be relevant & blocking.

Many thanks, Ben

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions