0
votes

I've been trying to get Matlab to change the labelling on my contourf plots for about an hour now. When I go to change XTickLabel or XTick, it simply removes my x-axis altogether! The frustrating and infuriating thing is that I'm doing exactly what all the help pages and help forums are asking me to do - I honestly don't understand why this is not working.

Hence, I am here.

My plotting code (knowledge of the function shouldn't be required - the code is rather intense. It is, however, a 2D contourf plot with valid data and ranges - the axes are the issue, not the graph):

   contourf(time,f,power,levels)
   colormap(jet(levels))
   set(gca,'XTickLabelMode','manual')
   set(gca, 'XTick', 0:23);
   set(gca, 'XTickLabel', {'0';'1';'23'});
   xlabel('Time (UT)')
   ylabel('Frequency (Hz)')
   caxis([0,8])
   axis([0 StopTime 0 0.1])

Any help would be greatly appreciated!

3
The plotting is or isn't the issue? ;) Also, it might be worth linking the help page you mentioned. - JoErNanO
mathworks.com/matlabcentral/answers/… mathworks.com.au/help/matlab/ref/axes-properties.html Neither were much help - as far as I know, I'm doing exactly what they're telling me to! And the graph itself isn't an issue - it's the axes that I need to change (specifically, the labels/ticks). - Yoshi
How are you trying to change XTickLabel or XTick ? And what do you intend to change it/them to? - Divakar
As far as I know (which isn't very far - I'm fairly new to this level of matlab coding), I am changing the axis handler settings of 'Xtick' and 'XTickLabel'. They are currently just holding the values of my arrays (which I don't want). I want to change my x-axis to UT time - from 0 through to 23, in equal increments (of 1). - Yoshi
Coulf you please provide us with some sample data for time, f, power, levels, for repeatability purposes? - JoErNanO

3 Answers

3
votes

Solved:

I realized that the 'XTick' relied on current values of the array I was using to define the x-axis. I can't just assume matlab will evenly space a new array (at least, if there's a way to do that, I don't know). So, since I have 85,680 data points on my X-axis, I simply rescaled it by:

   set(gca, 'XTick', 0:3570:85680)
   set(gca, 'XTickLabel', num2cell(0:24))

Moral of the story: Matlab doesn't let you arbitrarily stick a new axis over an old one using these two functions.

1
votes

You have a final axis([0 StopTime 0 0.1])) command which clears your plot, by creating a fresh new axis. That's why all your existing plots are gone. Try removing it:

contourf(time,f,power,levels)
colormap(jet(levels))
set(gca,'XTickLabelMode','manual')
set(gca, 'XTick', 0:23);
set(gca, 'XTickLabel', {'0';'1';'23'});
xlabel('Time (UT)')
ylabel('Frequency (Hz)')
caxis([0,8])

Now the question becomes: are your ticks sensibly placed for the data you are representing? Without knowing the data I cannot answer this for you. So the ball is in your court now. ;)

0
votes

You can use cell arrays to define the ticks and tick-labels and then use them with set function call, to make it more elegant -

xtick_label_cellarr = num2cell(0:24)
xtick_cellarr = linspace(0,85680,numel(xtick_label_cellarr))

set(gca, 'XTick',xtick_cellarr)
set(gca, 'XTickLabel',xtick_label_cellarr)