I want to color for $\eta<1.054288$ by blue and red for, $\eta>1.054288$ of a curve from data
조회 수: 3 (최근 30일)
이전 댓글 표시
I want to use two colors of a single curve obtained from the data. I want to make the curve color
- blue for $0.608130<\eta<1.054288$ in the upper portion of the curve only
- red for $\eta>1.054288$ in the upper portion of the curve only
- blue for $\eta>0.608130$ i.e the total lower portion of the curve only
How to change the color of the curve accordingly.
clear all
format long
set(0,'DefaultAxesFontSize',20);
figure;
load('EP_EP(1).mat')
plot(x(3,:),x(1,:),'b', 'LineWidth',2)
hold on
load('EP_EP(2).mat')
plot(x(3,:),x(1,:),'b', 'LineWidth',2)
xlabel('$\eta\rightarrow$','FontSize',20,'interpreter','latex')
ylabel('$x\rightarrow$','FontSize',20,'interpreter','latex')
xline(1.054288,'k', 'LineWidth',2)
xline(0.608130,'k', 'LineWidth',2);
axis([.4 1.5 0 .6]);
댓글 수: 0
채택된 답변
Star Strider
2023년 5월 14일
편집: Star Strider
2023년 5월 14일
This is a bit more involved than it might otherwise seem, because simply using logical indexing to define the red line creates a ‘bridging’ red line at the threshold value. Creating ‘v1’ and ‘v2’ avoids that problem, and provides (what I believe to be) the desired result.
Try this —
clear all
format long
set(0,'DefaultAxesFontSize',20);
figure;
LD1 = load('EP_EP(1).mat');
x1 = LD1.x;
LD2 = load('EP_EP(2).mat');
x2 = LD2.x;
thld = 1.054288;
x1b = x1(3,:) < thld;
ixt = find(diff(sign(x1(3,:)-thld)));
v1 = 1:ixt(1);
v2 = ixt(2):numel(x1(3,:));
x2b = x2(3,:) < thld;
figure
plot(x1(3,x1b),x1(1,x1b),'b', 'LineWidth',2)
hold on
plot(x2(3,x2b),x2(1,x2b),'b', 'LineWidth',2)
plot(x1(3,v1),x1(1,v1),'r', 'LineWidth',2)
plot(x1(3,v2),x1(1,v2),'b', 'LineWidth',2)
plot(x2(3,~x2b),x2(1,~x2b),'r', 'LineWidth',2)
xlabel('$\eta\rightarrow$','FontSize',20,'interpreter','latex')
ylabel('$x\rightarrow$','FontSize',20,'interpreter','latex')
xline(1.054288,'k', 'LineWidth',2)
xline(0.608130,'k', 'LineWidth',2);
axis([.4 1.5 0 .6]);
EDIT — (14 May 2023 at 16:18)
Plot changed following posted clarification.
.
댓글 수: 5
Star Strider
2023년 5월 15일
I thought I explained that earlier. I apparently wasn’t clear.
That finds the approximate indices of the points where ‘x1(3,:)’ crosses the ‘thld’ (threshold) value. There are two, and those values are then used to define index vectors ‘v1’ and ‘v2’ that define the two desired segments of ‘x1’.
I initially created them to avoid the ‘bridging’ red line that otherwise parallels the first yline call, however after I read the clarification, using them to create the desired plot only involved changing the colour of the part of the line that ‘v2’ indexes to blue.
추가 답변 (1개)
Shaik
2023년 5월 14일
Hi Atom,
To change the color of the curve based on specific conditions, you can use the plot function multiple times with different segments of the data and different colors. Here's an updated code snippet that demonstrates how to achieve this:
clear all
format long
set(0,'DefaultAxesFontSize',20);
figure;
load('EP_EP(1).mat')
xdata = x(3,:);
ydata = x(1,:);
% Plot the upper portion of the curve in blue for 0.608130 < eta < 1.054288
upper_blue_indices = xdata > 0.608130 & xdata < 1.054288;
plot(xdata(upper_blue_indices), ydata(upper_blue_indices), 'b', 'LineWidth', 2)
hold on
% Plot the upper portion of the curve in red for eta > 1.054288
upper_red_indices = xdata > 1.054288;
plot(xdata(upper_red_indices), ydata(upper_red_indices), 'r', 'LineWidth', 2)
% Plot the lower portion of the curve in blue for eta > 0.608130
lower_blue_indices = xdata <= 0.608130;
plot(xdata(lower_blue_indices), ydata(lower_blue_indices), 'b', 'LineWidth', 2)
xlabel('$\eta\rightarrow$','FontSize',20,'interpreter','latex')
ylabel('$x\rightarrow$','FontSize',20,'interpreter','latex')
xline(1.054288, 'k', 'LineWidth', 2)
xline(0.608130, 'k', 'LineWidth', 2);
axis([.4 1.5 0 .6]);
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!