0

I am generating a networkx word graph and displaying it with pyplot as follows:

import networkx as nx
import matplotlib.pyplot as plt
IN_TXT = """
Giorgia Meloni festeggia i 10 anni di FdI e oggi arriverà la ciliegina sulla torta: l’accordo con gli alleati su una manovra che colpisce i più poveri. Annunciata una nuova stretta al reddito di cittadinanza per finanziare le pensioni minime: solo 600 euro e solo agli over 75
"""

then populate my networkx graph and when ready:

nodelabels = nx.get_node_attributes(G, "lemma")
edgelabels = nx.get_edge_attributes(G, "label")
# and now display the graphs
nx.draw(G, pos, with_labels=True, labels=nodelabels, **NXDOPTS)
nx.draw_networkx_edge_labels(G, pos, edge_labels=edgelabels)
plt.margins(0.2)
plt.suptitle(sentence.text)
plt.show()

but this results in the superior title being vastly outside the picture: network graph with very long title Is there a way to word wrap the title so it fits the viewing window? If I manually stretch the window I can get the title to fit into it, but would like to obtain this then the graph is first displayed. I am not understanding the relationship between matplotlib abd pyplot :( Thank you.

Robert Alexander
  • 875
  • 9
  • 24
  • You can insert newlines into the title string to wrap it at the spots that make most sense for readability. – JohanC Dec 19 '22 at 12:01
  • That is correct, Manually inserting a new line every say 60 chars is possible, but as I am processing hundreds of phrases of which I cannot know the length in advance, I was hoping to find a pyplot function to do the wrapping. – Robert Alexander Dec 19 '22 at 12:25
  • 3
    Aside: since your networks are always directed, acyclic graphs (and usually trees), the `dot` layout will probably yield more readable results than the default `spring` layout. – Paul Brodersen Dec 19 '22 at 13:09
  • Thanks @PaulBrodersen! Only problem I have a bunch of edge labels in the xxx:yyy format which I'd have to quote/reformat :( – Robert Alexander Dec 19 '22 at 13:46
  • Why does the format of the edge labels matter when choosing a node layout? [This](https://networkx.org/documentation/stable/reference/generated/networkx.drawing.nx_pydot.graphviz_layout.html) is what I am suggesting. – Paul Brodersen Dec 19 '22 at 14:06
  • 1
    Because this is what happens when I try using: ValueError: Node names and attributes should not contain ":" unless they are quoted with "". For example the string 'attribute:data1' should be written as '"attribute:data1"'. Please refer https://github.com/pydot/pydot/issues/258 – Robert Alexander Dec 19 '22 at 14:17
  • 1
    IIRC, making a subgraph strips node and edge data: `H = nx.subgraph(G, G.nodes); pos = nx.nx_pydot.graphviz_layout(H)`. Otherwise, there are other, independent implementations of the Sugiyama layout (a.k.a. `dot`) in python. You can use [grandalf](https://github.com/bdcht/grandalf) or [netgraph](https://github.com/paulbrodersen/netgraph), which I wrote and maintain. The latter supports `networkx.Graph` objects. Example (albeit using an edge list) [here](https://netgraph.readthedocs.io/en/latest/sphinx_gallery_output/plot_08_dot_layout.html#sphx-glr-sphinx-gallery-output-plot-08-dot-layout-py). – Paul Brodersen Dec 19 '22 at 16:41
  • Thank you very much @PaulBrodersen! The subgraph does seem to keep the nodes/edges data unfortunately. So will look into netgraph or manually iterate and fix the xxxx:yyyy attributes :( – Robert Alexander Dec 19 '22 at 18:03

1 Answers1

0

Found the solution by reading matplotlib's documentation on autowrap

so the code in the question:

plt.suptitle(sentence.text)

just needs to be changed to:

plt.suptitle(sentence.text, wrap=True)
Robert Alexander
  • 875
  • 9
  • 24