Print string over plotted line (mimic contour plot labels)

后端 未结 2 1228
时光说笑
时光说笑 2020-12-03 11:34

The contour plot demo shows how you can plot the curves with the level value plotted over them, see below.

\"ent

2条回答
  •  伪装坚强ぢ
    2020-12-03 11:56

    Based on Jakob's code, here is a function that rotates the text in data space, puts labels near a given x or y data coordinate, and works also with log plots.

    def label_line(line, label_text, near_i=None, near_x=None, near_y=None, rotation_offset=0, offset=(0,0)):
        """call 
            l, = plt.loglog(x, y)
            label_line(l, "text", near_x=0.32)
        """
        def put_label(i):
            """put label at given index"""
            i = min(i, len(x)-2)
            dx = sx[i+1] - sx[i]
            dy = sy[i+1] - sy[i]
            rotation = np.rad2deg(math.atan2(dy, dx)) + rotation_offset
            pos = [(x[i] + x[i+1])/2. + offset[0], (y[i] + y[i+1])/2 + offset[1]]
            plt.text(pos[0], pos[1], label_text, size=9, rotation=rotation, color = line.get_color(),
            ha="center", va="center", bbox = dict(ec='1',fc='1'))
    
        x = line.get_xdata()
        y = line.get_ydata()
        ax = line.get_axes()
        if ax.get_xscale() == 'log':
            sx = np.log10(x)    # screen space
        else:
            sx = x
        if ax.get_yscale() == 'log':
            sy = np.log10(y)
        else:
            sy = y
    
        # find index
        if near_i is not None:
            i = near_i
            if i < 0: # sanitize negative i
                i = len(x) + i
            put_label(i)
        elif near_x is not None:
            for i in range(len(x)-2):
                if (x[i] < near_x and x[i+1] >= near_x) or (x[i+1] < near_x and x[i] >= near_x):
                    put_label(i)
        elif near_y is not None:
            for i in range(len(y)-2):
                if (y[i] < near_y and y[i+1] >= near_y) or (y[i+1] < near_y and y[i] >= near_y):
                    put_label(i)
        else:
            raise ValueError("Need one of near_i, near_x, near_y")
    

提交回复
热议问题