20
$\begingroup$

I have seen many queries on zooming in on plots etc. But I haven't seen any regarding 2D graphs. For eg if I have a graph like

Graph[{1->2, 2->3, 3->1}].

It will create a graph. I have a similar graph which is very complex which has vertices very close by. I want to zoom in on a set of nodes and see how they are related. I can increase the image size or decrease the vertex size, but both of them are not helping since the graph is pretty huge and has a complex network of nodes which are clustered together and connected to each other. I can use NeighbourhoodGraph function. But is there any way to zoom in on the graph itself to see the how the nodes are connected to each other?

$\endgroup$

2 Answers 2

30
$\begingroup$

You can do this using Show and PlotRange which can be used in combination with graphs. To determine the full PlotRange of the original Graph you could use AbsoluteOptions to determine the values of the VertexCoordinates of the graph. The function CoordinateBoundingBox, introduced in V10.1, is helpful here:

SeedRandom[1110];
g = RandomGraph[{70, 200}]

Mathematica graphics

{xrange, yrange} =  VertexCoordinates /. AbsoluteOptions[g, VertexCoordinates] 
                      // CoordinateBoundingBox // Transpose
(* {{0., 5.33683}, {0., 4.09534}}  *)

An interactive example of zooming:

zoomGraph[g_Graph] :=
 DynamicModule[{pt},
  pt = VertexCoordinates /. AbsoluteOptions[g, VertexCoordinates] // CoordinateBoundingBox;
  Row[
   {
    LocatorPane[Dynamic[pt], 
     Dynamic[
       Show[
         g, 
         Graphics[{FaceForm[], EdgeForm[Red], Rectangle @@ pt}], 
         ImageSize -> 400
       ]
      ]
    ],
    Dynamic[Show[g, PlotRange -> Transpose[pt], ImageSize -> 400]]
    }
   ]
  ]

zoomGraph[g]

enter image description here

In some cases, the zoomed-in vertices may become too big: you could reduce the vertex size as follows:

zoomGraph[Graph[g, VertexSize -> 0.5]]

or add a VertexSize to the zoomed-in graph.

$\endgroup$
1
  • $\begingroup$ How can I use this feature with Graphics[] instead? $\endgroup$
    – Stratus
    Commented Jan 27, 2019 at 14:30
30
$\begingroup$

This is a prototype, it can not handle Text Inset and some more complicated directives efficiently but I don't have time / motivation to improvie it.


Here's something fun:

g = Normal @ Show @ CommunityGraphPlot[
     ExampleData[{"NetworkGraph", "DolphinSocialNetwork"}]
];

enter image description here

dist = Normalize[#] (2./Pi ArcTan[Norm[5 #]]) &;

DynamicModule[{drag,pts,prims}
 ,
 pts = Union@Cases[g, {_?NumericQ, _?NumericQ}, \[Infinity]];

 prims = (
    First[g] /. Thread[# -> Range@Length@#] &@pts)/. FilledCurve -> (# &);

 Panel@Column[{EventHandler[
     Graphics[{
          GraphicsComplex[ Dynamic[dist /@ pts], prims]
      }, 
      ImageSize -> 500, 
      PlotRange -> 1], 
  {"MouseDown" :> {drag = {MousePosition@"GraphicsScaled"}}, 
   "MouseDragged" :> {drag = {Last@drag, 
          MousePosition@"GraphicsScaled"};
        pts = (# + 5 (#2 - # & @@ drag) & /@ pts)}}]}]]

enter image description here

$\endgroup$
5
  • $\begingroup$ Fun indeed. What parameter do I change to modify the magnitude of the fish-eye effect? $\endgroup$
    – Mr.Wizard
    Commented Jan 29, 2016 at 7:35
  • $\begingroup$ @Mr.Wizard The multiplier in dist inside Norm should be enough. $\endgroup$
    – Kuba
    Commented Jan 29, 2016 at 8:48
  • $\begingroup$ @Kuba +1 looks awesome, I was wondering if it would be possible to make this solution more generically applicable? Say, how could I pass a Graph and receive output as above? $\endgroup$ Commented Mar 28, 2016 at 8:38
  • $\begingroup$ @E.Doroskevic I was trying but labels are placed weirdly because "next to" after transformation may be "next to something else". There were also some other minor issues, I will try to back to that when I have more ideas and time. Thanks for feedback :) $\endgroup$
    – Kuba
    Commented Mar 31, 2016 at 8:02
  • $\begingroup$ @Kuba I appreciate that, thank you! I think this is a brilliant way to visualize large network graphs. Hope you will find a way to develop this idea forward! $\endgroup$ Commented Mar 31, 2016 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.