How to add plot labels loops in matlab [duplicate] - matlab

This question already has answers here:
Adding to a legend after each iteration
(5 answers)
Matlab dynamic legend / legend "hold on" like behavior
(1 answer)
Assemble Legend for Many Curves
(1 answer)
Closed 2 years ago.
I have data that I am plotting using a for loop. I dont know how to add a label for each graph to form a legend. This data is a lot and the names will have to be added in a looped manner. Please Advise.
Here is the code:
% Data for examples sake
q=[1;2;3;4;5;6;7;8;9;10];
a=[1;2;3;4;5;6;7;8;9;10];
b=a*2;
c=a*3;
d=a*4;
v_matrix=[a,b,c,d];
labels = ["a","b","c","d"];
%Code
[m,n]=size(v_matrix);
figure;
for i=1:1:n;
ylabel('Velocity (m/s)');
xlabel('Flow Rate (m^3/h)');
plot(q,v_matrix(:,i));
hold on;
end
The labels are generated in the same loop as the loop that generates the v_matrix.
This is what is generated:
This is what I want to be generated with the loop(legend was manually added with the "insert legend" button.

One way to do this would be to give the label of each line in the plot command itself using the 'DisplayName' property and then calling the legend:
figure
hold on
for i = 1:10
% char(97) = 'a', char(98) = 'b', ...
plot(1:10,(1:10).*i,'-','DisplayName',char(96 + i));
end
legend;

Related

Adding XTickLabels to grouped individual bar graphs and plot on hold [duplicate]

This question already has answers here:
Getting a second row of xlabels in matlab graph?
(1 answer)
add data label to a grouped bar chart in matlab
(1 answer)
Closed 7 months ago.
I have two types of plots.
In fist case I need to label individual bar graphs in the grouped bar plot. Here is the example
a=4.54,88.63,27.27,77.27,54.54;31.81,61.36,38.63,68.18,54.54;54.54,61.36,59.09,54.54,50;68.18,27.27,56.81,34.09,50;90.90,11.36,68.18,15.90,40.90];
b=0.40,0.55,0.70,0.85,1;1.39,1.54,1.69,1.84,1.99;2.340,2.49,2.64,2.79,2.94;3.36,3.51,3.66,3.81,3.96;4.29,4.44,4.59,4.74,4.89];
figure,
hold on
for i=1:5
bar(b(i,:),a(:,i))
end
figure,
hold on
for i=1:3
plot(b(i,:),a(:,i))
end
For the bar plot, I like to lable on the horizontal as shown in figure, the numbers are stored in an other matrix, say b
2) Similarly, I also want to XTickLables using values in b for the line plot.
you have first to define xticks and XTickLables
for example you can write:
a=[....];
b=[....];
c=[0.5,1,1.5,2,3];
D = {'0.5','1','1.5','2','3'};
figure,
hold on
for i=1:5
bar(b(i,:),a(:,i));
xticks(i) = c(i); %% first you define the ticks
XTickLables{i} = d(i); %% second you define the labels
end
for the exact same plot like the one you showed, I think you need two nested for loops.
more on that here: https://de.mathworks.com/help/matlab/ref/xticklabels.html

Overlaying / Plotting on top of an existing plot in MATLAB [duplicate]

This question already has answers here:
Show two different plots in one plot
(2 answers)
MATLAB - Plot multiple data sets on a scatter plot
(3 answers)
Closed 3 years ago.
I have a heat map of values generated by "pcolor" in MATLAB. I would like to plot a line plot on top of that.
I haven't found a proper solution in any measure yet.
The following code generates a "heat map" sort of output
hc = pcolor(middle_long, middle_height, middle_no2);
set(hc, 'Edgecolor', 'none');
c = colorbar;
caxis([0 0.015]);
axis([min(middle_long(:,1)) max(middle_long(:,1)) 0 1000])
The following code generates a line plot
plot(longflag, hflag)
The following are figures of the individual plot types that I would like to join, with an "example" of the final product I'd like listed afterwards:
Try something like this. Note the hold on part, which prevents plot from deleting the image produced by pcolor:
pcolor(rand(10))
colormap bone
axis xy
hold on
plot([1 10], [10 1], 'r')

How to plot with different colors [duplicate]

This question already has answers here:
How to hold a plot when using plot3 in matlab?
(1 answer)
plot hold for plot3 matlab
(1 answer)
Closed 5 years ago.
I am trying to plot a spiral with 8 turns. In each turn it is supposed to have a different color.
t = -1*pi*1:0.02:pi*1;
plot3(sin(t),cos(t),-1*pi*1:0.02:pi*1,'g')
t1 = -1*pi*2:0.02:pi*2;
plot3(sin(t1),cos(t1),-2*pi*1:0.02:pi*2,'r')
For now i am only plotting two turns, but it just turns out red. I have tried using hold on and hold off but its not working. Any suggestions?
check this
plot3(sin(t),cos(t),-1*pi*1:0.02:pi*1,'g-',sin(t1),cos(t1),-2*pi*1:0.02:pi*2,'r--')
The problem is that your red plot actually plots overtop of your previous one you should increase BOTH the lower bound and upper bound.
numberOfColors = 5;
for n = 0:numberOfColors-1
t = -pi + n*2*pi:0.02:pi + n*2*pi;
plot3(sin(t),cos(t),t,'Color',rand(3,1))
hold on
end

Is it possible to change markers in Matlab plot function? [duplicate]

This question already has an answer here:
Custom Markers for Matlab plot
(1 answer)
Closed 5 years ago.
I am trying to use matlab plot function to create a plot. However, the markers available are limited. For example:
plot(x,y,'-o')
will plot with circle markers.
However, if I want a marker with the arrow symbol or a letter, this is not possible. Does anyone know of any way to do this? Thanks!
Short answer : If you want custom markers, its convenient to make the plot in some other plotting software, like tikz.
Long answer :
If you want a simple text marker, e.g. the letter 'A', then its possible as follows (source)
font = 'Lucida Sans Typewriter'; % choose a font
m = 'A'; % choose desired character
x = 0:.5:2*pi;
y = sin(x);
% Use TEXT to plot the character along the data
text(x,y,m,'fontname',font,'color','red')
% Manually set axis limits, since TEXT does not do this for you
xlim([min(x) max(x)])
ylim([min(y) max(y)])
If you want a more complicated marker, then I found an example here on stackoverflow.

Is is possible to name a plot and attain a legend in MATLAB? [duplicate]

This question already has an answer here:
legend for selected plot objects in MATLAB figure
(1 answer)
Closed 8 years ago.
I have several plots in a MATLAB code. But handling the legend colors each time is painful. Is there a way to name plots such as plot1=... and then using this name attaina color to a corresponding legend?
Yes:
plot1 = plot(x,y);
legend(plot1, 'series 1', 'series 2');
plot1 is called the figure handle