6
$\begingroup$

I have just discovered that it is possible to manipulate vertices or edges of graphs if they are converted to Graphics.

After manipulation you can extract position of vertices and use them for VertexCoordinates of the graph.

Show@Graph[{{1, 2}, {2, 3}, {3, 1}, {1, 4}}]

enter image description here

Might be handy if you are not satisfied with default layouts.

But if VertexLabels -> Automatic is used it does not work anymore and edges are disconnected from vertices after manipulation.

Any idea how to preserve the same functionality also with labels?

Show@Graph[{{1, 2}, {2, 3}, {3, 1}, {1, 4}}, 
  VertexLabels -> Automatic]

enter image description here

$\endgroup$
6
  • $\begingroup$ Which version? I cannot manipulate the Graph on "14.2.1 for Mac OS X ARM (64-bit) (March 16, 2025)". $\endgroup$ Commented Aug 15 at 0:26
  • $\begingroup$ @A.Kato Double click the graphics at first. $\endgroup$ Commented Aug 15 at 0:55
  • $\begingroup$ @cvgmt, I got it! Thank you very much. $\endgroup$ Commented Aug 15 at 1:15
  • $\begingroup$ @A.Kato It is not allowed (currently) to manipulate Graph, we are manipulating Graphics of Graph. $\endgroup$ Commented Aug 15 at 10:21
  • $\begingroup$ Does IGGraphEditor in this recent answer meet your needs? mathematica.stackexchange.com/questions/314949/… $\endgroup$ Commented Aug 17 at 22:38

2 Answers 2

6
$\begingroup$

FYI older discussion. I think this should be done in the proper interactive environment, retaining Graph as a Graph object, not in Graphics editing window. Using tools in the interface you can save configs. or paste those you like.

Manipulate[
    Graph[{{1,2},{2,3},{3,1},{1,4}},
    VertexLabels->Automatic,
    VertexCoordinates->pt,
    ImageSize->300{1,1}],
{{pt,{{0,0},{1,0},{1,1},{0,1}}},Locator}]

enter image description here

You can also use DragGraphVertices at Wolfram Function Repository:

enter image description here

$\endgroup$
5
  • 1
    $\begingroup$ (+1) Nice but with just, say, 100 (or even less) vertices your tool becomes not very well responsive while on built-in tool for editing you can not recognize any decrease in performance - it is nicely smooth same as with few vertices. $\endgroup$ Commented Aug 15 at 19:03
  • $\begingroup$ But the repository function is better at performance even though also not as much as built-in tool. $\endgroup$ Commented Aug 15 at 19:07
  • $\begingroup$ @azerbajdzan repository function is pretty similar in nature but uses DynamicModule and LocatorPane - you can download the source notebook and modify function to your purpose - it's a pretty small definition. I also added a link to an older discussion in the post. $\endgroup$ Commented Aug 16 at 5:45
  • $\begingroup$ Why are Locator-s so slow compared to built-in edit tool? $\endgroup$ Commented Aug 16 at 10:04
  • $\begingroup$ @azerbajdzan if you can formulate a minimal example in code and post it on Wolfram Community I can try to get some internal insight $\endgroup$ Commented Aug 16 at 18:36
5
$\begingroup$
  • PerformanceGoal -> "Quality"
Show[Graph[{{1, 2}, {2, 3}, {3, 1}, {1, 4}}, 
  VertexLabels -> Automatic, PerformanceGoal -> "Quality"]]
$\endgroup$
4
  • $\begingroup$ Does not work on version 13.0.1. If it works on some other version then it is the weirdest usage of option PerformanceGoal -> "Quality". How anybody should guess that this option is needed? I do not see any logic other than trail and error method - using all possible options combinations. $\endgroup$ Commented Aug 15 at 10:19
  • $\begingroup$ @azerbajdzan It work for 13.0.1, please only double click the points. $\endgroup$ Commented Aug 15 at 23:09
  • $\begingroup$ I know how to do edits. I did the same as without ` VertexLabels -> Automatic, PerformanceGoal -> "Quality"` when it works but with these options the edge are separated from vertices on my version 13.0.1. $\endgroup$ Commented Aug 16 at 9:57
  • $\begingroup$ Try to move edge and you will see it does not work. Without ` VertexLabels -> Automatic, PerformanceGoal -> "Quality"` you can move also edges. $\endgroup$ Commented Aug 16 at 10:00

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.