How to disable and enable plot lines from the figure window?
조회 수: 133 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2022년 11월 23일
답변: MathWorks Support Team
2023년 1월 24일
I often have multiple lines in one axis as I would like to compare them with each other. However, this decreases the readability of the graph when I want to focus on only some of the data.
How can I disable and enable plot lines from the figure window?
채택된 답변
MathWorks Support Team
2022년 11월 23일
There are two methods to achieve this:
Using the Plot Browser tool:
You can activate and deactivate the plot lines with the Plot Browser tool. To show the Plot Browser interface, please use the following command in your code:
plotbrowser('on')
You can also access the tool from the figure's menu bar by clicking "View -> Plot Browser".
Using the legend object:
Alternatively, it is possible to enable and disable plot lines using the figure's legend. It requires implementing a custom callback function in the "ItemHitFcn" property of the "legend" object. You can implement the functionality to enable and disable plot lines by following the steps below:
Step 1:
Open the default "ItemHitFcn" callback function by running the following command in the MATLAB Command Window:
>> edit defaultItemHitCallback
Step 2:
Editing the default MATLAB functions is not recommended. Therefore, copy the contents of the "defaultItemHitCallback.m" function into a new script and close "defaultItemHitCallback.m" without saving any changes.
Step 3:
Edit the first line of the new script file to:
function myItemHitCallback(hSrc,eventData)
Step 4:
Add the following code at the bottom of the file before the last "end":
if strcmp(eventData.SelectionType, 'normal') && strcmp(eventData.Region, 'icon')
eventData.Peer.Visible = ~eventData.Peer.Visible;
end
This code will switch the visibility of the relevant plot line when a line icon in the legend is clicked.
Step 5:
Save the new file in your MATLAB working directory as "myItemHitCallback.m".
Step 6:
When creating a legend for your plot, attach the new callback function by adding the following line:
legHandle = legend; %Example legend definition
legHandle.ItemHitFcn = @myItemHitCallback;
Step 7:
In the created figure, you should now be able to disable and enable the visibility of the plot line by clicking on the line icon in the legend.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!