필터 지우기
필터 지우기

How to put the grid lines at the back of my figure?

조회 수: 6 (최근 30일)
Wiqas Ahmad
Wiqas Ahmad 2022년 7월 2일
답변: Zhaoxu Liu / slandarer 2022년 8월 26일
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
Simon Chan
Simon Chan 2022년 7월 2일
편집: Simon Chan 2022년 7월 2일
Any reason not to use function grid?
Or you can just reverse the order by plotting the grid first and then the bar charts.
Wiqas Ahmad
Wiqas Ahmad 2022년 7월 2일
Thank you

댓글을 달려면 로그인하십시오.

답변 (1개)

Zhaoxu Liu / slandarer
Zhaoxu Liu / slandarer 2022년 8월 26일
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;

카테고리

Help CenterFile Exchange에서 Bar Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by