Hi, I need help getting the X Axis labels to be complete.. I'd like to label every bar and have the years at a 45 degree angle. Code below.... thanks!
ax.XTickMode = 'manual';
ax.XTick = 1:24; %24 years of data, so I want 24 labels!
%ax.XTick = [0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1 1.05 1.1 1.15]; %tried making the bins tiny didn't help
ax.XTickLabels = {'2000','2001','2002','2003','2004','2005','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015','2016','2017','2018','2019','2020','2021','2022','2023'};
ax.XTickLabelRotation = 45;
w1 = 1; %this is how i figured out to get both data sets plotted on the same bar graph
bar(years,more,w1,'FaceColor',[0.4784 0.4627 0.4627])
w2 = 1;
hold on
bar(years,less,w2,'FaceColor',[0.8000 0.8000 0.8000])
hold off
grid on
ylabel('August Precipitation (in)')
legend({'Higher than Average','Lower than Average'},'Location','northeast')

 Accepted Answer

You must create the plot before you modify the axes properties. That, or as part of your plotting, specify the axes to add the plot to. Otherwise, the plotting function clears the axes before creating the figure.
years = 2000:2023;
precipitation = 4*rand(1,24)-2;
more = precipitation>=0;
less = precipitation<0;
w = 1;
bar(years(more),precipitation(more),w,'FaceColor',[0.4784 0.4627 0.4627])
hold on
bar(years(less),precipitation(less),1,'FaceColor',[0.8000 0.8000 0.8000])
hold off
xticks(years)
xtickangle(45);
grid on
ylabel('August Precipitation (in)')
legend({'Higher than Average','Lower than Average'},'Location','northeast')

1 Comment

Thank you! Really appreciate the hand-holding.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!