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
Related
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
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;
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')
This question already has answers here:
How should I update the data of a plot in Matlab?
(3 answers)
Real time plot in MATLAB
(2 answers)
Closed 5 years ago.
I know how to update the data in Matlab plot and plot it in real time; however, I d could not figure out how can I keep my background while I am updating it. Can anyone help me on that?
----------------------------------------
x=[0 2000];
y=[0 180e3];
xlim(x)
ylim(y)
I=imread('MAP.png');
%Flip the image
imagesc(x, y, flipud(I));
%Fix the axes
set(gca,'ydir','normal');
% hold on;
for i=1:2000
PlotUpdate(t(i),p(i))
grid on
pause(0.01);
end
----------------------------------------
%-------- Function is:
function PlotUpdate(speed,power)
h = plot(speed,power,'or','MarkerSize',5,'MarkerFaceColor','r');
h.XData = speed;
h.YData = power;
refreshdata(h,'caller')
end
When I am activating "hold on" command, it will show the background + all the 2000 points plot by function (PlotUpdate).
When I deactivate the hold on, my background picture is gone and I can only see the 2000 points in the plot!
Any idea how can I have both?
Thanks in advance,
This question already has answers here:
MATLAB - How to zoom subplots together?
(3 answers)
Closed 7 years ago.
I created a figure with 8 subplots of timeseries objects, because I wanted to have an overview of the data.
Is there an option which gives me the following possibility:
If I zoom into one subplot (for example: just the range from 5 to 10 on the x-axis is now visible), than all other plots will automatically zoom in (such that the range from 5 to 10 on the x-axis is now just visible for all other subplots) too??
For linking both the x and y axes, you should use the command linkaxes. It takes as input a vector of handles to axis objects you want to link together, and additional options if desired.
Example:
for k = 1:4
ah(k) = subplot(2,2,k);
plot(1:10, rand(1,10));
end
linkaxes(ah);
After this, if you apply e.g. zooming on any of the subfigures, the x and y limits of the other axes will change as well.
If you want to link only, say, the x axis, use instead:
linkaxes(ah,'x');