필터 지우기
필터 지우기

Vertical Line plot - change properties together

조회 수: 13 (최근 30일)
Mech Princess
Mech Princess 2014년 3월 25일
댓글: Steven Lord 2021년 10월 7일
Hi matlab comunity, I have a plot with 100 lines (using line command). Now, I want to change the color, line style and line width of all these 100 lines at once.
How I usually do this is by clicking on "Show plot tools and Dock Figure" and getting the "Plot Borwser" from the "Desktop" menu and selecting the line I want and change it in the "Property Editor". Really fast and easy. Only problem is that the 100 lines are being taken as individual plots instead of one entity. So now I need select it 100 times and do it.
Example code given below.
x=[10:10:200]; plot(sin(x),'b','LineWidth',2); hold on %plot data
SP = [1:2.5:20]'; %points to draw vertical line
yL = get(gca,'YLim'); line([SP SP],yL,'Color','k','LineWidth',0.5); %vertical line
legend('Data plot','vertical lines');
Is there a way I can select all vertical lines at once? (only vertical lines. not my data plot) Or can I change it in the command window AFTER I already plot it? (This is for a plot I already have. Don't have the data to re-plot)
Thanks

채택된 답변

Chad Greene
Chad Greene 2014년 3월 25일
Okay, this is somewhat clunky, but here it is. For the example you give in your original question, follow it with this line:
h = get(gca,'children');
Now you have handles of everything that's plotted on the current axes, and they're all in the variable called h. It looks like h(1) through h(8) are your vertical lines and h(9) is your plot data. so to change the properties of your vertical lines, try
set(h(1:8),'linewidth',5,'color','g')
  댓글 수: 1
Mech Princess
Mech Princess 2014년 3월 25일
Thanks. That works :) So basically, h(1:8) can be changed to h(1:16) if I have 16 lines (i.e. SP = [1:2.5:40]';)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Chad Greene
Chad Greene 2014년 3월 25일
When you plot the line, assign a handle to it like this:
h = line([SP SP],yL,'Color','k','LineWidth',0.5);
Then you can change its properties by
set(h,'color','m','linewidth',5)
  댓글 수: 1
Mech Princess
Mech Princess 2014년 3월 25일
Thanks. This is perfect for all future plots.
Any possibility for the one I have already plotted? (I dont have data point to re-plot figure)

댓글을 달려면 로그인하십시오.


Jean De Dieu Nshimiyimana
Jean De Dieu Nshimiyimana 2021년 10월 7일
편집: Jean De Dieu Nshimiyimana 2021년 10월 7일
Chad Greene , thank you very much for the super work! This helps me a lot:)
  댓글 수: 1
Steven Lord
Steven Lord 2021년 10월 7일
In the time since 2014 we introduced a function that you can use instead of calling plot or line with constant X data: xline.
x = 0:360;
y = sind(x);
h = plot(x, y, '-');
xline(0:90:360, 'r:')
There's also an option to change where the X axis line is drawn that may be useful.
ax = ancestor(h, 'axes'); % Get the axes containing the plot
ax.XAxisLocation = 'origin';
box off

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by