Effacer les filtres
Effacer les filtres

Problem with setting axis ticks

40 vues (au cours des 30 derniers jours)
Wenlong
Wenlong le 13 Juil 2012
Dear all,
I am using plot to visualize my data, here is my code
set1 = [65 42 31 23 20];
set2 = [63 40 28 19.5 17];
set3 = [42 27 18 13.5 12];
hold on
plot(set1, 'bo:')
plot(set2, 'rs:')
plot(set3, 'kd:')
x = 10:10:50;
y = 10:10:70;
set(gca, 'XTick', x);
set(gca, 'YTick', y);
The problem is, there are ticks only on the Y-axis shown on the plot, the ticks on the X-axis ticks can not be seen.
May I know what is wrong with my code?
Many thanks in advance.
Best wishes Wenlong

Réponses (1)

Image Analyst
Image Analyst le 13 Juil 2012
Modifié(e) : Image Analyst le 14 Juil 2012
Your independent values (the indexes of y) only go from 1 to 5. Your first tick mark is at 10 so it is off the far right end of your plot and can't be seen. To prove it, and see the ticks, put this line at the end of your code:
xlim([0 70]);
To fix, and make somewhat more robust and versatile, you might try it like this:
clc;
fontSize = 20;
xTickLocations = 10:10:50;
yTickLocations = 10:10:70;
numberOfXTickMarks = length(xTickLocations);
% Generate sample data. 3 sets of Y data
% all of the same length.
set1 = [65 42 31 23 20];
set2 = [63 40 28 19.5 17];
set3 = [42 27 18 13.5 12];
xValues = 1 : length(set1);
% Now plot all three sets of y data.
hold on;
plot(xValues, set1, 'bo-', 'LineWidth', 3);
plot(xValues, set2, 'rs:', 'LineWidth', 3);
plot(xValues, set3, 'kd-', 'LineWidth', 3);
grid on;
legend('Set 1', 'Set 2', 'Set 3');
xlabel('X', 'fontSize', fontSize);
ylabel('Y', 'fontSize', fontSize);
title('Plot of Three Sets', 'fontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Ask user if they want to switch tick marks.
promptMessage = sprintf('Do you replace the default X tick\nlocations and labels with ours?');
button = questdlg(promptMessage, 'Replace Tick Marks', 'OK', 'Cancel', 'OK');
if strcmp(button, 'Cancel')
return;
end
% Replace default tick locations with ours
for k = 1 : length(xTickLocations);
xTickString{k} = xTickLocations(k);
end
% Make new tick marks.
xl = xlim(); % Get existing range.
% Make 5 tick marks. (Existing # of tick marks may not = 5)
xTickLocations = linspace(xl(1), xl(2), numberOfXTickMarks)
set(gca,'XTick', xTickLocations);
% Make new labels for the new tick marks
set(gca,'XTickLabel', xTickString);

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by