# How to center the horizontal axis to the graph in Plots.jl

**URL:** <https://discourse.julialang.org/t/how-to-center-the-horizontal-axis-to-the-graph-in-plots-jl/96252>\
**Category:** General Usage\
**Tags:** question, plotting, plots\
**Created:** [March 17, 2023, 4:17pm UTC](https://discourse.julialang.org/t/how-to-center-the-horizontal-axis-to-the-graph-in-plots-jl/96252 "2023-03-17T16:17:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KZ-Spectra](https://avatars.discourse-cdn.com/v4/letter/k/2acd7d/32.png) [@KZ-Spectra](https://discourse.julialang.org/u/KZ-Spectra)\
**Post date:** [March 17, 2023, 4:17pm UTC](https://discourse.julialang.org/t/how-to-center-the-horizontal-axis-to-the-graph-in-plots-jl/96252/1 "2023-03-17T16:17:07Z")

</div>

```julia
using NonlinearSolve, LinearAlgebra, Plots

N₂=14

g₂(u,p) = p[1]*Tridiagonal(fill(1,N₂), fill(-2,N₂+1), fill(1,N₂))*u - u.^3 - p[2]*u 

probg₂ = NonlinearProblem{false}(g₂, [0.1; fill(0.3, N₂-1); 0.1], [0.6, 1])
solver₂ = solve(probg₂, NewtonRaphson(), reltol = 1e-9) # solution is given as a vector

plot(solver₂, xlabel="x", ylabel="gₙ", linewidth=2, label=false, color="green", size=(450, 300))
scatter!(solver₂, xlabel="n", ylabel="gₙ", color="red", label=false, size=(450, 300))

```

The above code produces a vector of data points, solver\_2, and then I plot those data points via the Plot.jl package and the scatter() function. See the below figure 1 for the plot resulting from the above code.

**Question:** How do I center the horizontal axis such that the peak of the graph is centered at the grid point `n=0`?

**Attempt:** Via the documentation here [API · Plots](https://docs.juliaplots.org/latest/api/#Plots.xlims) I include `xlims=(-7.5, 7.5)`, but this does not produce the desired result. See figure 2.

Fig 1:

![N6J79qG](https://global.discourse-cdn.com/julialang/original/3X/b/0/b0a61a0e44be045915515e4e31a2b51ad3529031.png)

Fig 2:  
 ![8EMwKB4](https://global.discourse-cdn.com/julialang/original/3X/0/9/0911a8949166a747ecb5855cefe6bfeda2838f6a.png)

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 17, 2023, 4:22pm UTC](https://discourse.julialang.org/t/how-to-center-the-horizontal-axis-to-the-graph-in-plots-jl/96252/2 "2023-03-17T16:22:39Z")

</div>

It looks like the graph peaks at `n=8`. So subtract 8 from your x axis variables. I do not know if there is an easy way to do this given that you are plotting using a convenience function rather than directly passing `x` and `y` values.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 17, 2023, 4:29pm UTC](https://discourse.julialang.org/t/how-to-center-the-horizontal-axis-to-the-graph-in-plots-jl/96252/3 "2023-03-17T16:29:41Z")

</div>

Sorry, I didn’t read that it actually is just plotting a vector. In this case you can do

```julia
plot((1:length(solver₂)) .- 8,solver₂, xlabel="x", ylabel="gₙ", linewidth=2, label=false, color="green", size=(450, 300))

```

In general, you’d want to have some function that finds the peak position rather than doing it visually like this.
