Plotting pie charts over GraphPlot

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

 Respuesta aceptada

Chunru
Chunru el 19 de Oct. de 2021
Editada: Chunru el 21 de Oct. de 2021
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 comentarios

Woda
Woda el 19 de Oct. de 2021
Editada: Woda el 19 de Oct. de 2021
Thank you for your efforts! However, that is not exactly what I wanted.
I was looking for a solution, which places the pie chart exactly over one node, i.e., it should be such that the point where the four patches meet is exactly the location of, for example, node 1.
If I modify your code to
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)=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)
slices = findobj(pie_chart, 'Type', 'Patch');
for i = 1:4
slices(i).XData = slices(i).XData + p.XData(1);
slices(i).YData = slices(i).YData + p.YData(1);
end
I should get (somewhat) what I want (moving the pie chart in such a way that its center coincides with the location of the first node). However, it does not work, since the units between the two plots seem to be different.
Here is the result
Chunru
Chunru el 21 de Oct. de 2021
See the updated.
Woda
Woda el 21 de Oct. de 2021
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).
Chunru
Chunru el 21 de Oct. de 2021
Updated.

Iniciar sesión para comentar.

Más respuestas (1)

Woda
Woda el 21 de Oct. de 2021

0 votos

Thank you very much for your patience - the last solution is spot on!

Etiquetas

Preguntada:

el 19 de Oct. de 2021

Respondida:

el 21 de Oct. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by