Home > Net >  How can I have two separate labels on each coordinate using matplotlib?
How can I have two separate labels on each coordinate using matplotlib?

Time:08-13

I am trying to use for loops to iterate a label with the coordinates of each point as well as a name. The coordinates work fine but all 6 names are iterating on each point. This is what I have so far.

import matplotlib.pyplot as plt

x = [0.0, -0.15, 0.04, 0.05, 0.1, -0.1]
y = [-0.1, -0.2, 0.0, 0.15, -0.06, 0.3]
titles = ["title 1", "title 2", "title 3", "title 4", "title 5", "title 6"]

plt.plot(x, y, 'r*')
plt.axis([-0.35, 0.35, -0.35, 0.35])

for i, j in zip(x, y):
    plt.text(i, j 0.02, '({}, {})'.format(i, j))
    for name in titles:
        plt.text(i, j 0.04, name)

plt.axvline(x=0, c="black")
plt.axhline(y=0, c="black")
plt.show()

image

CodePudding user response:

You were doing the right thing with zip, but now you can add titles also in it (also suggested in the comments above).

import matplotlib.pyplot as plt

x = [0.0, -0.15, 0.04, 0.05, 0.1, -0.1]
y = [-0.1, -0.2, 0.0, 0.15, -0.06, 0.3]
titles = ["title 1", "title 2", "title 3", "title 4", "title 5", "title 6"]

plt.plot(x, y, 'r*')
plt.axis([-0.35, 0.35, -0.35, 0.35])

for i, j, name in zip(x, y, titles):
    plt.text(i, j 0.02, '({}, {})'.format(i, j))
    plt.text(i, j 0.04, name)

plt.axvline(x=0, c="black")
plt.axhline(y=0, c="black")
plt.show()

Not sure to understand well what you want. Does this thing work for you ?

  • Related