Plot a bar graph using Matlab? | MATLAB | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
Plot a bar graph using Matlab?
Avatar
rw950431

Forum Posts: 252
sp_UserOfflineSmall Offline
1
August 23, 2016 - 11:16 pm
sp_Permalink sp_Print

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.

Avatar
Hans
Natick, MA

Forum Posts: 390
sp_UserOfflineSmall Offline
2
August 24, 2016 - 8:02 am
sp_Permalink sp_Print

Hi. You might be looking for a histogram (or hist). I will check with others to find other options. 

Avatar
Rob Purser
Natick, MA

Forum Posts: 46
sp_UserOfflineSmall Offline
3
August 24, 2016 - 1:20 pm
sp_Permalink sp_Print

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!

Avatar
rw950431

Forum Posts: 252
sp_UserOfflineSmall Offline
4
August 24, 2016 - 9:30 pm
sp_Permalink sp_Print

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

 

Avatar
Rob Purser
Natick, MA

Forum Posts: 46
sp_UserOfflineSmall Offline
5
August 25, 2016 - 1:34 pm
sp_Permalink sp_Print

Now you're getting into aesthetics. Smile

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!

Avatar
rw950431

Forum Posts: 252
sp_UserOfflineSmall Offline
6
August 25, 2016 - 11:42 pm
sp_Permalink sp_Print

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')

Avatar
Adarsh_Murthy

Forum Posts: 59
sp_UserOfflineSmall Offline
7
August 29, 2016 - 9:19 am
sp_Permalink sp_Print

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')

Avatar
rw950431

Forum Posts: 252
sp_UserOfflineSmall Offline
8
August 29, 2016 - 7:15 pm
sp_Permalink sp_Print

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.

Avatar
Adarsh_Murthy

Forum Posts: 59
sp_UserOfflineSmall Offline
9
August 31, 2016 - 10:04 am
sp_Permalink sp_Print

axis('tight') should do the trick. Here is the full doc for the function if you want to set custom limits:

http://www.mathworks.com/help/matlab/ref/axis.html 

Avatar
rw950431

Forum Posts: 252
sp_UserOfflineSmall Offline
10
August 31, 2016 - 7:59 pm
sp_Permalink sp_Print

Fabulous!  Thanks very much

Forum Timezone: America/New_York

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