close
The Wayback Machine - https://web.archive.org/web/20200905184835/https://github.com/JuliaPlots/Makie.jl/issues/551
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

[DOC] Adding custom labels to axis ticks, and displaying only specific ticks. #551

Open
Datseris opened this issue Jan 16, 2019 · 8 comments
Open

Comments

@Datseris
Copy link

@Datseris Datseris commented Jan 16, 2019

I have a plot. This plot has axis. These axis have ticks and the ticks have labels.

I have a label "A" and "B" and what I want is to make my axis instead of having numerous ticks and labels to have only ticks at the start and end. And the ticks to be "A" and "B" instead of 0 and 1 or 0.1552151 and 0.1556660. Or any custom string instead of A.

Can I do this, and if yes how?


EDIT: Solved, now needs to be documented.

For specific ticks, e.g.

limits = FRect3D((-5.0,-5.0,-5.0),(10.0,10.0,10.0))
w = wireframe(circle, limits=limits)
w[Axis][:ticks][:ranges] = (-5:5, -5:5, -5:5)

and for specific tick labels, e.g.

limits = FRect2D((-5.0,-5.0),(10.0,10.0))
w = scatter(rand(10), rand(10), limits=limits)
display(w)
w[Axis][:ticks][:ranges] = ([-5,5], [-5,5])
w[Axis][:ticks][:labels] = (["A", "B"], ["A", "B"])

(notice that ticks and labels must have same length)

you can also use observables, e.g.

xlab = Observable(["A", "B"])
on(xlab) do xlab
    w[Axis][:ticks][:labels] = (xlab, xlab)
end
@Datseris
Copy link
Author

@Datseris Datseris commented Jan 16, 2019

In pyplot this is simply ax.set_xticklabels([tick positions], [tick labels (strings)]).

@SimonDanisch
Copy link
Member

@SimonDanisch SimonDanisch commented Jan 17, 2019

scene = plot(..., ticks = (ranges = ([0, 1], [0, 1]), labels = (["a", "b"], ["a", "b"])))

should be possible to pass a node to ranges/labels a la ranges = Node(([0, 1], [0, 1]))

@Datseris
Copy link
Author

@Datseris Datseris commented Jan 17, 2019

Your suggestion doesn't work even without Nodes. I don't seem to see any tick labels updated:

scene = scatter(rand(10), rand(10), ticks = (ranges = ([0, 1], [0, 1]), labels = (["a", "b"], ["a", "b"])))
@Datseris
Copy link
Author

@Datseris Datseris commented Jan 17, 2019

Here is the code that does what I want:

limits = FRect2D((-5.0,-5.0),(10.0,10.0))
w = scatter(rand(10), rand(10), limits=limits)
display(w)
w[Axis][:ticks][:ranges] = ([-5,5], [-5,5])
w[Axis][:ticks][:labels] = (["A", "B"], ["A", "B"])

But unfortunately doesn't work with observables at the moment. That would be cool though!
E.,g.:

xlab = Observable(["A", "B"])
w[Axis][:ticks][:labels] = (xlab, xlab)

gives

MethodError: Cannot `convert` an object of type Observable{Array{String,1}} to an object of type Array{String,1}
Closest candidates are:
  convert(::Type{Array{String,1}}, !Matched::LibGit2.StrArrayStruct) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.0\LibGit2\src\strarray.jl:14
  convert(::Type{Array{T,N}}, !Matched::StaticArrays.SizedArray{S,T,N,M} where M) where {T, S, N} at C:\Users\datseris\.julia\packages\StaticArrays\mcf7t\src\SizedArray.jl:80
  convert(::Type{Array{T,N}}, !Matched::AxisArrays.AxisArray{T,N,D,Ax} where Ax where D) where {T, N} at C:\Users\datseris\.julia\packages\AxisArrays\G6pZY\src\core.jl:295
  ...
@SimonDanisch
Copy link
Member

@SimonDanisch SimonDanisch commented Jan 17, 2019

Yeah, that's not how you'd use observables... You need to pass the observables to the plotting function...
If you want to use your way, you'd need to do:

xlab = Observable(["A", "B"])
on(xlab) do xlab
    w[Axis][:ticks][:labels] = (xlab, xlab)
end
@Datseris Datseris changed the title Adding custom labels to axis ticks, and displaying only specific ticks. [DOC] Adding custom labels to axis ticks, and displaying only specific ticks. Jan 17, 2019
@Datseris
Copy link
Author

@Datseris Datseris commented Jan 17, 2019

Alright, this is satisfactory for me. Could you please put a "documentation" label on this one? Seems useful enough to be in the docs.

@SimonDanisch
Copy link
Member

@SimonDanisch SimonDanisch commented Jan 17, 2019

By the way this is in my opinion the preferred option:

using Makie

ranges = Node(([0, 1], [0, 1]))
labels = Node((["a", "b"], ["a", "b"]))
function to_limits(lims)
    xy = (lims[1][1], lims[2][1])
    xymax = (lims[1][2], lims[2][2])
    FRect(xy, xymax .- xy)
end
w = scatter(
    rand(10), rand(10),
    axis = (
        ticks = (
            ranges = ranges,
            labels = labels
        ),
    ),
    limits = lift(to_limits, ranges) # really unfortunate we 
)

TODO: automatically calculate limits from the ranges instead of the data + have limits work with the ranges format in general!

@asinghvi17
Copy link
Member

@asinghvi17 asinghvi17 commented May 10, 2019

To set alignment:

w[Axis][:ticks][:align] = ((:center, :top), (:right, :center))
@jkrumbiegel jkrumbiegel transferred this issue from JuliaPlots/AbstractPlotting.jl Apr 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.