Anyone know how to make matlab plot a bar (column) graph instead of a line?

I've got this graph https://imgur.com/a/ELcjl which I would like to plot as a bar graph with a different colors for positive and negative values.

What I am looking for is kind of like the stem plots (https://au.mathworks.com/help/thingspeak/thingspeakstem.html) but I would really like solid bars rather than lines with circles on the ends.

Histogram is a good option if you want to look at frequency of observations or variation. However, if you want a "traditional" bar or column chart, take a look at the bar and barh commands in MATLAB.

For example, to make a bar graph of the cars going east bound for the past 10 minutes from our car counter:

cars = thingSpeakRead(38629,'OutputFormat','table','NumMinutes',10);

bar(datenum(cars.Timestamps),cars.NumberOfEastboundCars)

datetick

Rob

Senior Development Manager for IoT and Hardware Interfacing for MATLAB at MathWorks. Visit ThingSpeak.com to explore the IoT Analytic platform that speaks MATLAB made for engineers and scientists. You can collect, analyze, and act in 5 minutes or less!

Thanks Rob.. I got this code that works but looks pretty crappy. Do you know how I can set the width so the bars dont have gaps between them? I tried the width parameter but it seems to make no difference.

https://thingspeak.com/apps/matlab_visualizations/89481

% Draw a bar graph of Interconnector power

% red for positive, blue for negative

power = unique(thingSpeakRead(148317,'OutputFormat','table','NumDays',1));

pos=power(power.Interconnect > 0,:)

neg=power(power.Interconnect <= 0,:)

%bar(datenum(power.Timestamps),power.Interconnect,1.0)

bar(datenum(neg.Timestamps),neg.Interconnect,'r')

hold on

bar(datenum(pos.Timestamps),pos.Interconnect,'b')

hold off

datetick

Now you're getting into aesthetics.

The doc pages linked earlier provide a lot of info and examples on the large number of parameters you can tweak to your liking. Based on your comment, you want "BarWidth" to be 1. I'd suggest setting EdgeColor to match the FaceColor as well.

cars = thingSpeakRead(38629,'OutputFormat','table','NumMinutes',10);

h = bar(datenum(cars.Timestamps),cars.NumberOfEastboundCars,'r',...

'BarWidth',1,'EdgeColor','r');

hold on

h2 = bar(datenum(cars.Timestamps),-cars.NumberOfWestboundCars,'b',...

'BarWidth',1,'EdgeColor','b');

datetick

hold off

-Rob

Senior Development Manager for IoT and Hardware Interfacing for MATLAB at MathWorks. Visit ThingSpeak.com to explore the IoT Analytic platform that speaks MATLAB made for engineers and scientists. You can collect, analyze, and act in 5 minutes or less!

Rob/

Thanks for the info.

One-day graph looks much better..

https://thingspeak.com/apps/matlab_visualizations/89481

however extending the time-frame to 3 days still leaves a little to be desired

https://thingspeak.com/apps/matlab_visualizations/89640

% Draw a bar graph of Interconnector power

% red for positive, blue for negative

power = unique(thingSpeakRead(148317,'OutputFormat','table','NumDays',3));

pos=power(power.Interconnect > 0,:)

neg=power(power.Interconnect <= 0,:)

%bar(datenum(power.Timestamps),power.Interconnect,1.0)

bar(datenum(neg.Timestamps),neg.Interconnect,'r','BarWidth',1,'EdgeColor','r')

hold on

bar(datenum(pos.Timestamps),pos.Interconnect,'b','BarWidth',1,'EdgeColor','b')

hold off

grid on

title 'South Australian Interconnector Load'

ylabel 'Net Power (MW)'

legend('Buying Power','Selling Power')

datetick('x','dd/mm')

Given the amount of data that your looking to plot in a single chart, another option to consider is an area chart. Here is an example code snippet:

power = unique(thingSpeakRead(148317,'OutputFormat','table','NumDays',3));

% Make a copy of the data to manipulate for the area plot

pos = power.Interconnect;

% For the positive data define all points less than or equal to zero to be NaN

pos(pos <= 0) = NaN;

% Make a copy of the data to manipulate for the area plot

neg=power.Interconnect;

% For negative data define all points greater than zero to be NaN

neg(neg > 0) = NaN;

% Plot the negative data

area(datenum(power.Timestamps),neg, 'FaceColor', 'r');

hold on

% Plot the positive data

area(datenum(power.Timestamps),pos, 'FaceColor', 'b');

% Additional settings

hold off

grid on

title 'South Australian Interconnector Load'

ylabel 'Net Power (MW)'

legend('Buying Power','Selling Power')

datetick('x','dd/mm')

Thanks Adarsh, that looks great.

https://thingspeak.com/apps/matlab_visualizations/90293

Is there an easy way to reduce the areas of unused space at the beginning and end of the plotted data? Currently my plot has 3 days of data but a 4-day timescale.

Most Users Ever Online: 114

Currently Online: cstapels

25 Guest(s)

Currently Browsing this Page:

1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

yeseniara11

RomanFug

JamesPes

RobertBak

ScottSmiva

Amourhotte

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee