Plots.jl : Obtain a graph with alligned ticks on x and y axes

Hey,

Is there a way to force a plot to have the same ticks values on the x and the y axes ? So that the ‘grid’ is symmetric. It will make the reading of my qqplots easier.

Can you show some example? ratio=1.0 keyword makes unit length in x and y be the same but I am not sure about tick values shown in the plot.

I suspect that providing common lims should suffice:

using Plots; gr()
plot(sin, lims=(-3, 3))

Consider the following :

using Plots
X = randn(1000)
Y = randn(1000)
Y = X .* (1 + Y / 5)
scatter(X,Y)

which on my machine produce the following graph :

simple graph

The ticks are not aligned : the values shown are not the same on the xaxis (-3,-2,-1,0,1,2,3) and the y axis (-4,-2,0,2), resulting in a gray grid behind the points that is not symmetric.

If i do:

scatter(X,Y,xlims=[-4,4],ylims=[-4,4])

this works and gives :

simple graph

However i think this is by chance and i am not sure it will make them symetric in other situation (different size of the plot, etc…), and i do not want to specify by hand the maximum and minimum values !

I am not aware of a built-in function to do that, but you could just supply the xticks and yticks, making them the same. It isn’t too hard to do “by hand” since you have the X and Y data right there. For example, range(start=round(minimum(X),RoundDown),step=2,stop=round(maximum(X),RoundUp)) would give the ticks in your example plot.

1 Like

If the minimum value is like 0.9 and the maximum value is like 1.1, plotting from 0 to 2 would be a shame.

Is there a way I could look at how Plots.jl computes these values ?

Nothing is by hand in Julia…

xr, yr = extrema.((X,Y))
lims = extrema((xr..., yr...))
scatter(X,Y, lims=lims)
1 Like

Plots.jl does not really take the extremum, it gives a little more room to the points. I am sure there is somewhere coded the computation of these limits, i just have to find it :slight_smile:

scatter(X,Y, lims=lims, widen=true)

1 Like

widen=true seemsto be the solution. Thanks a lot this works as expected now :slight_smile: