0
votes

Plot image

Plot using `set(gca, 'XTick', [1 10 20 50 100])

Plot with set(gca,'XTick',[1 10 20 50 100])

Hi everyone! I have created a graph with the function scatter and in the x-axis there are only three values shown: [1 10 100]. I'd like to add some values, specifically I'd like to show [1 5 10 20 50 100]. How can i do this?

My code is:

line(contrast2*100, RNorm2,'color','black');
hold on
scatter (contrast2*100, RNorm2,'y','filled');
set(gca,'XScale','log')
set(gca,'XTickLabel',num2str(get(gca,'XTick').'))
set(gca,'XTick',[1 10 20 50 100])
set(gca,'YScale','log')
set(gca,'YTickLabel',num2str(get(gca,'YTick').'))
grid on
1
Welcome to SO! What have you tried so far? Googling your title yields de.mathworks.com/help/matlab/creating_plots/… as the first result. HTH. - Matthias W.
Yes I tried with set(gca,'XTick',[1 10 20 50 100]) but it doesn't work in my graph :/ - Inis
@Inis can you include that line in your above example so we can see why it maybe didn't work? - Suever
@Suever I just include it and i have added also another image with the result :) - Inis

1 Answers

1
votes

You want to set your XTick values before you set your XTickLabels since you are constructing your XTickLabels from the values of the XTicks themselves.

What is currently happening is that you have 5 XTick values and only 3 labels. Because of this, MATLAB will repeat the labels that you have to populate labels for all XTick locations.

line(contrast2*100, RNorm2,'color','black');
hold on
scatter (contrast2*100, RNorm2,'y','filled');
set(gca,'XScale','log')
set(gca,'XTick',[1 10 20 50 100])
set(gca,'XTickLabel',num2str(get(gca,'XTick').'))
set(gca,'YScale','log')
set(gca,'YTickLabel',num2str(get(gca,'YTick').'))
grid on

Better yet, there is no real reason for you to be setting XTickLabel manually here. If you change the XTick locations, the labels will automatically be updated to reflect the new locations.

line(contrast2*100, RNorm2,'color','black');
hold on
scatter (contrast2*100, RNorm2,'y','filled');
set(gca, 'XScale', 'log', ...
         'XTick', [1 10 20 50 100], ...
         'YScale', 'log')