Matlab Help for 3D Graphing

조회 수: 22 (최근 30일)
Jack A
Jack A 2020년 4월 3일
댓글: darova 2020년 4월 3일
I'm trying to model the Boston Red Sox's and New York Yankees' homeruns over the past 50 years from 1969-2019 and the graph I'm getting is coming out as I want it to. Can somone help fix my code to get the graph right and explain to me the changes they made and why they did it?
x = 0:49;
y = 0:1;
z = [197 203 161 124 147 109 134 134 213 172 194 162 90 136 142 181 162 144 174 124 108 106 127 84 114 120 175 209 185 205 176 167 198 177 238 222 199 192 166 173 212 211 203 165 178 123 161 208 168 208
94 111 97 103 131 101 110 120 184 125 150 189 100 161 153 130 176 188 196 145 130 147 147 163 178 139 122 162 161 207 193 205 203 223 230 242 229 210 201 180 244 201 222 245 144 212 183 241 267 306];
xv = 0:0.05:49;
yv = 0:0.05:1;
[xi,yi] = meshgrid(xv,yv);
zc = interp2(x,y,z,xi,yi,'cubic');
mesh(xi,yi,zc);
surfc(xi,yi,zc);
xlabel('Years')
ylabel('Teams')
zlabel('Number of Homeruns');
title('Homeruns Hit Red Sox vs Yankees 1969-2019')
yt = get(gca,'YTick');
ytklbl = {'Boston Red Sox','New York Yankees'};
ytix = linspace(min(yt),max(yt),numel(ytklbl));
set(gca,'YTick',ytix,'YTickLabel',ytklbl)
xt = get(gca,'XTick');
xtklbl = {'69','70','71','72','73','74','75','76','77','78','79','80','81'
'82','83','84','85','86','87','88','89','90';'91','92','93','94','95'
'96','97','98','99','00','01','02','03','04','05','06','07','08','09'
'10','11','12','13','13','14','15','16','17','18','19'};
xtix = linspace(min(xt),max(xt),numel(xtklbl));
set(gca,'XTick',xtix,'XTckLabel',xtklbl)
[minhr,idxmin]=min(zc(:));
[maxhr,idxmax]=max(zc(:));
[rmin,cmin]=ind2sub(size(zc),idxmin);
[rmax,cmax]=ind2sub(size(zc),idzmax);
text(xv(cmax),yv(rmax),maxhr,{'This is the maximum';'\downarrow'},'HorizaontalAlignment','center','VerticalAlignment','bottom');
text(xv(cmin),yv(rmin),minhr,{'\uparrow';'This is the minimum'},'HorizontalAlignment','center','VerticalAlignment','top');
view(-100,100)
  댓글 수: 5
darova
darova 2020년 4월 3일
Tommy, you can add this as answer
Tommy
Tommy 2020년 4월 3일
Gladly!

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

답변 (1개)

Tommy
Tommy 2020년 4월 3일
The plot:
And the updated code:
x = 0:49;
y = 0:1;
z = [197 203 161 124 147 109 134 134 213 172 194 162 90 136 142 181 162 144 174 124 108 106 127 84 114 120 175 209 185 205 176 167 198 177 238 222 199 192 166 173 212 211 203 165 178 123 161 208 168 208
94 111 97 103 131 101 110 120 184 125 150 189 100 161 153 130 176 188 196 145 130 147 147 163 178 139 122 162 161 207 193 205 203 223 230 242 229 210 201 180 244 201 222 245 144 212 183 241 267 306];
xv = 0:0.05:49;
yv = 0:0.05:1;
[xi,yi] = meshgrid(xv,yv);
zc = interp2(x,y,z,xi,yi,'cubic');
mesh(xi,yi,zc);
surfc(xi,yi,zc);
xlabel('Years')
ylabel('Teams')
zlabel('Number of Homeruns');
title('Homeruns Hit Red Sox vs Yankees 1969-2019')
yt = get(gca,'YTick');
ytklbl = {'Boston Red Sox','New York Yankees'};
ytix = linspace(min(yt),max(yt),numel(ytklbl));
set(gca,'YTick',ytix,'YTickLabel',ytklbl)
xt = get(gca,'XTick');
xtklbl = {'69','70','71','72','73','74','75','76','77','78','79','80','81',...
'82','83','84','85','86','87','88','89','90','91','92','93','94','95',...
'96','97','98','99','00','01','02','03','04','05','06','07','08','09',...
'10','11','12','13','13','14','15','16','17','18','19'};
xtix = linspace(min(xt),max(xt),numel(xtklbl));
set(gca,'XTick',xtix,'XTickLabel',xtklbl)
[minhr,idxmin]=min(zc(:));
[maxhr,idxmax]=max(zc(:));
[rmin,cmin]=ind2sub(size(zc),idxmin);
[rmax,cmax]=ind2sub(size(zc),idxmax);
text(xv(cmax),yv(rmax),maxhr,{'This is the maximum';'\downarrow'},'HorizontalAlignment','center','VerticalAlignment','bottom');
text(xv(cmin),yv(rmin),minhr,{'\uparrow';'This is the minimum'},'HorizontalAlignment','center','VerticalAlignment','top');
view(-100,100)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by