【问题标题】:ConversionError: Failed to convert value(s) to axis units: '43pts' when displaying text in MatplotlibConversionError:在 Matplotlib 中显示文本时无法将值转换为轴单位:\'43pts\'
【发布时间】:2023-01-03 01:02:58
【问题描述】:

我在 numpy 数组(点和俱乐部)之间绘制了一个图,并在它们之间绘制了一个散点图。

代码如下:

Points = np.array([30, 33, 38, 43])
Clubs = np.array(['Manchester United', 'Newcastle United', 'Manchester City', 'Arsenal'])

plt.xticks([30, 33, 38, 43], ['30pts', '33pts', '38pts', '43pts'])
plt.scatter(Points, Clubs)
plt.show()

散点图见附图。

我想要的是添加标签“Toppers”,其中俱乐部有最高分(本例中为阿森纳)。

我为此目的使用以下 sn-p:

plt.text('43pts', 'Arsenal', 'Toppers')
plt.grid(True)

但是,这给出了错误:

ConversionError:无法将值转换为轴单位:'43pts'

【问题讨论】:

    标签: python matplotlib scatter-plot


    【解决方案1】:

    plt.text 函数期望 x 和 y 坐标与 x 和 y 轴的单位相同。要解决此问题,您可以这样做:

    max_points_index = Points.argmax()
    max_points = Points[max_points_index]
    max_club = Clubs[max_points_index]
    
    plt.text(max_points, max_club, 'Toppers')
    

    【讨论】:

      猜你喜欢
      • 2020-03-07
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      • 2012-06-15
      相关资源
      最近更新 更多