Plot limit at next tick label

I would like my plot axes to end at a tick label. How best may this be accomplished?

Nice-looking plot:

using Plots
using Random
Random.seed!(0)
time = 0:20
spend = 0.8 * rand(21)
bar(time, spend,
    title="Spend over time",
    xlabel="Year",
    ylabel="Spend",
    yformatter=y->"\$$y")

Figure_1

Less-nice-looking plot:

using Random
using Plots
pyplot()

Random.seed!(4)
time = 0:20
spend = 0.8 * rand(21)
bar(time, spend,
    title="Spend over time",
    xlabel="Year",
    ylabel="Spend",
    yformatter=y->"\$$y")

Plot 2
Figure_1

Welcome to the community! Please take a look at this post, which has some suggestions about how to make it easier for folks to help you when you ask questions on discourse. In particular you can quote entire blocks of code. It looks like you figured out how to do it for a single line with backticks, but you can use three of them to do a block:

```
function foo(x)
    return x + 42
end
```

Or you can just highlight it and click the button that looks like this: </>.

As you your actual question, i don’t think there’s a way to do this automatically, but you can set the ticks and limits manually. I think it’s something like yaxis = ("Spend", 0:0.2:0.8, (0, 0.8)) (or separately, ylabel= "Spend", yticks=0:0.2:0.8, ylims=(0, 0.8)).

Thanks for that - somehow I missed that the right panel was a preview. (Now that I’m typing this, I see that the ‘welcome to JuliaLang’ message obscures the right panel and doesn’t fade on its own, so I guess that’s how I missed it.) I’ve updated the code block formatting.

I understand that the plot limits may be set manually, but I was looking for an automated way to do this, if one existed. Alternatively, if there was any way to leverage functionality from something like optimal_ticks_and_labels in Plots.jl, this would also be a great solution.

1 Like

There is a way to get the properties of a plot after it’s created (I don’t remember off the top of my head what it is), and you could maybe use that to figure out whether the limits are appropriate. I haven’t spent much time with the internals of the pipeline, but I don’t think it’s possible to do this automatically.

That said, I think having more uniform ticks that end at the limits would be an improvement that might be worth opening an issue or PR for.