Python 绘制带注释的 3D 点 [英] Python plotting 3D points with annotation
问题描述
我有一个3D图形,我想用坐标标注它们.但是,注释会重叠.我希望它们不重叠.
I have a 3D graph, and I would like to annotate them with the co-ordinates. However, the annotations gets overlapped. I would like them to not overlap.
我的问题是 -
- 注释重叠
- 在传说中,我不明白为什么有三角形和圆形两个符号.不应该只有一个吗?
仅供参考,我的数据集仅限于以下几点.所以即使任何其他参数是硬编码的,我也没关系.
Just for information, my data set is limited to the following points only. So even if any other parameters are hard-coded, it is okay with me.
这是我的代码.
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import proj3d
import matplotlib.pyplot as plt
import pylab
xData1=[ 24500., 2980., 2980., 13740.]
xData2=[ 8360., 8360., 24500., 5670., 2980., 2980., 11050., 13740.]
yData1=[ 179., 244., 242., 181.]
yData2=[ 132., 149., 116., 163., 247., 228., 116., 116.]
zData1=[ 1., 44., 86., 44.]
zData2=[ 86., 22., 1., 86., 43., 86., 86., 22.]
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(xData1, yData1, zData1, '^', c='r', label='cfg1')
ax.plot(xData2, yData2, zData2, 'o', c='b', label='cfg2')
for i in range(len(xData1)):
text='['+str(int(xData1[i]))+','+str(int(yData1[i]))+','+str(int(zData1[i]))+']'
x2, y2, _ = proj3d.proj_transform(xData1[i],yData1[i],zData1[i], ax.get_proj())
label = pylab.annotate(text,
xycoords='data',
xy = (x2, y2), xytext = (60, 20),
textcoords = 'offset points', ha = 'right', va = 'bottom',
bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5),
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
for i in range(len(xData2)):
text='['+str(int(xData2[i]))+','+str(int(yData2[i]))+','+str(int(zData2[i]))+']'
x2, y2, _ = proj3d.proj_transform(xData2[i],yData2[i],zData2[i], ax.get_proj())
label = pylab.annotate(text,
xycoords='data',
xy = (x2, y2), xytext = (20, 20),
textcoords = 'offset points', ha = 'right', va = 'bottom',
bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5),
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
ax.set_xlabel('X-Data')
ax.set_ylabel('Y-Data')
ax.set_zlabel('Z-Data')
ax.legend(ncol=3)
plt.show()
推荐答案
这两个问题的答案都比较简单.我将首先从第二个开始:您的图例中有两个符号,因为您在定义图例时没有指定数字,并且默认值为 2.要更正,只需更改:
Both questions are relatively easy answers. I'll start with the second one first: There are two symbols in your legend because you didn't specify the number when you defined the legend and the default value is two. To correct, simply change:
ax.legend(ncol=3, numpoints=1)
其中 numpoints
更改图例中的点数 - 现在设置为 1.
where numpoints
changes the number of points within the legend - now it's set to 1.
第一个问题的答案涉及操纵文本注释的位置,更具体地说是 xytext
,它给出文本的坐标.用下面的代码替换第二个for循环应摆脱重叠的文本,并为您提供一个很好的示例,说明如何更改其他任何难看的位置问题的注释框的位置:
The answer to your first question involves manipulating the placement of the text annotations, more specifically the xytext
, which gives the coordinates for the text. Replacing your second for-loop with the below should get rid of your overlapping text and give you a good example of how to change the location of the annotation boxes for any other unsightly location-issues:
for i in range(len(xData2)):
text='['+str(int(xData2[i]))+','+str(int(yData2[i]))+','+str(int(zData2[i]))+']'
x2, y2, _ = proj3d.proj_transform(xData2[i],yData2[i],zData2[i], ax.get_proj())
if i==4:
label = pylab.annotate(text,
xycoords='data',
xy = (x2, y2), xytext = (0, -50),
textcoords = 'offset points', ha = 'right', va = 'bottom',
bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5),
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
elif i==6:
label = pylab.annotate(text,
xycoords='data',
xy = (x2, y2), xytext = (-40, 0),
textcoords = 'offset points', ha = 'right', va = 'bottom',
bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5),
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
else:
label = pylab.annotate(text,
xycoords='data',
xy = (x2, y2), xytext = (-20, 10),
textcoords = 'offset points', ha = 'right', va = 'bottom',
bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5),
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
这篇关于Python 绘制带注释的 3D 点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!