I have the following code:
using Plots
p = histogram(randn(1000)*10, bins=:scott, weights=repeat(1:5, outer=200))
p
How can I achieve that the x axis shows hex values instead of decimal values?
I have the following code:
using Plots
p = histogram(randn(1000)*10, bins=:scott, weights=repeat(1:5, outer=200))
p
How can I achieve that the x axis shows hex values instead of decimal values?
julia> tickpos = xticks(p)[1][1];
julia> ticknames = string.(Int.(tickpos), base=16);
julia> xticks!(tickpos, ticknames)
A workaround:
xt = round.(Int, xticks(p)[1][1])
xticks!(xt, string.(xt, base = 16))
This nearly does the job!
Questions:
a. what is the meaning of xticks(p)[1][1] ? Why the double index, and how is it possible that after applying a double index you get a vector?
b. the resulting values are ‘a’, ‘14’, ‘1e’ which is not wrong, but how can I get
‘0x0a’, ‘0x14’, ‘0x1e’ ?
b) "0x" .* string.(...)
Well, that does not convert ‘a’ in ‘0x0a’…
julia> a=["a","b"]
2-element Vector{String}:
"a"
"b"
julia> "0x" .* a
2-element Vector{String}:
"0xa"
"0xb"
Isn’t this what you want?
Exactly. And that is not what I want because I need a leading zero. 0x0a
I work with electrical engineers and they expect this numerical format for CAN bus IDs.
I see. Try the pad=
kwarg to string. Strings · The Julia Language
julia> a=[10,11]
2-element Vector{Int64}:
10
11
julia> "0x" .* string.(a,base=16,pad=2)
2-element Vector{String}:
"0x0a"
"0x0b"
Great! I learned something new…
using Plots
p = histogram(randn(1000)*10, bins=:scott, weights=repeat(1:5, outer=200))
xt = round.(Int, xticks(p)[1][1])
xticks!(xt, "0x" .* string.(xt,base=16,pad=2))
p
This works fine, but only for positive numbers…
In my real histogram I only need positive numbers…
To also handle negative numbers:
using Plots
p = histogram(randn(1000)*10, bins=:scott, weights=repeat(1:5, outer=200))
xt = round.(Int, xticks(p)[1][1])
xticks!(xt, @. ifelse(xt ≥ 0, "0x", "-0x") * string(abs(xt),base=16,pad=2))
You managed to do it in 3 lines! Great!
I would have written a separate function.