How to remove scientific notation from axes labels on log-log plot, and add thousands separator?

56 visualizzazioni (ultimi 30 giorni)
I am having difficulty making a plot with log-log axes where the axis labels are in fixed notation, not scientific notation, and commas are used as the thousands separator. After creating the plot and converting the axes to log scale, I was able to convert the axes to fixed notation using 3 methods:
1) Specifying the ticks
xtick = [1 10 100 1000];
xticks(xtick);
xticklabels(xtick);
2) xt = ax.XTick;
xt_sigfig = round(xt, 3, 'significant');
ax.XTickLabel = xt_sigfig;
3) using "compose" function
ax.XAxis.TickLabels = compose('%g', ax.XAxis.TickValues)
However, I was unable to add a thousands separator using any of those methods. For instance I would like the x tick labels to read: 1 10 100 and 1,000. And I would like the y tick labels to read: 1 10 100 1,000 10,000 and 100,000. My data and code are below using the "compose" function. Do I have to create tick labels that are strings? Any help would be greatly appreciated.
% X values
Q=[20.09 0.67 61.43 33.54 167.35 142.88 3.27 39.00 55.82 134.59 58.88 1.25 0.15 12.80 5.09 23.18 18.56 84.19 22.23 15.80 28.97 48.91 2.60 65.69 107.82]
Q = 1×25
20.0900 0.6700 61.4300 33.5400 167.3500 142.8800 3.2700 39.0000 55.8200 134.5900 58.8800 1.2500 0.1500 12.8000 5.0900 23.1800 18.5600 84.1900 22.2300 15.8000 28.9700 48.9100 2.6000 65.6900 107.8200
% Y values
S=[4190 60.10 56700 39900 82000 17700 26.40 12800 135000 20100 8420 7.14 39.40 329 253 5870 4270 23400 2320 5560 9210 107000 27.40 15700 17200]
S = 1×25
1.0e+05 * 0.0419 0.0006 0.5670 0.3990 0.8200 0.1770 0.0003 0.1280 1.3500 0.2010 0.0842 0.0001 0.0004 0.0033 0.0025 0.0587 0.0427 0.2340 0.0232 0.0556 0.0921 1.0700 0.0003 0.1570 0.1720
plot(Q,S,'o');
axis([0 1000 0 200000]);
set(gca,'XScale','log');
set(gca,'YScale','log');
ax=gca;
ax.XAxis.TickLabels = compose('%g', ax.XAxis.TickValues)
ax =
Axes with properties: XLim: [0 1000] YLim: [0 200000] XScale: 'log' YScale: 'log' GridLineStyle: '-' Position: [0.1300 0.1100 0.7750 0.8150] Units: 'normalized' Show all properties
ax.YAxis.TickLabels = compose('%g', ax.YAxis.TickValues)
ax =
Axes with properties: XLim: [0 1000] YLim: [0 200000] XScale: 'log' YScale: 'log' GridLineStyle: '-' Position: [0.1300 0.1100 0.7750 0.8150] Units: 'normalized' Show all properties

Risposta accettata

Walter Roberson
Walter Roberson il 28 Giu 2022
Modificato: Walter Roberson il 28 Giu 2022
Although you can set ax.xAxis.TickLabelFormat and ax.XAxis.Exponent none of the automatic formats support thousands separator. You will need to set the tick labels.
And if you permit zoom or pan, you will need to add listeners or put in something like a ResizeFcn Callback
https://www.mathworks.com/matlabcentral/answers/8080-how-to-set-1000-separator-for-large-numbers-in-gui-matlab
  5 Commenti
David Dean
David Dean il 30 Giu 2022
Yes Stephen, you are correct. The limitation is not documented and could be helpful to state as such in the Mathworks literature. Even better, it would be nice if Matlab was able to build the functionality into plots with log axes!
Walter Roberson
Walter Roberson il 30 Giu 2022
Odd...
@Stephen23 did you just happen to notice in the xtickformat information that it gives the example that %.4d format displays pi as 0003 ? That is not the same as fprintf()
fprintf('%.4d\n', pi)
3.1416e+00
but the xtickformat information appears to be correct for it
plot(rand(1,5))
xticks(pi)
xtickformat('%.4d')

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by