Question

I have a Matlab imagesc plot that can't rescale its x axis correctly when the figure window size is changed. When the figure is generated the axis is correct, but when the x axis is increased the number of ticks increases and the x-labels loop back and repeat. This is undesirable behavior.

I would post pictures, but apparently I'm not reputable enough. You'll have to do with my written description instead. Basically the x-axis labels of my figure go from -100 to 400 with 6 ticks, i.e [-100 0 100 200 300 400]. When I drag the window wider, it goes from -100 to 400, loops back to -100 and up to 300 again, with 11 ticks, i.e [-100 0 100 200 300 400 -100 0 100 200 300]. The data resizes correctly, but the x-axis tick marks do not.

I'm using the following code to generate the plot:

heatmap = figure(2),subplot(1,15,2:15)

    imagesc(stretched_psth_avg, [minValue maxValue]);
    xlim([0, range([plottingVars.xLims(1) plottingVars.xLims(2)])])

    set(gca,'XTickLabel',round([linspace(plottingVars.xLims(1),...
               plottingVars.xLims(2),(plottingVars.numBins/20)+1)]))

    xlabel('ms')

    hold on
    cBarHandle = colorbar;
    set(gca, 'YTick', [],'YTickLabel', []); 
    set(gca, 'YTick', 1, 'Color', [0 0 0]);

    % Plot colors as image Y axis

    newColorBar = getColorBar;
    subplot(1,15,1)
    imagesc(newColorBar)
    set(gca, 'YTick', [],'YTickLabel', []); 
    set(gca, 'XTick', [],'XTickLabel', []); 
    ylabel('condition')
    hold off

These images are typically 133x101 in dimensions. The same thing is happening with my figures generated with the 'plot' command.

edit working code follows. I replaced the actual variables with their equivalents or random numbers, but it displays the same behavior.

heatmap = figure(2),subplot(1,15,2:15)
    newdata = rand(133,100);
     for i = 1:size(newdata,2)
        for x = i*5-4:(i*5)

        stretched_psth_avg(:,x) = newdata(:,i);
        end
    end
    imagesc(stretched_psth_avg, [min(min(newdata)) max(max(newdata))]);
    xlim([0, range([-100 400])])
    set(gca,'XLimMode','Manual')
    set(gca,'XTickLabel',round([linspace(-100,400,(100/20)+1)]))
    uicontrol('Style', 'text',...
   'String', sprintf('Least number of trials = %d',min([conditions.numTrials])),... 
   'Units','normalized',...
   'Position', [0.85 0.9 0.1 0.1]); 



    xlabel('ms')

    hold on

    cBarHandle = colorbar;

    set(gca, 'YTick', [],'YTickLabel', []); 
    set(gca, 'YTick', 1, 'Color', [0 0 0]);

    % Plot colors as image Y axis

    subplot(1,15,1)
    newcolorbar = rand(133,1);
    imagesc(newcolorbar)
    set(gca, 'YTick', [],'YTickLabel', []); 
    set(gca, 'XTick', [],'XTickLabel', []); 
    ylabel('condition')
    hold off
Was it helpful?

Solution

By setting the XTickLabel property, you also change the XTickLabelMode to manual, thus Matlab won't recalculate labels if the ticks change and just loops through the specified labels if it finds it needs more (as you've observed).

To prevent the ticks themselves from changing as well, either specify some values for XTick or just set XTickMode to manual directly - this will stop the tick positions from being recalculated, so they should stay in the right place and in sync with the labels.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top