绘图上的标记点

Labeling points on a plot

我正在开发一个 returns 具有 4 个方程最大值的图。无论我采用何种方式,每个方程的最大值点都显示在不易阅读的位置。我怎样才能把图例放在右边或左边?

代码:

import math as mt
import numpy as np
import matplotlib.pyplot as plt
  
v = 20 #(Vão em metros)
h = 6 #("Altura do pilar em metros:")
ht = 8 #("Altura total metros:")
alfa_rad = mt.atan((int(ht)-int(h))/(int(v)/2))
alfa_deg = alfa_rad*180/mt.pi
lasna = ((v/2) ** 2 + (ht-h) ** 2) ** 0.5

alfa = (mt.atan((int(ht)-int(h))/(int(v)/2)))*180/((mt.pi))



h1 = np.arange(0, h+1, 1)
ha1 = np.arange(0, lasna, 0.1)
ha2 = np.arange(0, lasna, 0.1)
hp2 = np.arange(0, h+1, 1)

R = lambda x, y, theta: np.array([
    [np.cos(theta), np.sin(theta), x],
    [-np.sin(theta), np.cos(theta), y],
    [0, 0, 1],
])
Vx = np.array([
    [1, 0, 0], [0, -1, 0], [0, 0, 1]
])
Vy = np.array([
    [-1, 0, 0], [0, 1, 0], [0, 0, 1]
])
vpilar1 = (1000 * h1 ** 2 + 50)/ 1000
vasna1 = (50 *ha1 ** 2 + 5) / 1000
vasna2 = (100 * ha2 ** 2 + 7) / 1000
vpilar2 = (150 * hp2 ** 2 + 5) / 1000

def draw_line():
    fig, ax = plt.subplots()
    
    x_number_list = [0, 0, (v/2), v, v]
    y_number_list = [0, h, ht, h, 0]
    plt.plot(x_number_list, y_number_list, linewidth=3)
    
    
    points1 = np.stack([h1, vpilar1 / max(abs(vpilar1)), np.ones_like(h1)])
    points1 = np.matmul(R(0, 0, -np.pi/2), points1)
    plt.plot(points1[0, :], points1[1, :], label="Vpilar1")
    idx = np.argmax(vpilar1)
    max_p = np.array([[h1[idx], (vpilar1 / max(abs(vpilar1)))[idx], 1]]).T
    max_p_tranformed = np.matmul(R(0, 0, -np.pi/2), max_p)
    plt.text(*max_p_tranformed[:-1], str(vpilar1[idx]))
    plt.scatter(*max_p_tranformed[:-1], marker="x", color="k")
      
    
    points2 = np.stack([h1, vpilar2 / max(abs(vpilar2)), np.ones_like(h1)])
    points2 = np.matmul(R(v, 0, -np.pi/2), points2)
    plt.plot(points2[0, :], points2[1, :], label="Vpilar2")
    idx = np.argmax(vpilar2)
    max_p = np.array([[h1[idx], (vpilar2 / max(abs(vpilar2)))[idx], 1]]).T
    max_p_tranformed = np.matmul(R(v, 0, -np.pi/2),  max_p)
    plt.text(*max_p_tranformed[:-1], str(vpilar2[idx]))
    plt.scatter(*max_p_tranformed[:-1], marker="x", color="k")
    
    
    points3 = np.stack([ha1, vasna1 / max(abs(vasna1)), np.ones_like(ha1)])
    points3 = np.matmul(R(0, h, -alfa_rad), points3)
    plt.plot(points3[0, :], points3[1, :], label="Vasna1")
    idx = np.argmax(vasna1)
    max_p = np.array([[ha1[idx], (vasna1 / max(abs(vasna1)))[idx], 1]]).T
    max_p_tranformed = np.matmul(R(0, h, -alfa_rad), max_p)
    plt.text(*max_p_tranformed[:-1], str(vasna1[idx]))
    plt.scatter(*max_p_tranformed[:-1], marker="x", color="k")
    
    points4 = np.stack([ha1, vasna2 / max(abs(vasna2)), np.ones_like(ha1)])
    points4 = np.matmul(np.matmul(R(v, h, alfa_rad), Vy), points4)
    plt.plot(points4[0, :], points4[1, :], label="Vasna2")
    idx = np.argmax(vasna2)
    max_p = np.array([[ha1[idx], (vasna2 / max(abs(vasna2)))[idx], 1]]).T
    max_p_tranformed = np.matmul(np.matmul(R(v, h, alfa_rad), Vy), max_p)
    plt.scatter(*max_p_tranformed[:-1], marker="x", color="k")
    
    plt.title("Esforço de Corte (kN)", fontsize=15)
    plt.show()

draw_line()

PS: 我是 SO 的 nobbie,所以不幸的是我不能显示情节。

draw_line() 函数中的plt.text 是负责编写具有最大值的文本的代码部分。在您的代码中,它只有 3 个,认为您删除了第 4 个,因为它在另一个之上。文本的位置(x 和 y 坐标)由第一个变量控制。您需要添加一些额外的空格以将文本移动到您想要的位置。下面是更新后的 4 行代码。如果您对代码进行了更改,您应该能够看到文本移动。

plt.text(*max_p_tranformed[:-1][0]+0.1, *max_p_tranformed[:-1][1]+0.2, str(vpilar1[idx]))

.....

plt.text(*max_p_tranformed[:-1][0]+0.1, *max_p_tranformed[:-1][1]+0.3, str(vpilar2[idx]))

.....

plt.text(*max_p_tranformed[:-1][0]-7.3, *max_p_tranformed[:-1][1]+0.1, str(vasna1[idx]))

.....

plt.text(*max_p_tranformed[:-1][0]+0.1, *max_p_tranformed[:-1][1]+0.1, str(vasna2[idx]))

图表看起来像这样。更改增量(+0.1、+0.2 等)以查看文本移动 up/down 或 left/right.

经过这些调整的输出图