如何在networkx绘图的右上角放置文字
How to put text in the top right corner of networkx drawing
我正在绘制图表,想在右上角添加一些文字。但是我不确定该怎么做。我试过了:
import matplotlib.pyplot as plt
import networkx as nx
import pydot
from networkx.drawing.nx_pydot import graphviz_layout
T = nx.balanced_tree(2, 5)
pos = graphviz_layout(T, prog="dot")
nx.draw(T, pos, node_color="y", edge_color='#909090', node_size=200, with_labels=True)
plt.text(0,0,"******************")
plt.show()
看看我是否可以显示任何文本。它确实在左下角显示了星号。我如何知道图表的分辨率,以便计算出右上角的位置?
您可以阅读 networkx.drawing.nx_pylab.draw_networkx
:
draw_networkx(G, pos=None, arrows=None, with_labels=True, **kwds)
Parameters:
ax : Matplotlib Axes object, optional
Draw the graph in the specified Matplotlib axes.
然后您可以创建 fig, axe = plt.subplots()
并将 axe
传递给 nx.draw
并为 axe
添加标题 然后您可以显示绘图的标题,如下所示:
使用 matplotlib.axes.Axes.set_title
, which has parameters to set the location of the title, including the use of matplotlib.text
个参数设置标题。 (例如 x=1
或 loc='right'
)。
import matplotlib.pyplot as plt
import networkx as nx
import pydot
from networkx.drawing.nx_pydot import graphviz_layout
T = nx.balanced_tree(2, 5)
fig, axe = plt.subplots(figsize=(12,7))
axe.set_title('Title for NetworkX', loc='right')
pos = graphviz_layout(T, prog="dot")
nx.draw(T, pos, ax = axe, node_color="y", edge_color='#909090', node_size=200, with_labels=True)
plt.show()
输出:
- 当使用
x=
时,标题在x-axis上的x
居中;调整精确定位的值。
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(10, 4))
ax1.set_title('Title for NetworkX', loc='right')
ax2.set_title('Title for NetworkX', x=.68)
ax3.set_title('Title for NetworkX', x=1)
fig.tight_layout()
我正在绘制图表,想在右上角添加一些文字。但是我不确定该怎么做。我试过了:
import matplotlib.pyplot as plt
import networkx as nx
import pydot
from networkx.drawing.nx_pydot import graphviz_layout
T = nx.balanced_tree(2, 5)
pos = graphviz_layout(T, prog="dot")
nx.draw(T, pos, node_color="y", edge_color='#909090', node_size=200, with_labels=True)
plt.text(0,0,"******************")
plt.show()
看看我是否可以显示任何文本。它确实在左下角显示了星号。我如何知道图表的分辨率,以便计算出右上角的位置?
您可以阅读 networkx.drawing.nx_pylab.draw_networkx
:
draw_networkx(G, pos=None, arrows=None, with_labels=True, **kwds)
Parameters:
ax : Matplotlib Axes object, optional Draw the graph in the specified Matplotlib axes.
然后您可以创建 fig, axe = plt.subplots()
并将 axe
传递给 nx.draw
并为 axe
添加标题 然后您可以显示绘图的标题,如下所示:
使用 matplotlib.axes.Axes.set_title
, which has parameters to set the location of the title, including the use of matplotlib.text
个参数设置标题。 (例如 x=1
或 loc='right'
)。
import matplotlib.pyplot as plt
import networkx as nx
import pydot
from networkx.drawing.nx_pydot import graphviz_layout
T = nx.balanced_tree(2, 5)
fig, axe = plt.subplots(figsize=(12,7))
axe.set_title('Title for NetworkX', loc='right')
pos = graphviz_layout(T, prog="dot")
nx.draw(T, pos, ax = axe, node_color="y", edge_color='#909090', node_size=200, with_labels=True)
plt.show()
输出:
- 当使用
x=
时,标题在x-axis上的x
居中;调整精确定位的值。
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(10, 4))
ax1.set_title('Title for NetworkX', loc='right')
ax2.set_title('Title for NetworkX', x=.68)
ax3.set_title('Title for NetworkX', x=1)
fig.tight_layout()