Matlab中的XTicks不完整标签如何具有非零符号? [英] How to Have Non-Zero Symbol for Incomplete Labels of XTicks in Matlab?

查看:149
本文介绍了Matlab中的XTicks不完整标签如何具有非零符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这样的问题:当x轴变大时,Matlab 2015b在线程

无法动态扩展xticks的不完整标签,因为总是存在空间不足的情况,但是只需要一个符号即可标记两个值之间的一半. 这种情况对于零点是有问题的,因为我有几个校准点和几个多余零点容易出错的系统. 我想在那里再有一个符号.

示例代码如何创建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!

没有xticks的不完整标签,但标签范围更广,情况更糟

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);

Suever的 answer

的输出

美丽,小窗口具有科学编号,因为其后的代码末尾是callback();,因此具有科学编号

中等窗口

代码

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


在Matlab中,如何为xticks的不完整标签添加另一个符号?

解决方案

正如我在另一个问题中所述,如果您希望在调整大小时自动更新标签,则需要执行以下操作.

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();

更改图形大小时,标签将自动更改,位置也会更新.

小窗口

中型窗口

大窗口

注意:单独测试此代码以验证其是否有效,然后将其应用于您的解决方案.

看来,由于命名空间被污染(例如,您的示例甚至没有运行,因为未定义labels),您似乎将面临很多麻烦.

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?

解决方案

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).

这篇关于Matlab中的XTicks不完整标签如何具有非零符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆