How to use Xlim for only one x-axis in yyaxis function?

6 views (last 30 days)
Hi! I have a question about how to use xlim in yyaxis function which limited to only one x-axis. I have two types of chart in one figure. They both have different x-axis and y-axis. One y-axis has a reversed value and I want its x-axis value limited to the other one. For example, x1 ranges from 01 November until 15 November, x2 ranges from 10 November until 23 November. I want y2 value here shown based on x1, so the data shown in figure for y2 should be from 10 November until 15 November (intersection). Therefore in my figure x-axis is x1 value. In my code below, x-axis value is the combination of both x1 and x2. I want to use Xlim for this purpose but I do not know how to do it.
I try to find the example in mathworks question but I could not find it. Could you please help me? Here is the code I wrote:
%%Plot Flow [l/min] and Cumulative Rainfall Depth [mm] (Figure 3)
yyaxis left
xlabel('Date and Time');
ylabel('Flow [l/min]');
yyaxis right
ax.YDir = 'reverse';
title('Flow and Rainfall');
ylabel('Rainfall [mm]');
grid on % show grid on plot
datacursormode on % enable to display data value interactively in the plot
clearvars ax x* y* z* % clear temporary var
Thank you very much for your help.

Answers (1)

Elizabeth Reese
Elizabeth Reese on 5 Dec 2017
Here is an example of how to do this. I used the min and max functions on the datetimes to establish where the intersect was. Another option would be to use the intersect function with the index outputs to only plot the necessary y1 and y2 values.
x1 = datetime(2017,11,1:15)
x2 = datetime(2017,11,10:23)
y1 = 1:length(x1);
y2 = (1:length(x2)).^10 % to create the different scaling
yyaxis left
yyaxis right
xlim([max(min(x1),min(x2)) min(max(x1),max(x2))])


Find more on Two y-axis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!