When I replot some of the axes in my figure the previous tick labels don't delete and the new tick labels overlap.

16 views (last 30 days)
I have a figure window with 6 axes and a control panel. The axes can be re-plotted using different coordinate systems, which moves the ticks and grids on the plot, creating new tick labels. I cannot get the previous labels to delete before the new labels are added, making the mess shown in the image. I've tried "clf()", but that clears almost everything off of my figure, which is not useful. And putting "figure" before the plot commands opens a new figure window which is also not useful. Multiple figure windows makes the use of the program cumbersome. So far the best I've been able to do is to keep the first set of tick labels without being over written with new labels by putting
set(gca,'xTickLabel','','yTickLabel','') % delete the x-tick and y-tick labels
before or after the plot command (seems to do the same thing regardless of location), but then the tick labels are not lined up with the ticks and are meaningless. Here is the code and image of the plot:
if true
%First plotted with this code:
eclip_lon_overlay = axes('Units','pixels','Position',[1077 547 450 450],'Xdir','reverse');
eclip_lon_overlay = gca;
%Plot initial background radiants
plot(orbit_data(o_index,14),orbit_data(o_index,13),'.k','markersize',markersize)
set(gca,'Xdir','reverse')
hold on
%Add Titles and labels
eclip_lon_overlay.Title.String = 'D Pass Overlay';
eclip_lon_overlay.FontSize = 10;
eclip_lon_overlay.Title.FontSize = 13;
xlabel('\bfEcliptic Longitude','FontSize',12)
ylabel('\bfEcliptic Latitude','FontSize',12)
%Set Plot size
axis([shower_data(s_index,15) - plot_frame_offset,shower_data(s_index,15) + plot_frame_offset,...
shower_data(s_index,16) - plot_frame_offset,shower_data(s_index,16) + plot_frame_offset])
%Plot center cross marker
plot(shower_data(s_index,15),shower_data(s_index,16),'+r','markersize',(markersize + 12))
grid on
hold off
if true
%Replotted with this code:
gca = axes('Units','pixels','Position',pos1,'Xdir','reverse');
plot(orbit_data(o_index,o_x_data),orbit_data(o_index,o_y_data),'.k','markersize',markersize)
set(gca,'Xdir','reverse')
hold on
%Set Plot size
axis([x - plot_frame_offset - 0,x + plot_frame_offset + 0,y - plot_frame_offset - 0,y + plot_frame_offset + 0])
%Plot D Pass overlays
plot(orbit_data(o_index(d_pass),o_x_data),orbit_data(o_index(d_pass),o_y_data),'Marker','.','Color',d_color,...
'MarkerSize',(markersize + 4),'LineStyle','none')
%Plot center cross marker
plot(x,y,'+r','markersize',(markersize + 12))
grid on
hold off
end
end

Accepted Answer

Cam Salzberger
Cam Salzberger on 16 May 2017
Hello David,
I believe this is occurring because you are creating a new set of axes when you do:
gca = axes('Units','pixels','Position',pos1,'Xdir','reverse');
"axes" is the function to create a new axes. So this will create a new axes in the position specified by "pos1". I'm not sure where "pos1" is defined, but I can only assume it's the same as the old axes. You are seeing both the old and new tick labels because the old axes is still behind there.
Also, please never assign things to variables that have the same name as built-in functions. "gca" is a built-in function to get the current axes. When you do "gca = axes(...)", it assigns "gca" as a variable, which will always be accessed now, making the "gca" function unusable. Really, really recommend against doing that. It also makes code far more confusing.
As "MathReallyWorks" said in your last question, you could use "clf" to clear the figure before making a new axes. However, that's probably overkill.
If you are looking to keep your old data, you can just keep "hold on" and just plot to the old axes. If you don't want your old data, you could call "cla" on the old axes, or do "hold off", and then call plot again.
If you'd like an advanced maneuver, I'd recommend retaining your original handle to the line object output by the first "plot" call. This would allow you to simply replace the data in the line properties, rather than clearing the axes and replotting. This is faster by a small amount. For example:
figure
axes
hStars = plot(1:10,1:10,'.k');
% ... do stuff ...
newX = 1:2:20;
newY = 11:2:30;
hStars.XData = newX;
hStars.YData = newY;
-Cam
  2 Comments
David Holman
David Holman on 16 May 2017
Cam, Thanks for the help. I've eliminated all the miss-used gca commands. I thought that gca = axes(....,'Position','pos1',...) was telling the following plot command which axes to plot on, and it seemed to work. But now I'm having trouble getting the replots to occur in the proper axes. Now when I replot they all happen in the last axes, but as all six plots flash by the ticks and their markers change accordingly with no overlapping and the tick marks for the last plot are perfect! Any advice on how to get the plots in the proper axes? It will take me a longer while to figure out your advanced maneuver, but it sounds like the right way to do it. Thanks, David

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!