Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center an annotation horizontally over a point?

I'd like to take something like this code for scatter plot annotations...

import matplotlib; matplotlib.use('TkAgg') import matplotlib.pyplot as plt  labels = ["Abra", "Kadabra", "Alazkazam", "Mew"] x_values = [0.3, 0.6, 0.2, 0.4] y_values = [0.2, 0.2, 0.4, 0.9]  fig = plt.figure(figsize=(5, 5)) plt.axis('off')  renderer = fig.canvas.get_renderer()  for i, label in enumerate(labels):     plt.scatter(x_values[i], y_values[i])     text_object = plt.annotate(label, xy=(x_values[i], y_values[i]))  plt.savefig("horizontally_centered_text_annotations.png") 

...which produces this plot: annotations without centering

...and make it produce something like this plot: annotations with centering

I've tried getting the window extent around the text boxes, grabbing the x coordinate and width, and shifting over for each annotation like so:

for i, label in enumerate(labels):     plt.scatter(x_values[i], y_values[i])     text_object = plt.annotate(label, xy=(x_values[i], y_values[i]))     text_window_extent = text_object.get_window_extent(renderer)     new_x_position = x_values[i] - text_window_extent.width / 2     text_object.set_position((new_x_position, y_values[i]))     print "x_value: {}, window_extent_width: {}, new_x_position: {}".format(x_values[i], text_window_extent.width, new_x_position) 

But as you can see here from print statements the widths are too big:

x_value: 0.3, window_extent_width: 31.5, new_x_position: -15.45 x_value: 0.6, window_extent_width: 56.0, new_x_position: -27.4 x_value: 0.2, window_extent_width: 72.875, new_x_position: -36.2375 x_value: 0.4, window_extent_width: 30.75, new_x_position: -14.975 

Not sure how if this has to do with coordinate systems...

like image 601
legel Avatar asked Jul 03 '16 19:07

legel


People also ask

How do I center annotations in Matplotlib?

Use scatter() method to scatter the points. Iterate labels, xpoints and ypoints and annotate the plot with label, x and y with different properties, make horizontal alignment ha=center.

How do you annotate a point in Python?

Annotating with Arrow. The annotate() function in the pyplot module (or annotate method of the Axes class) is used to draw an arrow connecting two points on the plot. This annotates a point at xy in the given coordinate ( xycoords ) with the text at xytext given in textcoords .

What does Xytext in annotate function indicate?

xy: This parameter is the point (x, y) to annotate. xytext: This parameter is an optional parameter. It is The position (x, y) to place the text at. xycoords: This parameter is also an optional parameter and contains the string value.


1 Answers

Use the horizontalalignment (which can be shortened as ha) option to the annotate call:

text_object = plt.annotate(label, xy=(x_values[i], y_values[i]), ha='center') 
like image 87
Diziet Asahi Avatar answered Oct 07 '22 21:10

Diziet Asahi