JuliaPlots / Plots.jl

Powerful convenience for Julia visualizations and data analysis
https://docs.juliaplots.org
Other
1.84k stars 355 forks source link

[BUG] ticks = {Matrix} torpedos pyplot painting #4196

Open bc0n opened 2 years ago

bc0n commented 2 years ago

Details

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. vscode>Jupyter fails silently while repl gives an error. This can be worked around by xt = vec([1 3 7 10]). GR() is happy with a 1D matrix for ticks.

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: ...

It's good to have any error, but a type error would seem more correct. Any thoughts on why Jupyter doesn't pass on the error?

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

Backends

This bug occurs on ( insert x below )

Backend yes no untested
gr (default) x
pyplot x
plotlyjs (ignores ticks)
pgfplotsx x
unicodeplots x
inspectdr x
gaston x

Versions

Plots.jl version: 1.28.1 Backend version (]st -m <backend(s)>): PyPlot v2.10.0 Output of versioninfo():

Julia Version 1.7.2
Commit bf53498635 (2022-02-06 15:21 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: AMD Ryzen 5 1600X Six-Core Processor
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, znver1)
Environment:
  JULIA_PKG_USE_CLI_GIT = true
t-bltg commented 2 years ago

Xref https://github.com/JuliaPlots/Plots.jl/issues/4157, maybe.

can types be added to the documentation display

I think that's a feature to ask for in Documenter (@autodocs).

rec.src = "api.html"
rec.fragment = "#Plots.xticks!-Tuple{Union{Symbol, Tuple{AbstractVector{T}, AbstractVector{S}}, AbstractVector{T}} where {T<:Real, S<:AbstractString}}"
rec.category = "method"
rec.title = "Plots.xticks!"
rec.page_title = "API"