问题描述:

I would like to access to all labels coordinates in a plot.

For example, I draw 2 lines and show a legend:

import matplotlib.pyplot as plt

plt.plot([1,2],label="first_image")

plt.plot([2,1],label="second_image")

plt.legend()

plot.show()

I would like to get the position of all the circled labels in the following image:

网友答案:

Assuming you want to reposition the legend, you can use something like this..

import matplotlib.pyplot as plt

line1, = plt.plot([1,2],label="first_image", linestyle='--')
line2, = plt.plot([2,1],label="second_image", linewidth=4)

# Create a legend for the first line.
first_legend = plt.legend(handles=[line1], loc=1)

# Add the legend manually to the current Axes.
ax = plt.gca().add_artist(first_legend)

# Create another legend for the second line.
plt.legend(handles=[line2], loc=4)

plt.show()

Alternatively you can use matplotlib.legend_handler and Axis.set_label_position

网友答案:

You can get most of the artists' positions in window coordinates by using the get_window_extent() method.

To be able to use this method, the artist needs to have previously been drawn to the canvas. This can be manually triggered via plt.gcf().canvas.draw().

import matplotlib.pyplot as plt
plt.plot([1,2],label="first_image")
plt.plot([2,1],label="second_image")
leg = plt.legend()

plt.gcf().canvas.draw()
ticks = [t for t in plt.gca().get_xticklabels()]
for i, t in enumerate(ticks):
    print "Label {}, data: {}".format(i, t.get_text()), t.get_window_extent()
print "Legend location: ", leg.get_window_extent()
for i, l in enumerate(leg.texts):
    print "Label {}, {}".format(i, l.get_text()), l.get_window_extent()
plt.show()

This will print all the relevant coordinates

Label 0, data:  Bbox(x0=102.545454545, y0=43.0777777778, x1=102.545454545, y1=43.0777777778)
Label 1, data: 0.0 Bbox(x0=91.6079545455, y0=29.0777777778, x1=113.482954545, y1=43.0777777778)
Label 2, data: 0.2 Bbox(x0=181.789772727, y0=29.0777777778, x1=203.664772727, y1=43.0777777778)
Label 3, data: 0.4 Bbox(x0=271.971590909, y0=29.0777777778, x1=293.846590909, y1=43.0777777778)
Label 4, data: 0.6 Bbox(x0=362.090909091, y0=29.0777777778, x1=384.090909091, y1=43.0777777778)
Label 5, data: 0.8 Bbox(x0=452.272727273, y0=29.0777777778, x1=474.272727273, y1=43.0777777778)
Label 6, data: 1.0 Bbox(x0=542.454545455, y0=29.0777777778, x1=564.454545455, y1=43.0777777778)
Label 7, data:  Bbox(x0=102.545454545, y0=43.0777777778, x1=102.545454545, y1=43.0777777778)
Legend location:  Bbox(x0=419.305555556, y0=214.431597222, x1=569.055555556, y1=260.768402778)
Label 0, first_image Bbox(x0=463.75, y0=241.072222222, x1=541.375, y1=255.212847222)
Label 1, second_image Bbox(x0=463.75, y0=219.987152778, x1=563.5, y1=234.127777778)

Note however that it is in general not required or recommended to use those coordinates for any manipulations inside the figure, since they may change for every redraw. Depending on the usage case there might be better methods to achieve a certain goal.

Also note that those coordinates are not necessarily the pixel coordinates in the saved image. Those will depend on the dpi setting which may be different between on-screen and saved image. Also the backend used for saving may redraw the canvas which can possibly change the coordinates.

相关阅读:
Top