How to make vertical stripes line contour plot?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hello everyone,
I have a one dimensional monthly data for 5 years (i.e, 60 points). The data are varying in range between -3 to +3. I want to make a contour plot
of vertical striped lines each of same width corresponding to the value of the each data points.
To clarify more---
in X axis-------there are months.
in Y axis--- Vertical stripe lines of same width and height (may be 4 units). The vertical colour lines intensity shouldd be representing the data value.
first line==(same colour thorught the height). (if data has positive value in this point )
second line==(same color different intensity) (if data has positive value in this point )
So the plot must show the colorbar too.
Any sort of help would be really great.
Thanking you in advance.
채택된 답변
dt = datetime(01,01,2000) + calmonths(0:59); % row vector
vals = rand(size(dt))*6-3; % vector (row or col)
plot(dt,vals,'ks')
hold on
dateInt = diff(dt);
dateInt = dateInt([1:end, end])/2;
x = dt + dateInt.*[-1;-1;1;1];
[ymin,ymax] = bounds(vals);
y = repmat([ymin;ymax;ymax;ymin],1,numel(vals));
cvals = 3*ones(numel(vals),1).*-(vals(:)<0);
yline(0)
hold on
patch(x, y, cvals, 'FaceAlpha', 0.25, 'EdgeColor','none')
colormap([0 0 1; 1 0 0])
cb = colorbar();
caxis([-3,3])

Alpha scaled to y-values
See "fadescale" to scale the fade.
figure()
dt = datetime(01,01,2000) + calmonths(0:59); % row vector
vals = rand(size(dt))*6-3; % vector (row or col)
vals = sin(linspace(-pi,pi,numel(dt)))*2 + rand(1,numel(dt))*2-1;
% Compute patch verts
dateInt = diff(dt);
dateInt = dateInt([1:end, end])/2;
x = dt + dateInt.*[-1;-1;1;1];
[ymin,ymax] = bounds(vals);
y = repmat([ymin;ymax;ymax;ymin],1,numel(vals));
% Plot, start with underlying patches
figure()
axes()
hold on
h =patch(x, y, vals, 'EdgeColor','none');
% set colormap
nbins = 255;
fadescale = 0.7; % higher is more faded (0:1)
rgb = [ones(ceil(nbins/2),1),linspace(fadescale,1,ceil(nbins/2))' .* [1,1]];
rgb = [rgb; rot90(rgb(1:floor(nbins/2),:),2)];
colormap(flipud(rgb))
cb = colorbar();
caxis([-3,3])
% Add data
plot(dt,vals,'ko:','MarkerFaceColor',[.4 .4 .4], 'MarkerSize',4)
axis tight
ylim([-3,3])

댓글 수: 11
You question didn't mention anything about intensity.
I based the solution off of this description:
first line==(same colour thorught the height). (if data has positive value in this point )
second line==(same color different intensity) (if data has positive value in this point )
I interpreted that as "the same color for positive values" and "the same color for negative values" but I think you mean that the y-values should indicate the shade of reds and blues. I'll update my answer.
@Adam Danz Thanks man! But line no 7 there is dimenision mismatch coming, If it is possible Could be pls tell what are you doing in this line. The following error is coming
x = dt + dateInt.*[-1;-1;1;1];
Data inputs must be the same size, or any of them can be a scalar.
Thanks for your help.
What Matlab release are you using?
That line uses implicit expansion which became available in Matlab r2016b. Also, dt must be a row vector.
x = dt + dateInt.*[-1;-1;1;1];
This line creates the x-values for each patch-stripe. If you're using an earlier release of Matlab before r16b, replace that line with this one.
% x = dt + dateInt.*[-1;-1;1;1];
x = dt + [-dateInt; -dateInt; dateInt; dateInt];
@Adam Danz I am using the R2020a. Even there it's showing the same error. Other lines are fine only this line showing issue.
x = dt + [-dateInt; -dateInt; dateInt; dateInt];
Data inputs must be the same size, or any of them can be a scalar.
> I am using the R2020a.
Good. What's the result of
size(dt)
size(dateInt)
Subhodh Sharma
2021년 8월 24일
편집: Subhodh Sharma
2021년 8월 24일
Both has the same dimension 1*60
size(dt)
size(dateInt)
ans =
1 60
ans =
1 60
all variables
dateInt 1x60 496 duration
dt 1x60 480 datetime
vals 1x60 480 double
It turns out that implicit expansion was not supported for duration and datetime values until R2020b (see release notes).
Now I gotta think back to how I did stuff before implicit exapansion....
Try this,
x = [dt-dateInt; dt-dateInt; dt+dateInt; dt+dateInt];
Now this is coming.
patch(x, y, vals, 'EdgeColor','none');
Error using patch
Non-numeric data is not supported in 'patch'
I think I can do from here. Thanks @Adam Danz
Try surf()
Or just update to a newer release ;)
By overlay line do you mean the gray line in my example?
What specific problems are you having? An error? Line not appearing?
Are you plotting the stripes first and then adding the line? If you're plotting the line first, it's under the stripes so you can't see it.
@Adam Danz Thanks. worked
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
