Erincon
Erincon

Reputation: 399

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()

Upvotes: 0

Views: 409

Answers (1)

r-beginners
r-beginners

Reputation: 35115

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()

enter image description here

Upvotes: 1

Related Questions