LyxUser12345
LyxUser12345

Reputation: 411

How can I set the number of ticks in Julia using Pyplot?

I am struggling to 'translate' the instructions I find for Python to the use of Pyplot in Julia. This must be a simple question, but do you know how to set the number of ticks in a plot in Julia using Pyplot?

Upvotes: 4

Views: 4239

Answers (2)

LyxUser12345
LyxUser12345

Reputation: 411

After some trying and searching, I found a way to do it. One can just set the number of bins that should be on each axis. Here is a minimal example:

using PyPlot
x = linspace(0, 10, 200)
y = sin(x)
fig, ax = subplots()
ax[:plot](x, y, "r-", linewidth=2, label="sine function", alpha=0.6)
ax[:legend](loc="upper center")
ax[:locator_params](axis ="y", nbins=4)

The last line specifies the number of bins that should be used on the y-axis. Leaving the argument axis unspecified will set that option for both axis at the same value.

Upvotes: 2

isebarn
isebarn

Reputation: 3940

If you have

x = [1,2,3,4,5]
y = [1,3,6,8,11]

you can

PyPlot.plot(x,y)

which draws the plot

and then do

PyPlot.xticks([1,3,5])

for tics at 1,3 and 5 on the x-axis

PyPlot.yticks([1,6,11])

for tics at 1,6 and 11 on the y-axis

Tic spacing

if you want fx 4 tics and want it evenly spaced and dont mind Floats, you can do

collect(linspace(x[1], x[end], 4). 

If you need the tics to be integers and you want 4 tics, you can do

collect(x[1]:div(x[end],4):x[end]) 

Edit

Maybe this wont belong here but atleast you'll see it...

whenever you're looking for a method that's supposed to be in a module X you can find these methods by typing in the REPL X. + TAB key

to clarify, if you want to search a module for a method you suspect starts with an x, like xticts, in the REPL (terminal/shell) do

PyPlot.x

and press TAB twice and you'll see

julia> PyPlot.x
xkcd   xlabel  xlim    xscale  xticks

and if you're not sure exactly how the method works, fx its arguments, and there isnt any help available, you can call

methods(PyPlot.xticks)

to see every "version" that method has

Bonus

The module for all the standard methods, like maximum, vcat etc is Base

Upvotes: 6

Related Questions