I am creating a GUI in Matlab. I have several axes in which I plot different graphs. I have set in some of the axes the property XTick to []. However, each time I plot a new graph in the same axes, the xticks appear again. I know I can delete them by using set:
set(handles.axes_0, 'XTick', []);
However, this creates a "flickering" effect: you see the ticks appearing and then dissapearing each time I plot something new.
Do you know how could I have an axes with the XTick disabled avoiding the flickering effect?
Some basic code:
figure(1); %create new figure
set(gca, 'XTick', []); %Disable xtick
plot([1 2 ], [2, 3]); %Plot something. Xtick appears again
set(gca, 'XTick', []); %Disable xtick until next plot
As Shai pointed out in a comment, when using hold on the ticks don't reappear. As I want to clean the previous plot before drawing the new one, I search for its identifier using findobj and then delete it. Finally, I draw the new plot with hold on. Example (suppose the axes handle is called handles.axes_0):
h = findobj(handles.axes_0,'Type','line');
if ~isempty(h)
delete(h);
end
hold on
plot(handles.axes_0,x,y);
hold off
Related
Until now i had only 1 axis in my GUI to I used to just plot directly using plot command. Plus i need to plot these in a loop.
for i = 1:length(sig)
plot(sig(i).time,sig(i).signal,sig(i).time,updated(i).filter,)
hold on
end
Now i have 2 axes in my GUI, how can I make a certain plot appear in 1st axis and another in my 2nd axis
Now for example i need to plot the below in the 2nd axis
for i = 1:length(sig)
plot(sig(i).time,sig(i).fil,sig(i).time,updated(i).sig,)
hold on
end
Any help will be appriciated
You could specify the axes for hold and plot functions. Considering you have two axes, h1 and h2 inside your figure, you could do the following:
hold(h1, 'on')
hold(h2, 'on')
for i = 1:length(sig)
plot(h1, sig(i).time,sig(i).signal,sig(i).time,updated(i).filter)
plot(h2, sig(i).time,sig(i).fil,sig(i).time,updated(i).sig)
end
Please, create two functions to be able to reproduce what I mean:
First function:
function testPlot1()
pointData = rand(20000,3);
figure;
%hold on; % <- if commented out, does not work
plot3(pointData(:,1), pointData(:,2), pointData(:,3),'Marker', '.', 'MarkerEdgeColor', 'b','MarkerSize', 5, 'LineStyle', 'none');
axis equal;
xh = xlabel('X');
yh = ylabel('Y');
zh = zlabel('Z');
set([xh,yh, zh],...
'fontweight','bold',...
'fontsize',14,...
'color',[0,0,0]);
view(0,20);
end
Second function:
function testPlot2(fighandle)
axes(fighandle);
hold on;
plot3([0 3],[0 3],[0 3], 'r', 'LineWidth', 10);
end
If you now call
testPlot1();testPlot2(gca)
you will get the following:
If you however uncomment the "hold on" line in testPlot1() and call the above statement again, you will get:
To me this is unclear behavior. In the first case, testPlot1() creates a figure, draws the point cloud into it and modifies the axes properties. Then the call to testPlot2(gca) adds the line to the figure, but the line is clipped.
In the second case however the line is not clipped anymore. Why is it now not clipped and previously it was?
It seems to be related to the changes I make in the axes properties in testPlot1(). Could somebody explain this behavior to me? (why does it work with hold on, what do my changes in the axes properties cause)
hold on is a Matlab command (hold off turns it off again), where you can draw multiple elements on a single figure without the previous elements being erased.
What happens
If you call the plot function, a figure is created (or an already exisiting figure is used!) and Matlab draws a new plot into that figure. The previous plot that was in that figure is gone.
If you want to add more points to your plot, you can call hold on and then call plot again, this time with different numbers and maybe a different colour or so.
However, if you forget to turn hold off again for the active figure, any drawing activity you do (like plot) will be added to the figure. This is what happens in your second image in your question. You drew some points in the range 0 to 1, and then in the second function, you add some more but in the range 2 to 3. As a result, the axes expand to the range of 0 to 3.
Alternatively, you can call figure, which will cause a new figure to appear. figure_handle = figure(); will return a figure handle, which you can pass to your function, in case you have multiple figures and want to change one of them after a while.
I have 2 sets of data I want to plot on the same graph.
First an histogram:
hist(data1);
ax1 = gca;
I set the next set of axis, y on the other side
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','bottom',...
'YAxisLocation','right',...
'Color','none',...
'XColor','k');
If I use line() to plot my data it works:
line(data2a, data2b, 'Color', 'r', 'LineStyle', '-', 'Marker', '.', 'Parent', ax2);
But if I use plot(), the histogram is erased and both axis appear on the left.
plot(ax2, data2a, data2b);
Can somebody figure out why the second axis is not valid for plot()?
You should check out doc hold.
Axes in MATLAB have the 'NextPlot' property, specifying what to do when a new plot-function is issued on this axis.
The default for 'nextplot' is replace, meaning that before anything new is drawn, existing plots are erased.
Using hold(ax, 'on') or set(ax, 'nextplot', 'add') you can specify that new plots are added to the existing ones, instead of replacing them.
The reason that line and plot behave differently is, that high level functions (like plot) respect this axis property, while low-level functions like line, patch and others do not. They are added to axis in any case and do not remove existing children.
EDIT:
Now I'm noticing that ax2 should be empty in your case - maybe just try the above nevertheless ;)
I have a plot in MATLAB from which I would like to remove the xticks but keep the xticklabels. If I just remove the xtick like so:
set(gca, 'XTick', []);
...then the labels also disappear. Is there a way to keep the labels, without having to manually recreate them with text boxes? I thought about trying to make the length of the xticks zero, but this answer suggests that xtick properties cannot be independently controlled.
Try modifying the TickLength property:
set(gca, 'Ticklength', [0 0])
I plot a single trace in MATLAB with plot(). I'd like to add a right-y axis with a different set of tick marks (scaled linearly). Is this possible?
There are a number of good suggestions on this closely related question, although they deal with a more complicated situation than yours. If you want a super-simple DIY solution, you can try this:
plot(rand(1, 10)); % Plot some random data
ylabel(gca, 'scale 1'); % Add a label to the left y axis
set(gca, 'Box', 'off'); % Turn off the box surrounding the whole axes
axesPosition = get(gca, 'Position'); % Get the current axes position
hNewAxes = axes('Position', axesPosition, ... % Place a new axes on top...
'Color', 'none', ... % ... with no background color
'YLim', [0 10], ... % ... and a different scale
'YAxisLocation', 'right', ... % ... located on the right
'XTick', [], ... % ... with no x tick marks
'Box', 'off'); % ... and no surrounding box
ylabel(hNewAxes, 'scale 2'); % Add a label to the right y axis
And here's what you should get:
You may try this submission to MATLAB File Exchange - PLOT2AXES.
PLOT2AXES example http://www.mathworks.com/matlabcentral/fx_files/7426/2/plot2axes.png
Jiro's solution is good (file Exchange function), however, it does not allow to use Matlab's built-in plot functions (bar, scatter, etc.), and you have to use plot2axes instead. Matlab's own help gives the solution to have two axes on any type of plots:
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','k','YColor','k');
Look at: http://www.mathworks.com/help/techdoc/creating_plots/f1-11215.html
From matlab 2016 and onwards there is an option to define on what axis one plots:
yyaxis left
plots...
yyaxis right
plots...
source:
https://se.mathworks.com/help/matlab/ref/yyaxis.html
Open MATLAB Help with F1 and take a look at the functions below function plot which you mentioned, there you will see plotyy. This is what you probably need.
UPDATE: actually plotyy is NOT the answer to the question as pointed by gnovice.
I was able to do it with the following after plotting the left axis graph:
yyaxis right
ylabel('Right axis label')
plot(x,y1) % plot your right axis graph
Hope it helps.