How to put the grid lines at the back of my figure?
11 views (last 30 days)
Show older comments
I want to put the grid lines into the back of my plot, I have searched the previous answered questions but I'm unable to do so. Please, your cooperation will be highly appreciated.
%% Plottting data of global warming
gw_data=readtable('Global warming.csv');
figure('Name','Global warming data','innerposition',[.14 .3 .34 .6]')
x=gw_data{41:141,1};
y=gw_data{41:141,2};
% Find where data is positive or negative.
pind = y >= 0;
nind = y < 0;
hold on
% Plot column plot (bar chart).
b=bar(x(pind), y(pind),'BarWidth',1,'FaceColor',[0 0.4470 0.7410]);%[0 0.4470 0.7410]
c=bar(x(nind), y(nind),'BarWidth',1,'FaceColor',[0.6350 0.0780 0.1840]);%[0.6350 0.0780 0.1840]
title('\fontname{Arial}Global average surface temprature','FontSize',16,'FontWeight','bold')
xlabel('\fontname{Arial}Years','FontSize',14,'FontWeight','normal');
ylabel('\fontname{Arial}Temprature difference \newline 1920-2022 (\circC)','FontSize',14,'FontWeight','normal');
set(gca,'xlim',[1920 2020],'xtick',[1920:20:2020],'ylim',[-1 1],'ytick',[-1:0.2:1],'FontSize',12,'FontWeight','normal');
xticklabels({'1922','1942','1962','1982','2008','2022'});
%%gridlines-----
g_y=[-1:0.2:1]; % user defined grid Y [start:spaces:end]
g_x=[1920:10:2020]; % user defined grid X [start:spaces:end]
for i=1:length(g_x)
plot([g_x(i) g_x(i)],[g_y(1) g_y(end)],'Color',[.7 .7 .7]) %y grid lines
hold on
end
for i=1:length(g_y)
plot([g_x(1) g_x(end)],[g_y(i) g_y(i)],'Color',[.7 .7 .7]) %x grid lines
hold on
end
set(gcf, 'units','normalized','outerposition',[.3 .4 .35 .38]);
%print(gcf,'figure.tiff','-dtiff','-r300');
2 Comments
Simon Chan
on 2 Jul 2022
Edited: Simon Chan
on 2 Jul 2022
Or you can just reverse the order by plotting the grid first and then the bar charts.
Answers (1)
Zhaoxu Liu / slandarer
on 26 Aug 2022
use grid instead of plot:
%% Plottting data of global warming
gw_data=readtable('Global warming.csv');
figure('Name','Global warming data','innerposition',[.14 .3 .34 .6]')
x=gw_data{41:141,1};
y=gw_data{41:141,2};
% Find where data is positive or negative.
pind = y >= 0;
nind = y < 0;
hold on
% Plot column plot (bar chart).
b=bar(x(pind), y(pind),'BarWidth',1,'FaceColor',[0 0.4470 0.7410]);%[0 0.4470 0.7410]
c=bar(x(nind), y(nind),'BarWidth',1,'FaceColor',[0.6350 0.0780 0.1840]);%[0.6350 0.0780 0.1840]
title('\fontname{Arial}Global average surface temprature','FontSize',16,'FontWeight','bold')
xlabel('\fontname{Arial}Years','FontSize',14,'FontWeight','normal');
ylabel('\fontname{Arial}Temprature difference \newline 1920-2022 (\circC)','FontSize',14,'FontWeight','normal');
set(gca,'xlim',[1920 2020],'xtick',[1920:20:2020],'ylim',[-1 1],'ytick',[-1:0.2:1],'FontSize',12,'FontWeight','normal');
xticklabels({'1922','1942','1962','1982','2008','2022'});
%%gridlines-----
g_y=[-1:0.2:1]; % user defined grid Y [start:spaces:end]
g_x=[1920:10:2020]; % user defined grid X [start:spaces:end]
ax=gca;hold on;grid on
g_y=-1:0.2:1;
g_x=1920:10:2020;
ax.GridColor=[.7,.7,.7];
ax.GridAlpha=1;
ax.XTick=g_x;
ax.YTick=g_y;
0 Comments
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!