Inis
Inis

Reputation: 3

Matlab how to add values in the x-axis of a plot

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

Upvotes: 0

Views: 600

Answers (1)

Suever
Suever

Reputation: 65430

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')

Upvotes: 1

Related Questions