How to make vertical stripes line contour plot?

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.

 Akzeptierte Antwort

Adam Danz
Adam Danz am 23 Aug. 2021
Bearbeitet: Adam Danz am 24 Aug. 2021
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 Kommentare

Adam Danz
Adam Danz am 24 Aug. 2021
Bearbeitet: Adam Danz am 24 Aug. 2021
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
Subhodh Sharma am 24 Aug. 2021
Bearbeitet: Subhodh Sharma am 24 Aug. 2021
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
Adam Danz
Adam Danz am 24 Aug. 2021
Bearbeitet: Adam Danz am 24 Aug. 2021
Try surf()
Or just update to a newer release ;)
Patch supports datetime objects starting in R2021a (see release notes).
Adam Danz
Adam Danz am 24 Aug. 2021
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Color and Styling finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by