简体   繁体   中英

Label inset_axes scatter plot

I want to label the scatter markers in this plot by numbering them, but I do not know how to get the inset_axes coordinates to place the labels.

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
ax.plot(range(10))

x = np.random.rand(5)
y = np.random.rand(5)
labels = [str(num+1) for num in range(len(x))]

axin2 = ax.inset_axes(
    [5, 7, 2.3, 2.3], transform=ax.transData)
axin2.scatter(x, y)
plt.show()

I don't have much experience with this type of graph, but I think I can use the coordinates of the scatter plot I have already set up and annotate it.

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(2021)
fig, ax = plt.subplots(figsize=(12,9))
ax.plot(range(10))

x = np.random.rand(5)
y = np.random.rand(5)
labels = [str(num+1) for num in range(len(x))]

axin2 = ax.inset_axes(
    [5, 7, 2.3, 2.3], transform=ax.transData)
axin2.scatter(x, y)

for i in range(len(x)):
    axin2.annotate(str(i), xy=(x[i]+0.02,y[i]))
inset = axin2.transData
# print(inset)
plt.show()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM