Matlab and XTickLabel

前提是你 提交于 2019-12-04 06:49:34

问题


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!


回答1:


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.




回答2:


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. ;)




回答3:


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)


来源:https://stackoverflow.com/questions/26732035/matlab-and-xticklabel

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