How to speed up xline / yline ?

10 ビュー (過去 30 日間)
Matthias Fath
Matthias Fath 2024 年 4 月 12 日
回答済み: Matthias Fath 2024 年 4 月 12 日
I want to plot several xlines in a simple 2D plot at dedicated x-values. When i plot many lines (e.g.) 200 at once using xline, the response is very slow. I am passing all x-Values as a vector at once to xline. When enabling the grid with same x-spacing, it is ~ 80 times faster. Any idea how to speed up the xline command? I already tried disabling the lines transparency by setting alpha = 1, but it didn't help.
Regards,
Matthias

採用された回答

Manikanta Aditya
Manikanta Aditya 2024 年 4 月 12 日
Hi,
The xline and yline functions in MATLAB are designed for convenience and ease of use, but they may not be the most efficient for plotting a large number of lines. When you’re dealing with a large number of lines, it might be more efficient to use the line or plot functions instead.
Here’s an example of how you might use the line function to plot vertical lines (similar to xline):
x_values = 1:200;
for x = x_values
line([x x], ylim);
end
This code will create a vertical line at each x-value in x_values. The ylim function is used to automatically adjust the y-coordinates of the line to the current y-axis limits.
Hope this helps, let me know.

その他の回答 (1 件)

Matthias Fath
Matthias Fath 2024 年 4 月 12 日
Thanks for your help. The "line" command is a bit faster. After implementing some additonal required formating a part of the speed advantage disapears. Using ylim was also slowing down a bit, but i can avoid that since i have fixed (maximum) axislimits anyway. In the end it's ~25% faster than xline in my testcases.
Regards,
Matthias

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by