- How to extract data from specfic sheets in the Excel file (using the African data as an example).
- How to produce a map of the world with functions from the Mapping Toolbox.
- How to add new axes to the map on which you can visualize your data using a stacked bar chart.
Geoplot with bar chart for each zone
6 views (last 30 days)
Show older comments
Hi,
I am really new to MATLAB and am trying to learn it very well for future positions. However, for now, I need to do a geoplot for 7 zones (Africa, Europe, Middle East, Asia Pacific, Euraisa, South & Central America and North America) that shows the change in their energy consumption by stacked bar chart in 8 different sectors for 2015 and 2021.
I would appreciate if anyone can give me a hint on how to do the code.
Thanks
0 Comments
Answers (1)
Austin M. Weber
on 27 Apr 2024
Edited: Austin M. Weber
on 27 Apr 2024
Assuming I understand what you're asking for, the following code should help get you started. Just note that this method requires the MATLAB Mapping Toolbox (also, I changed the name of your Excel file to 'CO2data.xlsx' for simplicity). In the example below, I show:
% African data
filename = 'CO2data.xlsx';
africa2015 = readtable(filename,'FileType','spreadsheet','Sheet','Africa 2015',...
'ReadRowNames',true,'VariableNamingRule','preserve');
africa2021 = readtable(filename,'FileType','spreadsheet','Sheet','Africa 2021',...
'ReadRowNames',true,'VariableNamingRule','preserve');
variable_names = {'Industry','Transport','Residential','Commercial','Agriculture','Fishing','Nonspecified','Non-energy use'};
africa2015totals = africa2015{"Total",:};
africa2021totals = africa2021{"Total",:};
% CREATE A WORLD MAP
load coastlines
figure()
ax=axesm('braun','MapLonLimit', [-180 180],'MapLatLimit',[-60 90]);
fillm(coastlat,coastlon,'#d5d5d5')
tightmap()
framem()
gridm()
% CREATE A NEW AXES FOR EACH BAR CHART
% Example for Africa:
axes('Position',[0.500, 0.400, 0.250, 0.100]) % X,Y,width,height
bar(variable_names,[africa2015totals; africa2021totals],'stacked')
title('Africa')
legend(gca,'2015','2021','Location','eastoutside')
It will take a lot of trial-and-error to get all 7 bar charts located in the correct places if you're doing everything programmatically, so I recommend simply moving the charts with the tools in the Figure Window whenever you create the map in your MATLAB Desktop application. I hope this helps!
See Also
Categories
Find more on Geographic 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!