Python plotting 3D points with annotation

2020-07-27 04:28发布

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.

My problem is -

  • Annotations get overlapped
  • In the legends, I don't understand why there are two symbols of triangles and circles. Shouldn't it be just one?

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.

Here is my code.

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()

1条回答
神经病院院长
2楼-- · 2020-07-27 05:19

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)

where numpoints changes the number of points within the legend - now it's set to 1.

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'))
查看更多
登录 后发表回答