Plotting pie charts over GraphPlot

6 次查看(过去 30 天)
Hi,
I want to plot pie-charts over the nodes of a graph plotted as a GraphPlot object such that the middle of the pie sits exactly at the node.
Hence, I want to do something like this
s = [1 1 1 1 2 2 3 4 4 5 6];
t = [2 3 4 5 3 6 6 5 7 7 7];
G = graph(s,t);
p = plot(G);
and then add an axes and plot a pie chart over one of the nodes with
axes('Position',[?, ?, pie_size, pie_size]);
X = [1 0.5 2.5 2]; pie_chart = pie(X, repmat({''},size(X)));
The ? represent the coordinates of the new plot inside the graph plot.
While I can read out the coordinates of the nodes in the graph plot from p.XData and p.YData, these seem to be relative to the center of the plot in units which are incompatible to any available unit system in the axes function.
Does anyone know of a way to accomplish what I want (possibly also in a different way)?

采纳的回答

Chunru
Chunru 2021-10-19
编辑:Chunru 2021-10-21
Here is the updated answer. We play with the properties of pie_chart this time.
clf
s = [1 1 1 1 2 2 3 4 4 5 6];
t = [2 3 4 5 3 6 6 5 7 7 7];
ax=axes;
G = graph(s,t);
p = plot(G);
hold on
X = [1 0.5 2.5 2];
pie_chart = pie(ax, X, repmat({''},size(X)));
% Align the pie_chart
idx = find(p.NodeLabel == "1");
xn=p.XData(idx);
yn=p.YData(idx);
scale = 0.2;
for k = 1:length(pie_chart)
if pie_chart(k).Type=="patch"
XData = pie_chart(k).XData;
YData = pie_chart(k).YData;
set(pie_chart(k),'XData', XData*scale + xn);
set(pie_chart(k),'YData', YData*scale + yn);
set(pie_chart(k),'FaceAlpha', 0.5);
else % Text labels
Pos = pie_chart(k).Position;
pie_chart(k).Position = Pos*scale + [xn yn 0];
end
end
axis equal
% clf
% % Pie chart
% ax(1)=axes;
% X = [1 0.5 2.5 2]; pie_chart = pie(X, repmat({''},size(X)));
% set(findobj(pie_chart, 'Type', 'Patch'), 'FaceAlpha', 0.8)
%
% % Graph
% ax(2)=axes;
% ax(2).Position = ax(1).Position;
%
% s = [1 1 1 1 2 2 3 4 4 5 6];
% t = [2 3 4 5 3 6 6 5 7 7 7];
%
% G = graph(s,t);
% p = plot(G);
% ax(2).Color='none';
%
% % Align the graph
% idx = find(p.NodeLabel == "1");
% xn=p.XData(idx);
% yn=p.YData(idx);
%
% % scale the graph by 0.5 (adjust this number)
% p.XData = (p.XData-xn)*.5;
% p.YData = (p.YData-yn)*.5;
%
% ax(2).XLim = ax(1).XLim;
% ax(2).YLim = ax(1).YLim;
% s = [1 1 1 1 2 2 3 4 4 5 6];
% t = [2 3 4 5 3 6 6 5 7 7 7];
% ax(1) = axes;
% G = graph(s,t);
% p = plot(G);
% ax(2)=axes;
%
% ax(2).Color='none'
% X = [1 0.5 2.5 2]; pie_chart = pie(X, repmat({''},size(X)));
% set(findobj(pie_chart, 'Type', 'Patch'), 'FaceAlpha', 0.3)
  4 个评论
Woda
Woda 2021-10-21
Thank you for your answer. However, I am afraid that still does not solve my problem. At least I cannot see how to adapt your code so that it does.
The problem is that I want to have a small pie chart plotted over every (!) node of the lattice, much like this
only with one chart per node.
In your solution, you move one node of the graph to the center of the plot to align the pie chart with the node. This, however, works only for one node! When trying to instead move the pie-chart to a non-zero location to coincide with the node, I faced the same problem as in my first answer (the coordindates do not seem to measured in the same units).

请先登录,再进行评论。

更多回答(1 个)

Woda
Woda 2021-10-21
Thank you very much for your patience - the last solution is spot on!

类别

Help CenterFile Exchange 中查找有关 Pie Charts 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by