Plot a bar graph using Matlab? | MATLAB | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





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

sp_Feed sp_TopicIcon
Plot a bar graph using Matlab?
No permission to create posts
August 23, 2016
11:16 pm
Avatar
rw950431

Top
Members
Forum Posts: 269
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

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.

August 24, 2016
8:02 am
Avatar
Hans
Natick, MA

Admin
Forum Posts: 400
Member Since:
January 18, 2011
sp_UserOfflineSmall Offline

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

August 24, 2016
1:20 pm
Avatar
Rob Purser
Natick, MA

MathWorks
Members
Forum Posts: 47
Member Since:
August 22, 2015
sp_UserOfflineSmall Offline

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!

August 24, 2016
9:30 pm
Avatar
rw950431

Top
Members
Forum Posts: 269
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

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

 

August 25, 2016
1:34 pm
Avatar
Rob Purser
Natick, MA

MathWorks
Members
Forum Posts: 47
Member Since:
August 22, 2015
sp_UserOfflineSmall Offline

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!

August 25, 2016
11:42 pm
Avatar
rw950431

Top
Members
Forum Posts: 269
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

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

August 29, 2016
9:19 am
Avatar
Adarsh_Murthy

MathWorks
Members
Forum Posts: 62
Member Since:
August 25, 2015
sp_UserOfflineSmall Offline

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

August 29, 2016
7:15 pm
Avatar
rw950431

Top
Members
Forum Posts: 269
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

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.

August 31, 2016
10:04 am
Avatar
Adarsh_Murthy

MathWorks
Members
Forum Posts: 62
Member Since:
August 25, 2015
sp_UserOfflineSmall Offline

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 

August 31, 2016
7:59 pm
Avatar
rw950431

Top
Members
Forum Posts: 269
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

Fabulous!  Thanks very much

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online: samdavies
19 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

Lakshmanaprakash S B, bettetj60, ronnievw3, bessiesj18, Pavan kumar, Williampup

Administrators: Hans: 387, lee: 457