My plot does not show anything
Show older comments
I'm trying to plot a function but I don't get lines in it.
delta = pi / 100;
x1 = -pi + delta : delta : 0 - delta;
x2 = 0 + delta : delta : pi - delta;
x3 = -pi : delta : pi;
hold on
plot(x1, delta, '-b', 'LineWidth', 2), xlabel('x'), ylabel('y')
plot(x2, delta, '-r', 'LineWidth', 2), xlabel('x'), ylabel('y')
plot(x3, delta, '-y', 'LineWidth', 2), xlabel('x'), ylabel('y')
hold off
legend('-pi + delta : delta : 0 - delta', '0 + delta : delta : pi - delta', '-pi : delta : pi')
box on
grid on
Answers (2)
John D'Errico
on 22 Feb 2019
Edited: John D'Errico
on 22 Feb 2019
delta is a scalar.
plot does not work well when one of the arguments is a scalar. I suppose some might call if a feature that it works partially at all. For example
This works:
plot(1:5,1,'o')
but this gives you just white.
plot(1:5,1,'-')
And this plots the symbols, but no connecting line.
plot(1:5,1,'-o')
As I said, the ability of plot to do anything at all when x and y are different sizes is a feature. It should arguably return an error in my opinion. My guess is this has been in MATLAB for so long that the capability has been left untouched, a grandfathered feature. There are probably some people who use that capability in legacy code.
Anyway, an easy fix is:
delta = pi / 100;
x1 = -pi + delta : delta : 0 - delta;
x2 = 0 + delta : delta : pi - delta;
x3 = -pi : delta : pi;
hold on
plot(x1, delta*ones(size(x1)), '-b', 'LineWidth', 2), xlabel('x'), ylabel('y')
plot(x2, delta*ones(size(x2)), '-r', 'LineWidth', 2), xlabel('x'), ylabel('y')
plot(x3, delta*ones(size(x3)), '-y', 'LineWidth', 2), xlabel('x'), ylabel('y')
hold off
legend('-pi + delta : delta : 0 - delta', '0 + delta : delta : pi - delta', '-pi : delta : pi')
box on
grid on
1 Comment
Khristopher Macedo
on 22 Feb 2019
Yukun
on 22 Feb 2019
0 votes
So, firstly, can you describe your purpose of writing the code? What do you want to achieve?
And as you can see, "delta" is a scalar but x1,x2,x3 are vectors. I don't think it is proper.
Categories
Find more on Exploration and Visualization 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!