# Plots.jl - Strings as xticks

**URL:** <https://discourse.julialang.org/t/plots-jl-strings-as-xticks/20745>\
**Category:** Visualization\
**Tags:** plots\
**Created:** [February 13, 2019, 2:31pm UTC](https://discourse.julialang.org/t/plots-jl-strings-as-xticks/20745 "2019-02-13T14:31:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mariok90](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariok90/32/7076_2.png) [@mariok90](https://discourse.julialang.org/u/mariok90)\
**Post date:** [February 13, 2019, 2:31pm UTC](https://discourse.julialang.org/t/plots-jl-strings-as-xticks/20745/1 "2019-02-13T14:31:46Z")

</div>

I want to replace the xticks with strings. How can I do that? When I run :

```julia
using Plots

xticks = ["$i" for i in 1:10]
plot(rand(10), xticks=xticks)

```

I get an error:

```julia
Julia Client – Internal Error
MethodError: no method matching isless(::Float64, ::String)
Closest candidates are:
  isless(::Float64, !Matched::Float64) at float.jl:459
  isless(!Matched::Missing, ::Any) at missing.jl:70
  isless(::AbstractFloat, !Matched::AbstractFloat) at operators.jl:148
  ...
<(::Float64, ::String) at operators.jl:260
<=(::Float64, ::String) at operators.jl:309
(::getfield(Plots, Symbol("##110#111")){Float64,Float64})(::String) at axes.jl:207
mapfilter(::getfield(Plots, Symbol("##110#111")){Float64,Float64}, ::typeof(push!), ::Array{String,1}, ::Array{String,1}) at abstractset.jl:340
filter(::Function, ::Array{String,1}) at array.jl:2351
optimal_ticks_and_labels(::Plots.Axis, ::Array{String,1}) at axes.jl:207
get_ticks(::Plots.Axis) at axes.jl:272
axis_drawing_info(::Plots.Subplot{Plots.GRBackend}) at axes.jl:591
_update_min_padding!(::Plots.Subplot{Plots.GRBackend}) at gr.jl:584
iterate at generator.jl:47 [inlined]
_collect(::Array{AbstractLayout,2}, ::Base.Generator{Array{AbstractLayout,2},typeof(Plots._update_min_padding!)}, ::Base.EltypeUnknown, ::Base.HasShape{2}) at array.jl:619
collect_similar at array.jl:548 [inlined]
map at abstractarray.jl:2018 [inlined]

```

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [February 13, 2019, 3:11pm UTC](https://discourse.julialang.org/t/plots-jl-strings-as-xticks/20745/2 "2019-02-13T15:11:17Z")

</div>

Try `xticks = (1:10, xticks)`. Not at a computer to confirm, but I’m 80% sure that’s it. It does live in the docs somewhere, I’ve gone looking for it on numerous occasions.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 13, 2019, 3:18pm UTC](https://discourse.julialang.org/t/plots-jl-strings-as-xticks/20745/3 "2019-02-13T15:18:43Z")

</div>

This is correct - `plot(rand(10), xticks = (1:10, string.(1:10))` should do what your example was trying to do.

---

<div class="post-metadata">

**Author:** ![mariok90](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariok90/32/7076_2.png) [@mariok90](https://discourse.julialang.org/u/mariok90)\
**Post date:** [February 13, 2019, 3:23pm UTC](https://discourse.julialang.org/t/plots-jl-strings-as-xticks/20745/4 "2019-02-13T15:23:35Z")

</div>

Thanks that works!

---

<div class="post-metadata">

**Author:** ![Marc.Cox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marc.cox/32/7514_2.png) [@Marc.Cox](https://discourse.julialang.org/u/Marc.Cox)\
**Post date:** [March 8, 2020, 6:41pm UTC](https://discourse.julialang.org/t/plots-jl-strings-as-xticks/20745/5 "2020-03-08T18:41:09Z")

</div>

Thx for the tips guys, very happy to find an answer and wanted to share the automation tip to show fewer digits automatically in ticks / xticks … maybe this should become a Plots Recipe macro candidate or such so everyone doesn’t have to recreate the wheel over and over ?

```julia
**# Programming is Automation and this WORKS but ONLY MANUALLY -- xticks = ([-2π:π:2π;], ["-2\\pi", "-\\pi", "0", "+\\pi", "+2\\pi"]),**
**# NOGO SubString(string.(-2π:π:2π)[1:3], 1:3) ),**
**# NOGO no method matching SubString(::Array{String,1}, ::UnitRange{Int64})**
 **#** NOTE Plot Graphics TIP Below WORKS to AUTOMATICALLY show 3 Digits of Precision in xticks ****
**xticks = ( [-2π:π:2π;], parse.(Float16,string.(-2π:π:2π)[:]) ),**

```

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [May 27, 2022, 8:45pm UTC](https://discourse.julialang.org/t/plots-jl-strings-as-xticks/20745/6 "2022-05-27T20:45:59Z")

</div>

I think the canonical way to do that is to use `round`

```julia
julia> xticks = ( [-2π:π:2π;], string.(round.(-2π:π:2π, digits=2)))
([-6.283185307179586, -3.141592653589793, 0.0, 3.141592653589793, 6.283185307179586], ["-6.28", "-3.14", "0.0", "3.14", "6.28"])

```
