How to merge a pie chart with a map

How to merge a pie chart with a map using MATLAB Mapping Toolbox? Just like this one.

19 Comments

I see you found <Merging a bar chart with a map>; I would suggest that is a good starting point. The code that draws the bar patches would be replaced by the code to draw the pie. You might be able to get away with using the builtin pie or piechart with the map axes as the axes handle, but it is also possible that won't work because of the map axes...in that case you would be forced to either the patch() solution of the given code with the X,Y coordinates computed to trace out the circle or see if rectangle will be accepted. In one of the most incongrous things still extant in MATLAB HG (handle graphics), there is no native circle function.
If you want/expect somebody here to provide working code, you'll need to at least provide one map and some corresponding data I think...expecting volunteers here to build something from complete scratch is asking quite a lot...
Thanks. Although you have provided many suggestions, it is of no help in solving my problem.
Hi @Hongyun,
@dpb and @Walter have many years of experience and they are one of the best technical experts in Matlab. They have given me best tips to resolve problems and helped others when someone gets stuck in the program. You can learn a lot from their tips. However, to answer your question about merging a pie chart with a map in MATLAB using the Mapping Toolbox, I can provide you guidance. First, create a geographic axes by start creating a geographic axes using the geoplot function. This will serve as the base map for your visualization.
figure
ax = geoaxes;
geoplot(ax, 'your_map.shp') % Example map data
Then, add pie chart data by defining the data for the pie chart that you want to overlay on the map. You can use the pie function to create the pie chart.
data = [30 20 15 35]; % Example data for the pie chart
labels = {'A', 'B', 'C', 'D'}; % Example labels for the pie chart
pie(ax, data, labels)
To position the pie chart correctly on the map, you can adjust the Position property of the pie chart. This property specifies the location and size of the pie chart within the axes.
ax.Position = [0.1 0.1 0.6 0.6]; % Adjust the position of the pie chart within the axes
Now time to customize the appearance of the pie chart by changing properties such as colors, labels, and explode slices if needed.
colormap(ax, jet) % Change the colormap of the pie chart
Once you have set up the map and pie chart overlay, you can finalize any additional customization and display the combined visualization.
title(ax, 'Map with Pie Chart Overlay') % Add a title to the visualization
So, if you follow these steps, you can effectively merge a pie chart with a map using MATLAB's Mapping Toolbox, creating a visually informative and engaging representation of geographic data with associated proportions. Please let me know if you have any further questions.
So how to add multiple pies into the map?
Your answer only can add one pie into the map. I need merge multiple pies within the map.
dpb
dpb on 29 Jul 2024
Edited: dpb on 29 Jul 2024
"So how to add multiple pies into the map?"
"Wash, rinse, repeat..." as the instructions on the shampoo bottle said.
Put the code to make the pie chart fit onto the map coordinates where desired in a function and then put that in a loop passing the data and coordinates.
Or, put the loop inside the function and then you can call it only once passing the data as arrays.
But, you've yet to follow up with my very first suggestion of putting up data files and code that generates the map and provides the needed data for somebody to play with...expecting volunteers to also create a working example as well of something pretty complicated is passing the buck too much. I don't have the TB so only way I could do anything at all further would be to poke at something here online in the forum...
Thanks. Although you have provided many suggestions, it is still of no help in solving my problem.
Hi @Hongyun ,
To add multiple pie charts to a geographic map in MATLAB, repeat the step to create the geographic map as shown in code snippet example below.
figure
ax = geoaxes;
geoplot(ax, 'your_map.shp') % Example map data
Add pie charts by defining the data and labels for each pie chart you want to overlay on the map by using the pie function to create each pie chart.
data1 = [30 20 15 35]; % Example data for the first pie chart
labels1 = {'A', 'B', 'C', 'D'}; % Example labels for the first pie chart
pie(ax, data1, labels1)
data2 = [25 30 20 25]; % Example data for the second pie chart
labels2 = {'X', 'Y', 'Z', 'W'}; % Example labels for the second pie chart
pie(ax, data2, labels2)
% Add more pie charts as needed
Then, adjust each pie chart positions by adjusting the Position property of each pie chart. This property specifies the location and size of the pie chart within the axes.
ax.Position = [0.1 0.1 0.6 0.6]; % Adjust the position of the first pie chart within the axes
% Adjust positions for other pie charts as necessary
Then, customize the appearance of each pie chart by changing properties such as colors, labels, and exploding slices if needed.
Thanks for Umar and dpb. I have addressed the problem on myself.
dpb
dpb on 30 Jul 2024
Edited: dpb on 31 Jul 2024
We knew you could if just gave it a go... :)
Looks good except maybe you could get them to rearrange their province boundaries some so it's not so cluttered in the SE? <VBG>
ADDENDUM/ERRATUM
One tiny nit caught my eye -- there's an "e" missing in "R[e]vised" in the caption...
@ Hongyun,
Glad to know your problem is resolved. I will @dpb suggestion under consideration. He has been given good tips. We tried our best to help you out and you finally able to resolve problem, I am happy 😃 for you. Please let us know if you have any further questions and don’t forget to vote for @dpb.
@Umar Although @dpb put forward some sugestions for my questions, they were all just empty talk on paper. He should learn to answer others' questions more through code programming rather than lengthy arguments.
You can challenge another difficult problem I posted here How can I hide the black line on the right side of colorbar. You can find the source code for cbarrow here https://www.mathworks.com/matlabcentral/fileexchange/52515-cbarrow-pointy-ends-for-colorbars.
@ Hongyun,
I took a gland through the documentation of charrow function,and then I came across this as quoted verbatim by author of the code. Hope you are aware of it. I probably have to do some side research on it and then do some code experimentation, if that is okay with you. Hope, you are not in a rush.
“Known issues ,This function only works once per figure. If you have multiple subplots,you can only use it once, and you'll have to call cbarrow last. Also, editing plots after calling cbarrow can sometimes be a bit glitchy.”
@Umar Thanks. I'm sure you will earn my respect through code programming.
@Hongyun,
Please check my solution regarding your query about, “ You can challenge another difficult problem I posted here How can I hide the black line on the right side of colorbar. You can find the source code for cbarrow here https://www.mathworks.com/matlabcentral/fileexchange/52515-cbarrow-pointy-ends-for-colorbars.”
@Umar Thanks for your best answer!
"@Umar Although @dpb put forward some sugestions for my questions, they were all just empty talk on paper. He should learn to answer others' questions more through code programming rather than lengthy arguments."
@Hongyun - if you'll look you'll see I've posted a great deal of code here over the years. If you will recall, my very first reply asked you to simply attach the data so folks had something as a starting point to which end you never responded.
When the solution to a problem is straightforward application of principles outlined, seeing effort towards solving the problem instead of whining also tends to elicit more willingness to invest what time I have...
Umar
Umar on 3 Aug 2024
Edited: Umar on 4 Aug 2024
Hi @Hongyun,
I was quiet for sometime but I would like to speak in defense of @dpb, although his sense of humor sometimes offend people but look at all of us, dealing with all technical issues, family problems even myself I lost everything in my life, went through lot of hardships,at this platform I have seen brilliant group of people including @dpb whom I consider as a brother, helped me through lot of issues, provided guidance and he is very exceptional and has helped lot of engineers, research students, teachers etc, you name it, his motivation excelled me at this platform and @Walter Robertson as well, @Walter goes above and beyond solving people problems even helps us by formatting our code. We need to work together as a team to solve our problems and should not be embarrassed to ask for tips or help even having higher ego. Again, @dpb is a gem in stone whose value should not be depreciated and I would like to thank both @Walter Robertson and @dpb for what they do to help out people at this platform among others such as @DGM, @Torsten, @Image Analyst,@Cyclist, it’s like Avengers Team.
dpb
dpb on 4 Aug 2024
Edited: dpb on 4 Aug 2024
@Umar, thank you first for your service; secondly for the kind words. I do figure there's no reason for a little jocularity not to tag along with the technical, agreed, and I am a sucker for bad puns and the like...and my preferred technique is to coach/teach rather than provide canned solutions that ime too often are just pasted into the asker's code without any attempt to understand or effort on their part. OTOH, sometimes one just does want to show off MATLAB's facilities when a particularly concise/neat solution is available...vectorization and/or logical indexing and the like that most newcomers find strange and daunting.

Sign in to comment.

Answers (0)

Categories

Products

Release

R2024a

Asked:

on 28 Jul 2024

Edited:

dpb
on 4 Aug 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!