Suppress exponential formatting in figure ticks

半城伤御伤魂 提交于 2019-11-26 08:26:13

问题


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


回答1:


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



回答2:


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

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

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','')




回答3:


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


来源:https://stackoverflow.com/questions/3588293/suppress-exponential-formatting-in-figure-ticks

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