Label 0 in a semilog plot

25 visualizzazioni (ultimi 30 giorni)
Gianluca Regina
Gianluca Regina il 16 Feb 2022
Commentato: Gianluca Regina il 17 Feb 2022
Dear MatLab users,
I have some data I want to plot, and the x-axis values go from 0 to 1. However, I need to plot them in a semilog plot, and for obvious mathematical reasons I cannot plot the 0, despite being a value of my data. So my question is: can I somehow add a label on the plot or on the x axis? I attached a picture which shows two possible solutions (the left one would be ideal). Below is my code for the first part of the plot, but I am utterly clueless on how to do something like that in the picture. I can use XTickLabel and write 0 but I would like to leave that as a last resort.
x = [ 0.01 , 0.05 , 0.1 ] ; % My x axis. 0.01 in reality is 0.
a = [ rand(1) , rand(1) , rand(1) ] ; % My data
b = [ rand(1) , rand(1) , rand(1) ] ; % My data
figure
errorbar( x , a , b ,'o' , 'Color' , 'k' , 'MarkerSize' ,10 , 'Capsize' , 10 , 'linewidth' , 2 , 'MarkerEdgeColor','k','MarkerFaceColor','k' );
set(gca,'xscale','log')
axis([0.01 1 -2 2])
set(gca ,'Fontsize',20,'FontWeight','bold', 'Box','on')
set(gcf , 'color','w' , 'WindowState' , 'Maximize')
grid on

Risposta accettata

Dave B
Dave B il 16 Feb 2022
You can fake the tick labels, but setting the TickLabels property, but another way to do this is to use 2 axes. I like doing this kind of thing (broken axes) with tiledlayout. Note that I don't have lines coming out to 0, but that's because those lines are a little strange given the infinite space between 0 and 0.05:
rng(pi) %just for reproducibility of the random numbers
x = [ 0, 0.05 , 0.1 ];
a = rand(1,3);
b = rand(1,3);
ind0 = x==0;
t=tiledlayout(1,6);
nexttile(t)
errorbar( x(ind0) , a(ind0) , b(ind0) ,'o' , 'Color' , 'k' , 'MarkerSize' ,10 , 'Capsize' , 10 , 'linewidth' , 2 , 'MarkerEdgeColor','k','MarkerFaceColor','k' );
ax1=gca;
set(ax1,'Fontsize',20,'FontWeight','bold', 'Box','on')
nexttile([1 5])
errorbar( x(~ind0) , a(~ind0) , b(~ind0) ,'-o' , 'Color' , 'k' , 'MarkerSize' ,10 , 'Capsize' , 10 , 'linewidth' , 2 , 'MarkerEdgeColor','k','MarkerFaceColor','k' );
ax2=gca;
set(ax2,'xscale','log')
axis([0.01 1 -2 2])
set(gca,'Fontsize',20,'FontWeight','bold', 'Box','on','YTickLabel',[])
linkaxes([ax1 ax2],'y')
grid([ax1 ax2],'on')
  3 Commenti
Dave B
Dave B il 17 Feb 2022
In general the tile's size is proportional to the width of the layout...you can fix the width of the layout but not of the relative width of the tile to the layout (in my code snippet is was 1/6 because the layout was 6 tiles wide and the left axes had a span of 1 tile).
There's actually, semi-coincidentally, a nice little workaround that will prevent the left axes from getting too big. TiledChartLayout objects have 'outer tiles' which do scale but are clamped so they never get too big. Here's how to use them (I'm skipping the plotting bits, they'll be the same as what you had before, but here I named the axes mainax and sideax instead of ax1 and ax2...just for some clarity):
t=tiledlayout(1,1);
mainax=nexttile;
sideax=axes('Parent',t);
sideax.Layout.Tile='west';
The alternative would be to manage your own layout (i.e. create axes and set positions and skip the TiledChartLayout), but that can be pretty painful...it's why we created tiledlayout and nexttile!
Let me know if that helps, I think I sortof got your issue but I might have missed the mark. Also you might find exportgraphics/copygraphics give you better results than screenshots.
Gianluca Regina
Gianluca Regina il 17 Feb 2022
I managed to use those commands but the plot became a bit messy and the width reduction was not worth it. Still, I can simply choose the width I desire by manually adjusting the windows figure screen. Thank you either way, and for the exportgraphics command information, though I typically use print.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by