How to keep a background picture fixed while updating a plot? [duplicate] - matlab

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,

Related

How to add plot labels loops in matlab [duplicate]

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;

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

matlab image popping during animation creation [duplicate]

This question already has answers here:
Approaches to create a video in matlab
(4 answers)
Closed 6 years ago.
I'll do my best to explain my problem. I am simulating the dynamics of a network and I'd like to get an animation where each frame represents my network with a specific color for each node with respect to an input file.
Here my script
for ii=1:Movie_size
hfig=figure('visible','off');
hold on;
%plot edges
for kk=1:Nedge,
plot(xedge(kk,:),yedge(kk,:),'black')
end
%color of the nodes
for kk=1:nodes,
val=(1-(Color_node(12798 ,kk)-umin)/(umax-umin));
ggCol(kk,:)=[1,val,1-val];
end
%enhanced the contrast of the figure
ggCol = imadjust(ggCol,[.2 .3 0; .6 .7 1],[]);
%plot nodes
for kk=1:nodes,
plot(xpos(kk),ypos(kk),'o','MarkerFaceColor',ggCol(kk,:), ...
'MarkerEdgeColor','k','MarkerSize',10)
end
frames(ii)=getframe(hfig);
hold off;
end
movie(frames);
I succeeded in plotting each frame but when I want to get the animation, I have all the figures being displayed and no movie. I tried a lot of different things but it never works...
PS : I have been editing the title since the topic seems to have been already asked...
While you have already called getframe which takes a screen capture of the current figure, you need to do something with this frame to make a movie. The typical thing would be to add this frame to an existing VideoWriter object within your loop.
writer = VideoWriter('output.avi');
hfig = figure();
hplot = plot(rand(10,1));
for k = 1:100
% Update the plot
set(hplot, 'YData', rand(10, 1));
% Take a screengrab and add it to the video file
frame = getframe(hfig);
writer.writeVideo(frame);
end
writer.close()
Alternately, you can create an array of frames and then display these interactively within MATLAB with movie.
for k = 1:100
frames(k) = getframe(hfig);
end
% View as a movie
movie(frames)
Update
Based on your updated question, the windows have to popup because getframe must have the figure render before it is able to capture the screen. Also, you've created your array of frames but haven't attempted to display a movie. You need:
movie(frames)

Create a colormap in matlab [duplicate]

This question already has answers here:
How to create a custom colormap programmatically?
(2 answers)
Closed 7 years ago.
I have a contour plot with data that goes from -90 to 90 degrees. for now i am using jet so I have a map that looks like this
I have been asked to change the colormap so that instead of having a gradient, I have a fixed color for each 5 degress (so I believe 36 colors). Also i was thinking of maybe having same colors for the interval [5 10] and [-10 -5], and so on if that makes sense.
My code is quite long because i have a lot of data to process, but that's part of it just so you can see what function i am using to plot this
%%
x1=data(:,5); %x location
y1=data(:,16); %y location
z1=phi*90; %angle phi
z2=gamma*90; %angle gamma
n=300; precision of grid
%Create regular grid across data space
[X,Y] = meshgrid(linspace(min(x1),max(x1),n), linspace(min(y1),max(y1),n));
figure(3);
contourf(X,Y,griddata(x1,y1,z1,X,Y),100,'EdgeColor', 'None')
%title('Variation of In-plane angle \phi')
axis equal
axis ([0 8000 0 12000])
axis off
h=colorbar;
caxis([-90 90])
set(h, 'YTick', [-90:15:90])
Does anyone know how to create this colorbar?
Cheers
Every colormap-generating function in Matlab, including jet, takes an argument that specifies how many colormap entries there should be. In your case, you want 180 / 5 = 36 discrete colors:
colormap(jet(36))
To make sure the 36 colors cover exactly the 5 degree steps, set the color axis explicitly:
caxis([-90 90])
The result looks e.g. like this: