# Formatting the x-axis with hex values

**URL:** <https://discourse.julialang.org/t/formatting-the-x-axis-with-hex-values/83528>\
**Category:** General Usage\
**Tags:** question, plotting\
**Created:** [June 29, 2022, 5:20pm UTC](https://discourse.julialang.org/t/formatting-the-x-axis-with-hex-values/83528 "2022-06-29T17:20:01Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [June 29, 2022, 5:20pm UTC](https://discourse.julialang.org/t/formatting-the-x-axis-with-hex-values/83528/1 "2022-06-29T17:20:02Z")

</div>

I have the following code:

```julia
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?

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [June 29, 2022, 5:50pm UTC](https://discourse.julialang.org/t/formatting-the-x-axis-with-hex-values/83528/2 "2022-06-29T17:50:03Z")

</div>

```julia
julia> tickpos = xticks(p)[1][1];

julia> ticknames = string.(Int.(tickpos), base=16);

julia> xticks!(tickpos, ticknames)

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [June 29, 2022, 5:50pm UTC](https://discourse.julialang.org/t/formatting-the-x-axis-with-hex-values/83528/3 "2022-06-29T17:50:21Z")

</div>

A workaround:

```julia
xt = round.(Int, xticks(p)[1][1])
xticks!(xt, string.(xt, base = 16))

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [June 29, 2022, 7:19pm UTC](https://discourse.julialang.org/t/formatting-the-x-axis-with-hex-values/83528/4 "2022-06-29T19:19:38Z")

</div>

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’ ?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [June 29, 2022, 7:22pm UTC](https://discourse.julialang.org/t/formatting-the-x-axis-with-hex-values/83528/5 "2022-06-29T19:22:40Z")

</div>

b) `"0x" .* string.(...)`

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [June 29, 2022, 7:31pm UTC](https://discourse.julialang.org/t/formatting-the-x-axis-with-hex-values/83528/6 "2022-06-29T19:31:16Z")

</div>

Well, that does not convert ‘a’ in ‘0x0a’…

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [June 29, 2022, 7:32pm UTC](https://discourse.julialang.org/t/formatting-the-x-axis-with-hex-values/83528/7 "2022-06-29T19:32:02Z")

</div>

```julia
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?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [June 29, 2022, 7:33pm UTC](https://discourse.julialang.org/t/formatting-the-x-axis-with-hex-values/83528/8 "2022-06-29T19:33:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [June 29, 2022, 7:34pm UTC](https://discourse.julialang.org/t/formatting-the-x-axis-with-hex-values/83528/9 "2022-06-29T19:34:47Z")

</div>

I see. Try the `pad=` kwarg to string. [Strings · The Julia Language](https://docs.julialang.org/en/v1/base/strings/#Base.string)

```julia
julia> a=[10,11]
2-element Vector{Int64}:
 10
 11

julia> "0x" .* string.(a,base=16,pad=2)
2-element Vector{String}:
 "0x0a"
 "0x0b"

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [June 29, 2022, 7:36pm UTC](https://discourse.julialang.org/t/formatting-the-x-axis-with-hex-values/83528/10 "2022-06-29T19:36:50Z")

</div>

Great! I learned something new… 🙂

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [June 29, 2022, 7:41pm UTC](https://discourse.julialang.org/t/formatting-the-x-axis-with-hex-values/83528/11 "2022-06-29T19:41:30Z")

</div>

```julia
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…

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [June 29, 2022, 8:06pm UTC](https://discourse.julialang.org/t/formatting-the-x-axis-with-hex-values/83528/12 "2022-06-29T20:06:20Z")

</div>

To also handle negative numbers:

```julia
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))

```

 ![Plots_hexadecimal_xticks](https://global.discourse-cdn.com/julialang/original/3X/a/a/aab413075a3a53cec15547898bd6ce078806b20c.png)

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [June 29, 2022, 8:24pm UTC](https://discourse.julialang.org/t/formatting-the-x-axis-with-hex-values/83528/13 "2022-06-29T20:24:13Z")

</div>

You managed to do it in 3 lines! Great! 😀

I would have written a separate function.
