Vectorize plotting multiple lines with different colors in MATLAB? - matlab

Is there a way to vectorize/accelerate the task of plotting multiple lines with different colors?
The working-but-slow approach is
X = [1 2; 3 4];
Y = [2 -4; 5 2];
figure;
hold on;
colors = [1 0 0; 0 1 0];
for idx = 1:size(X, 2)
l = plot(X(:, idx), Y(:, idx), 'Color', colors(idx, :));
end
hold off;
I tried
X = [1 2; 3 4];
Y = [2 -4; 5 2];
figure;
plot(X, Y, 'Color', [1 0 0; 0 1 0]);
but no luck.

This is probably too hacky to be a useful replacement of the loop, but here it goes:
set(gca, 'ColorOrder', [1 0 0; 0 1 0], 'NextPlot', 'add')
plot(X, Y);
The 'ColorOrder' property contains the colors to be used by default for new plots. Setting 'NextPlot' to 'add' seems to be necessary so that the call to plot doesn't reset 'ColorOrder' to its default value.
Tested on R2015b.

Related

How to shade area and make it transparent between two lines in MATLAB?

I shaded the area between two lines, it's not very clean:
area(xData,[Y1(:) ,Y2(:)-Y1(:)]); hold on
colormap([1 1 1; 0 0 1]);
How to make it transparent too in MATLAB? So that it comes like:
ref:peltiertech.com
You can use the FaceAlpha property of the area object to set the transparency level:
xData = 1:7;
Y1 = [3 2 1 4 3 2 1];
Y2 = [8 6 9 8 7 5 6];
area(xData, Y2, 'EdgeColor',[0 .447 .741], 'FaceColor',[0.929 .694 .125], 'FaceAlpha',.3);
hold on
area(xData, Y1, 'EdgeColor',[0 .447 .741], 'FaceColor', [1 1 1]);
A cleaner approach is to use patch instead of area:
h = patch([xData xData(end:-1:1) xData(1)], [Y1 Y2(end:-1:1) Y1(1)], 'b');
set(h, 'EdgeColor',[0 .447 .741], 'FaceColor',[0.929 .694 .125], 'FaceAlpha',.3)

How to change bar graph style

I'm trying to create a bar graph, and set every bar a different style (lines, dots, circles and ext...).
In this example:
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
x = [10; 20; 50; 90];
bar(x,y);
All 3 bars have the same style.
How can I change it and set 3 differents styles for the 3 bars ?
Use a handle output when calling bar
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
x = [10; 20; 50; 90];
h = bar(x,y);
That gives an array h of bar objects (of length 3 in your example), and you can set their aspect independently. For example,
set(h(1), 'EdgeColor', 'r');
set(h(2), 'EdgeColor', 'g');
set(h(3), 'EdgeColor', 'b');
gives the following graph in R2015b (the aspect will vary in other versions).
Other properties you can change are 'BarWidth', 'LineStyle', etc. To see the list type get(h(1)).
I am adding some style changes to what #Luis given
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
x = [10; 20; 50; 90];
h = bar(x,y);
set(h(1),'FaceColor', 'w','LineWidth', 1, 'LineStyle',':');
set(h(2),'FaceColor', 'w','LineWidth', 1, 'LineStyle','--');
set(h(3),'FaceColor', 'w','LineWidth', 1, 'LineStyle','-.');
'FaceColor', 'w' -- Makes the color of bar white
'LineWidth', 1 -- Width of the border line of bar
'LineStyle',':' -- Dotted line
'LineStyle','--' -- Dashed line
'LineStyle','-.' -- Dash-dot line
For different style for each bar set
figure
hold on;
y = [2 2 3; 0 0 0; 0 0 0;0 0 0 ];
x = [10; 20; 50; 90];
z=bar(x,y);
ylim([0 15]);
set(z,'FaceColor', 'w','LineWidth', 1, 'LineStyle',':');
y = [0 0 0; 2 5 6; 0 0 0;0 0 0 ];
x = [10; 20; 50; 90];
z1=bar(x,y);
set(z1,'FaceColor', 'w','LineWidth', 1, 'LineStyle','-.');
y = [0 0 0;0 0 0; 2 8 9; 0 0 0];
x = [10; 20; 50; 90];
z2=bar(x,y);
set(z2,'FaceColor', 'w','LineWidth', 1, 'LineStyle','-');
y = [0 0 0;0 0 0; 0 0 0; 2 11 12];
x = [10; 20; 50; 90];
z4=bar(x,y);
set(z4,'FaceColor', 'w','LineWidth', 1, 'LineStyle','--');
hold off;

How to change size of grid & granularity in meshc function

Show mask by meshc function
z = [0 0 0 0;0 1 1 0;0 0 0 0];
meshc(z)
Output is:
Desired output:
There is a lot of guessing on my side, and I guess you want something like this:
%// data
z = [0 0 0 0;0 1 1 0;0 0 0 0];
%// grid
[n,m] = size(z);
[x,y] = ndgrid(1:n,1:m);
%// finer grid
[xq, yq] = ndgrid(linspace(1,n,100),linspace(1,m,100));
%// interpolation
F = griddedInterpolant(x, y, z, 'cubic')
zq = F(xq, yq);
%// interpolated plot
figure(1)
meshc(xq,yq,zq)

In matlab, plot a heatmap and a line plot on the same figure

In matlab 2014b, I want to make a heatmap and then overlay a line plot using the right y-axis. For example
colormap bone
data = rand(6);
imagesc(data)
ax = gca;
ax.XTick = [1 2 3 4 5 6];
ax.YTick = [1 2 3 4 5 6];
hold on
Now plot a line but use the right y-axis because this has negative values:
x2 = [1 2 3 4 5 6];
y2 = [-0.0001 -0.0997 -0.1997 -0.2995 -0.3994 -0.4995];
plot(x2,y2,'r')
You can make it with a variation of plotyy where the first plot is composed of NaNs.
Here is the code:
hold on
colormap bone
data = rand(6);
imagesc(data)
ax = gca;
yT = ax.YTick;
x2 = [1 2 3 4 5 6];
y2 = [-0.0001 -0.0997 -0.1997 -0.2995 -0.3994 -0.4995];
[ax, ~, h] = plotyy(yT*NaN, yT, x2,y2);
ax(1).YLim = [yT(1)-0.5 yT(end)+0.5];
ax(1).YTick = yT;
ax(1).YColor = [0 0 0];
set(h, 'Color', 'r');
ax(2).YColor = [1 0 0];
ax(2).YTick = -0.5:0.1:0;
and the result:
Best,

Add markers to a step function in Matlab

I have the following step function:
I wish to add 2 markers, on the left hand side of the horizontal line as a filled circle and on the right hand side a circle which is not filled with color. How can I do this please? Thank you.
I used very simple code for this graph. The code is:
x=[1 1.5];
y=[1 1];
plot(x,y)
hold on
x=[1.5 2.6];
y=[2 2];
plot(x, y)
hold on
x=[2.6 5];
y=[3 3];
plot(x, y)
hold on
x=[5 6];
y=[4 4];
plot(x, y)
hold on
x=[6 6.5];
y=[5 5];
plot(x, y)
hold on
x=[6.5 8];
y=[6 6];
plot(x, y)
axis([0 9 0 7])
Do as follows for each segment (the new lines are the last two):
x=[1 1.5];
y=[1 1];
plot(x,y)
hold on
plot(x(1),y(1),'o','MarkerFaceColor','b') %// filled circle
plot(x(2),y(2),'o') %// non-filled circle