Rodrigo Morante
Rodrigo Morante

Reputation: 103

Octave, barplot, only one row

I need to plot a bar graph in Octave. The following code:

clf;

data = rand(4, 5);

h1 = bar(data(1, :), "stacked");
l1 = legend("Col1", "Col2", "Col3", "Col4", "Col5");
legend(l1, "location", "northeastoutside");

figure
h4 = bar(data, "stacked");
l4 = legend("Col1", "Col2", "Col3", "Col4", "Col5");
legend(l4, "location", "northeastoutside");

does it. The second plot produces exactly what I need: enter image description here

The first one, however, does not work as I would assume it would:

enter image description here

Is there any way to have a one-row bar plot that produces a graph with only one column in the style of the first plot?

Thanks in advance.

Rodrigo

Upvotes: 0

Views: 506

Answers (1)

marolafm
marolafm

Reputation: 345

This is not an elegant solution, but it can work in most of the cases.

The idea is to plot an additional bar (in x=2), and then hide it changing the limits of the x axis (let's say [0.05, 1.95]):

M=zeros(2,4);
M(1,:)=rand(1,4);

bar(M, "stacked");
xlim([0.05 1.95]);

l1 = legend("Col1", "Col2", "Col3", "Col4", "Col5");
legend(l1, "location", "northeastoutside");

Upvotes: 1

Related Questions