How to Have Non-Zero Symbol for Incomplete Labels of XTicks in Matlab?

99封情书 提交于 2019-11-28 10:21:12

问题


I run into the problem where Matlab 2015b expands the labels of new Xticks when the x-axis gets bigger by using incomplete label, zeros, in the thread No Gap Next to Axis Label in Matlab?

The dynamic expansion of incomplete labels of xticks is not possible because there is always cases of insufficient space but only one symbol is needed to mark half between two values. The situation is problematic with zeros because I have several calibration points and several systems where the extra zeros are errorprone. I would like to have there another symbol.

Example code how to create those incomplete labels of xticks

labels = arrayfun(@(x)sprintf('%.2g', x), xticks, 'uniform', 0);
ax2 = axes('OuterPosition', [0.51 0.5 0.5 0.5]); % anything here
xticks = get(ax2, 'xtick'); % https://stackoverflow.com/a/35776785/54964
set(ax2, 'xticklabels', labels); % here the point!

Without those incomplete labels of xticks but broader labelling which is worser

labels = arrayfun(@(x)sprintf('%.2g', x), xticks, 'uniform', 0);
ax2 = axes('OuterPosition', [0.51 0.5 0.5 0.5]);
xticks = get(ax2, 'xtick'); % https://stackoverflow.com/a/35776785/54964
set(ax2, 'xtick', xticks, 'xticklabels', labels);

Output of Suever's answer

Beautiful Small window in the original size with scientific numbering because of callback(); at the end of the code following

Medium window

Code

hFig=figure;
data=randi(513,513);
D=mat2gray(pdist(data, 'correlation'));

ax2 = axes('OuterPosition', [0.51 0.5 0.5 0.5]);
plot(D, 'Parent', ax2);
axis(ax2, 'square');
title('Corr pdist');
cbar2 = colorbar(); 
set(ax2, 'XLim', [0 size(D,2)]);
set(cbar2, 'Visible', 'off')
grid minor;
labelconverter = @(x)sprintf('%.2g', x); % https://stackoverflow.com/a/35780915/54964
callback = @(varargin)set(ax2, 'xticklabels', arrayfun(labelconverter, get(ax2, 'xtick'), 'uniform', 0));
set(hFig, 'SizeChangedFcn', callback);
callback(); % necessary for small window

How can you have another symbol for the incomplete labels of xticks in Matlab?


回答1:


As I said in the other question, if you want the labels to be updated automatically when you resize things, you'll want to do the following.

fig = figure;

% Set large xlimits to demonstrate the issue at hand
ax2 = axes('xlim', [0 1e9]);

% Force a draw event to have the axes determine where the
labelconverter = @(x)sprintf('%.2g', x);
callback = @(varargin)set(ax2, 'xticklabels', arrayfun(labelconverter, get(ax2, 'xtick'), 'uniform', 0));

set(fig, 'SizeChangedFcn', callback);

% Be sure to execute the callback to get new labels prior to figure resize.
callback();

As you change the size of your figure, the labels will be changed automatically and the positions will be updated.

Small Window

Medium Window

Large Window

Note: Test this code in isolation to verify that it works, then adapt the idea to your solution. It seems like you're ending up with a lot of complications because your namespace is polluted (for example your examples don't even run because labels isn't defined).



来源:https://stackoverflow.com/questions/35779680/how-to-have-non-zero-symbol-for-incomplete-labels-of-xticks-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!