Matplotlib:注释3D散点图 [英] Matplotlib: Annotating a 3D scatter plot

查看:99
本文介绍了Matplotlib:注释3D散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Matplotlib生成3D散点图.我想在此处注释单个点,例如2D情况: Matplotlib:如何为散点图放置单个标签.

I'm trying to generate a 3D scatter plot using Matplotlib. I would like to annotate individual points like the 2D case here: Matplotlib: How to put individual tags for a scatter plot.

我尝试使用此功能并查阅了Matplotlib,但发现该库似乎不支持3D注释.有人知道该怎么做吗?

I've tried to use this function and consulted the Matplotlib docoment but found it seems that the library does not support 3D annotation. Does anyone know how to do this?

谢谢!

推荐答案

计算点的2D位置,并使用它创建注释.如果需要与图形交互,则可以在释放鼠标时重新计算位置.

Calculate the 2D position of the point, and use it create the annotation. If you need interactive with the figure, you can recalculate the location when mouse released.

import pylab
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import proj3d
fig = pylab.figure()
ax = fig.add_subplot(111, projection = '3d')
x = y = z = [1, 2, 3]
sc = ax.scatter(x,y,z)
# now try to get the display coordinates of the first point

x2, y2, _ = proj3d.proj_transform(1,1,1, ax.get_proj())

label = pylab.annotate(
    "this", 
    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'))

def update_position(e):
    x2, y2, _ = proj3d.proj_transform(1,1,1, ax.get_proj())
    label.xy = x2,y2
    label.update_positions(fig.canvas.renderer)
    fig.canvas.draw()
fig.canvas.mpl_connect('button_release_event', update_position)
pylab.show()

这篇关于Matplotlib:注释3D散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆