Ian Marshall
Ian Marshall

Reputation: 739

How to change labels on the x axis of a bar plot from numbers to text?

I'm trying to make a bar plot using Plots.jl and the GR backend and wanted to ask how to make the x axis display text labels rather than numbers. Basically this is what I'm doing:

using Plots; gr()
data = [1,2,3]
labels = ["one","two","three"]

bar(data, legend=false)

This produces the following plot:

example bar plot

How do I display my labels ("one", "two", "three"), instead of "1 2 3" on the x axis?

Thanks!

Upvotes: 3

Views: 2148

Answers (2)

Shayan
Shayan

Reputation: 6295

In the Plots version v1.38.0, one can do it by specifying the labels in the optional keyword argument xticks:

data = [1,2,3]
labels = ["one","two","three"]

bar(
  data,
  legend=false,
  xticks=(1:length(data), labels)
)

enter image description here

Upvotes: 0

Ian Marshall
Ian Marshall

Reputation: 739

The answer (thanks Tom!) is to pass the labels as x values (currently only possible on the dev branch):

Pkg.checkout("Plots","dev")
using Plots
gr()

data = [1,2,3]
labels = ["one","two","three"]

bar(labels, data, legend=false)

Upvotes: 3

Related Questions