To use multiple xtick methods in MATLAB plot

49 views (last 30 days)
I have a set of data where the y-axis is the same, but there are three distinct sets of data on the x-axis. I want to use multiple xtick methods in MATLAB to display my data. Here is an example of my data. How can I achieve having three ticks on the final plot in one figure? %%%%%%%%%%%%7/18 12:24%%%%%%%%%%%%%%%. What I want to change is to have three different sets of x values sharing the same x-axis. I have created a rough sketch using a clipboard to illustrate this. Despite the significant differences in numerical values among these three x sets, I would like them to be displayed together on a single graph by sharing the x-axis.
clear all clc ; clf
x1 = [1 2 3 4 5]
x2 = [0.1 0.2 0.3 0.4 0.5]
x3 = [10 20 30 40 50]
y = 1 :length(x1)
plot(x1 , y) ;hold on
plot(x2 , y)
plot(x3 , y).
  1 Comment
Dyuman Joshi
Dyuman Joshi on 17 Jul 2023
What is the final result you want to obtain? Can you provide an example?

Sign in to comment.

Accepted Answer

Chunru
Chunru on 18 Jul 2023
x1 = [1 2 3 4 5];
x2 = [0.1 0.2 0.3 0.4 0.5];
x3 = [10 20 30 40 50];
y = rand(3, 5); % make data different
%% Use multiple axes
ax1 = axes('Position', [0.13 0.2 0.775 0.715]);
plot(ax1, x1 , y(1,:), 'r');
ax2 = axes('Position', ax1.Position);
plot(ax2, x2 , y(2,:), 'g');
ax2.Color ='none';
ax2.YTick=[];
ax2.YTickLabel=[];
ax2.XRuler.TickLabelGapOffset=15; % change tick label location
ax3 = axes('Position', ax1.Position);
plot(ax3, x3 , y(3,:), 'b');
ax3.Color ='none';
ax3.YTick=[];
ax3.YTickLabel=[];
ax3.XRuler.TickLabelGapOffset=30;
linkaxes([ax1, ax2, ax3], 'y')

More Answers (1)

Harshal Ritwik
Harshal Ritwik on 17 Jul 2023
Hi,  
As per my understanding you would like to know how to add three ticks on the final plot. To add ticks to a plot, you can use the 'xticks' function. You need to create an array that consists of the x-axis points where you want to add ticks and pass the array as an argument to the 'xticks' function. To add labels to these points, you can use the 'xticklabels' function. The following code snippet may help you achieve this.
%Code Section
clear all;
clc;
y = 1 : 0.01:5;
x1 = sin(y);
x2 = sin(y)*100;
x3 = sin(y)/100;
plot(x1 , y) ;hold on;
plot(x2 , y);
plot(x3 , y);
xticks([sin(1.03)/100 sin(1.05) sin(1.03)*100]);
xticklabels({'Set 1', 'Set 2', 'Set 3'});
Please refer to the following documentation for more information.
I hope it helps! 
Thanks.
  1 Comment
peter huang
peter huang on 17 Jul 2023
What I want to change is to have three different sets of x values sharing the same x-axis. I have created a rough sketch using a clipboard to illustrate this. Despite the significant differences in numerical values among these three x sets, I would like them to be displayed together on a single graph by sharing the x-axis.
clear all clc ; clf
x1 = [1 2 3 4 5]
x2 = [0.1 0.2 0.3 0.4 0.5]
x3 = [10 20 30 40 50]
y = 1 :length(x1)
%%
plot(x1 , y) ;hold on
plot(x2 , y)
plot(x3 , y)

Sign in to comment.

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!