寻找一种解决方案,用一对有序的笛卡尔坐标正确注释我的情节。 我当前的解决方案没有正确地将我的标签元素识别为可订阅对象。 需要将列表中的每个元素转换为字符串格式。
请注意,我有两个垂直轴和一个共享的 x 轴。 我不是绘制 (x,y) 标签,而是尝试绘制 (y1, y2) 的标签。
尝试
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.bar(products, quantity, color='.8', alpha =.8)
ax2.plot(products, price, 'bo-')
ax1.set_xlabel('', fontweight='bold')
ax1.set_ylabel('Quantity', color = 'k', fontweight='bold')
ax2.set_ylabel('Price $', color = 'b', fontweight='bold')
ax1.set_xticklabels(products, rotation=45, size = 8)
y1 = [i for i in quantity]
y2 = [j for j in price]
问题区
label = []
for x, y in zip(y1,y2):
label.append(f"({x:.2f},{y:.2f})") #can't find a method to convert my list elements from float to string values
for i, label in enumerate(label):
plt.annotate(label, xy=(x[i], y[i]), xytext=(5, 5),
textcoords='offset points', ha='left', va='bottom')
plt.show()
我尝试合并 "%.2f" % 但不明白如何使用 .append() 内联完成它
我觉得我正在寻找类似于以下任一的解决方案:
我的最终图看起来与本文底部的相同,只是折线图具有适当的 (y1, y2) 标记坐标: