r/matplotlib • u/____PatriotsSuck____ • Aug 19 '21
How to add labels to my data?
Like I have a plot and I wanna be able to label the x and/or y value of each individual data point on the plot. Is that possible?
2
Upvotes
1
u/the_guruji Aug 19 '21 edited Aug 19 '21
If you are using a bar-chart, then this is easy. You can directly use
matplotlib.pyplot.bar_label(see this example from the gallery).Are you using a scatter plot? in this case it may not be trivial, since some of the points will need to have the label above/below or left/right, depending on its neighbours. There are some efforts to do this well, for example see these two stackoverflow posts: (https://stackoverflow.com/questions/8850142/matplotlib-overlapping-annotations) and (https://stackoverflow.com/questions/19073683/matplotlib-overlapping-annotations-text)
If your plot is very simple, and you just want some text on top of it or something, then you can directly just use
This will align your text to be on top of your point (but the marker may overlap the text, adjust the values to get it right).