How to plot (bar) a negative data in the first quadrant?

6 views (last 30 days)
I have a set of data similar to the frequency spectrum. I want to plot that in MATLAB. The problem is that the magnitude (y) of data is a combination of negative and positives values. I want to plot it on the Y-axis as if it was positive, so the plot remains on the first quadrant, and I also like that the values show as negative and positive.
I tried following functions:
x = 0:1:10; %% x-axis data
y = [-1 -2 -4 -5 0 3 1 9 -2 -8 -1.5];
  1. bar(x,y); %% But it plots negative value magnitude in the fourth quadrant.
  2. stem (x,y); %% Plots negative values in the fourth quadrant.
  3. scatter(x,y); %% It will plot in the first quadrant but only shows the marker. I want a similar graph with a line joining to the x-axis.
  4. plot(x,y); %% doesn't work for me as I want spikes of magnitude (on y-axis) on particular values of x.
But, all of the above functions do not work because either it plots the negative values on the fourth quadrant or just giving discrete values without a line in the first quadrant. I want the graph in the first quadrant (above the x-axis) of the data.
Any help on how to do this? I have searched documentation and forums, and I couldn't find it.
  2 Comments
Adam Danz
Adam Danz on 2 May 2021
Why not just apply abs() to the variable that you want to be positive?
Vijender Kumar Sharma
Vijender Kumar Sharma on 2 May 2021
Thanks Mr Adam Danz for your answer. However, abs() function only gives positive values. I need both negative and postive values on y-axis and that should be above x-axis.

Sign in to comment.

Accepted Answer

Scott MacKenzie
Scott MacKenzie on 2 May 2021
The "wrong information" was in your question, as posted. I'll admit that my original solution was a bit odd, but it faithfully rendered what you described (which I thought was a bit odd in the first place). If you had posted the figure in your original question and/or provided a clear description of the goal, we could have saved some back-and-forth commenting. Anyway, not to worry. I think is what you are looking for is below. All you need to do is set the BaseValue property of the stem (or bar) graph. Good luck.
% your test data
x = 0:1:10;
y = [-1 -2 -4 -5 0 3 1 9 -2 -8 -1.5];
h = bar(x,y);
h.BaseValue = -10;
h.LineWidth = 2;
h.Color = 'k';
  1 Comment
Vijender Kumar Sharma
Vijender Kumar Sharma on 2 May 2021
Edited: Vijender Kumar Sharma on 2 May 2021
Thanks for the answer. My problem is solved by shifting the base value. Yes, I want to shift the axis.
Sorry, for my wrongly posted question if you misunderstood that.

Sign in to comment.

More Answers (1)

Scott MacKenzie
Scott MacKenzie on 1 May 2021
How about this:
% your test data
x = 0:1:10;
y = [-1 -2 -4 -5 0 3 1 9 -2 -8 -1.5];
% determine which values of y are negative
yNegative = y < 0;
% use custom colors (different color for negative y)
colorPos = [.6 .8 .9];
colorNeg = [.65 .9 .95];
clr = repmat(colorPos, length(y), 1);
clr(yNegative,1:3) = repmat(colorNeg,sum(yNegative),1);
% make all y values positive
y1 = abs(y);
% create the bar chart (all bars in 1st quadrant)
b = bar(x, y1, 'facecolor', 'flat');
ax = gca;
ax.YLim = [0 max(y)+1];
% set bar colors to custom colors (negative y in different color)
b.CData = clr;
% put data values above bars (showing negative values for y)
xn = b.XData;
yn = b.YEndPoints + 0.4;
s = string(y);
text(xn, yn, s, 'color', [.2 .2 .2], 'fontsize', 12, 'horizontalalignment', 'center');
  1 Comment
Vijender Kumar Sharma
Vijender Kumar Sharma on 2 May 2021
Thanks Mr Scott MacKenzie for your answer. However, I need the negative and positive values on Y-axis. Your posted code giving the wrong information. For an example, -1>-5, but in your code the amplitude of -5 is showing higher than -1. For your reference, I am attaching a figure with this message. I need similar type of plot.

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots 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!