Spa*_*ine 7 python annotations matplotlib networkx
我用matplotlib绘制了networkx创建的图形.现在,我想将注释添加到具有圆圈的特定节点.例如,
我使用plt.annotate(*args, **kwargs)以下代码,
# add annotate text
pos = nx.get_node_attributes(G, 'pos')
pos_annotation_node = pos['Medici']
ax2.annotate('Midici',
xy=pos_annotation_node,
xytext=(i+0.2 for i in pos_annotation_node),
color='blue',
arrowprops=dict(facecolor='blue', shrink=0.01)
)
Run Code Online (Sandbox Code Playgroud)
我得到了这个丑陋的图表,
我有两个问题:
6,如第一张图所示.xytext多次手动设置值.有没有更好的办法?如果您使用fancyarrow arrowprops这表现在语法annotation_demo2,有shrinkA和shrinkB,让你缩小你的箭尾(选项shrinkA)和尖端(shrinkB)独立,以点为单位.
这是一些任意设置代码:
import matplotlib.pyplot as plt
import numpy as np
# Some data:
dat = np.array([[5, 3, 4, 4, 6],
[1, 5, 3, 2, 2]])
# This is the point you want to point out
point = dat[:, 2]
# Make the figure
plt.figure(1, figsize=(4, 4))
plt.clf()
ax = plt.gca()
# Plot the data
ax.plot(dat[0], dat[1], 'o', ms=10, color='r')
ax.set_xlim([2, 8])
ax.set_ylim([0, 6])
Run Code Online (Sandbox Code Playgroud)
这里的代码围绕其中一个点放置一个圆圈,并绘制一个仅在尖端收缩的箭头:
circle_rad = 15 # This is the radius, in points
ax.plot(point[0], point[1], 'o',
ms=circle_rad * 2, mec='b', mfc='none', mew=2)
ax.annotate('Midici', xy=point, xytext=(60, 60),
textcoords='offset points',
color='b', size='large',
arrowprops=dict(
arrowstyle='simple,tail_width=0.3,head_width=0.8,head_length=0.8',
facecolor='b', shrinkB=circle_rad * 1.2)
)
Run Code Online (Sandbox Code Playgroud)
请注意:
1)我已经使圆圈的标记面颜色透明mfc='none',并将圆形尺寸(直径)设置为半径的两倍.
2)我将箭头缩小了圆周半径的120%,使其稍微偏离圆圈.显然你可以玩circle_rad和你的价值,1.2直到你得到你想要的.
3)我使用了"花式"语法来定义字符串中的几个箭头属性,而不是在dict中.据我所知,shrinkB如果您不使用花式箭头语法,则该选项不可用.
4)我已经使用了textcoords='offset points'这样我可以指定文本相对于点的位置,而不是轴上的绝对位置.
| 归档时间: |
|
| 查看次数: |
4367 次 |
| 最近记录: |