抑制数字刻度中的指数格式

发布于 2024-09-16 04:46:56 字数 406 浏览 3 评论 0原文

对于大于约 10'000 的刻度的刻度标签,例如将格式设置为 1x10^4。而指数部分出现在相应轴的上方。这种不当行为在 matlabcentral 上也得到了很好的描述,但没有解决方案。

感谢您的帮助。


当应用于 bar3 时, “快速技巧”

set(gca, 'YTickLabel',get(gca,'YTick'))

不起作用,如下图所示。

bar3 绘图失败

Tick labels for ticks bigger than about 10'000, get formatted to 1x10^4 for example. Whereas the exponential part appears above the corresponding axes. This misbehavior has been well described on on matlab central as well, but without a solution.

Thanks for your help.


The 'quick trick'

set(gca, 'YTickLabel',get(gca,'YTick'))

did not work when applied to bar3, as can be seen on the following figure.

bar3 plot failing

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

漫漫岁月 2024-09-23 04:46:56

编辑

根据此技术解决方案页面,格式化刻度标签的推荐方法是这样的(您可以使用任何数字格式化函数,如 NUM2STR、SPRINTF、MAT2STR 或任何其他..)

y = cool(7);
bar(y(:,1)*1e6)
set(gca, 'YTickMode','manual')
set(gca, 'YTickLabel',num2str(get(gca,'YTick')'))

alt text

但是,在 Z 轴方面似乎存在错误(标签格式正确,但由于某种原因指数乘数仍然显示!)

y = cool(7);
bar3(y*1e6, 'detached')
set(gca, 'ZTickMode','manual')
set(gca, 'ZTickLabel',num2str(get(gca,'ZTick')'))

alt text

最后,还有另一种解决方法,我们用文本对象替换刻度标签(请参阅此 技术解决方案页面作为参考):

y = cool(7);
bar3(y*1e6, 'detached')
offset = 0.25; Xl=get(gca,'XLim'); Yl=get(gca,'YLim'); Zt=get(gca,'ZTick');
t = text(Xl(ones(size(Zt))),Yl(ones(size(Zt)))-offset,Zt, num2str(Zt')); %#'
set(t, 'HorizontalAlignment','right', 'VerticalAlignment','Middle')
set(gca, 'ZTickLabel','')

替代文本

EDIT

According to this technical solution page, the recommended way of formatting the tick labels is this (you can use any of the number formatting functions like NUM2STR, SPRINTF, MAT2STR, or any other..)

y = cool(7);
bar(y(:,1)*1e6)
set(gca, 'YTickMode','manual')
set(gca, 'YTickLabel',num2str(get(gca,'YTick')'))

alt text

However there seems to be a bug when it comes to the Z-axis (the labels are correctly formatted, but the exponential multiplier is still showing for some reason!)

y = cool(7);
bar3(y*1e6, 'detached')
set(gca, 'ZTickMode','manual')
set(gca, 'ZTickLabel',num2str(get(gca,'ZTick')'))

alt text

Finally, there's another workaround where we replace the tick labels with text objects (see this technical solution page as reference):

y = cool(7);
bar3(y*1e6, 'detached')
offset = 0.25; Xl=get(gca,'XLim'); Yl=get(gca,'YLim'); Zt=get(gca,'ZTick');
t = text(Xl(ones(size(Zt))),Yl(ones(size(Zt)))-offset,Zt, num2str(Zt')); %#'
set(t, 'HorizontalAlignment','right', 'VerticalAlignment','Middle')
set(gca, 'ZTickLabel','')

alt text

烟凡古楼 2024-09-23 04:46:56

您可以尝试的另一个技巧是在绘制数据之前缩放数据,然后缩放刻度标签以使其看起来像是以不同的比例绘制的。您可以使用函数 LOG10 自动为您提供帮助根据绘制的值计算适当的比例因子。假设您的数据位于变量 xy 中,您可以尝试以下操作:

scale = 10^floor(log10(max(y)));  %# Compute a scaling factor
plot(x,y./scale);                 %# Plot the scaled data
yTicks = get(gca,'YTick');        %# Get the current tick values
set(gca,'YTickLabel',num2str(scale.*yTicks(:),'%.2f'));  %# Change the labels

One other trick you can try is to scale your data before you plot it, then scale the tick labels to make it appear that it is plotted on a different scale. You can use the function LOG10 to help you automatically compute an appropriate scale factor based on your plotted values. Assuming you have your data in variables x and y, you can try this:

scale = 10^floor(log10(max(y)));  %# Compute a scaling factor
plot(x,y./scale);                 %# Plot the scaled data
yTicks = get(gca,'YTick');        %# Get the current tick values
set(gca,'YTickLabel',num2str(scale.*yTicks(:),'%.2f'));  %# Change the labels
So尛奶瓶 2024-09-23 04:46:56

更好地控制刻度标签并避免指数格式的一种方法是使用 TICK2TEXT 来自文件交换。

这是一个例子:

y = cool(7); %# define some data
ah = axes; %# create new axes and remember handle
bar3(ah,y*1E6,'detached'); %# create a 3D bar plot
tick2text(ah, 'ztickoffset' ,-1.15,'zformat', '%5.0f', 'axis','z') %# fix the tick labels

One way to get better control over tick labels, and to avoid the exponential formatting, is to use TICK2TEXT from the File Exchange.

Here's an example:

y = cool(7); %# define some data
ah = axes; %# create new axes and remember handle
bar3(ah,y*1E6,'detached'); %# create a 3D bar plot
tick2text(ah, 'ztickoffset' ,-1.15,'zformat', '%5.0f', 'axis','z') %# fix the tick labels
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文