Custom grid on a plot

29 views (last 30 days)
BN
BN on 19 Apr 2020
Edited: Adam Danz on 21 Apr 2020
Hey all,
I have a plot that represents an error of 7 models in the months. I wanted to split each month's results by a line or grid to determine it more clear. so I used this code:
set( gca, 'YGrid', 'on' );
But as you can see the bars not adjusted between the grids; for example, if you look at Jan results, some of them appear before Jan line and some of them appear after this line. How I can have a line that split each month's results?
Thank you so much.
  1 Comment
Ameer Hamza
Ameer Hamza on 19 Apr 2020
Can you show the commands used to plot this graph? It would be better if you can also attach the variables used to make this bar plot.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 20 Apr 2020
Edited: Adam Danz on 21 Apr 2020
If the x-axis is a datetime axis, compute the 15th of each month and use plot(), line(), or xline() to plot vertical lines on the 15th from min(ylim()) to max(ylim()).
If the x-axis is numeric and each month is represented by x = 1,2,...,12, then your x values will be +0.5.
If you get stuck, show us what you've got and I'd be glad to help straighten it out.
Demo
fig = figure();
ax = axes(fig);
xlim([0,13])
ylim([0,10])
hold on
plot((.5:1:12.5).*[1;1], [min(ylim(ax)),max(ylim(ax))], '-k', 'Color', [.5 .5 .5])
ax.XTick = 1:12;
Or use xline()
fig = figure();
ax = axes(fig);
xlim([0,13])
ylim([0,10])
ax.XTick = 1:12;
hold on
arrayfun(@(x)xline(x,'-','Color',[.5 .5 .5]), .5:1:12.5)

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!