Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing numerical values on the plot with Matplotlib

Is it possible, with Matplotlib, to print the values of each point on the graph?

For example, if I have:

x = numpy.range(0,10) y = numpy.array([5,3,4,2,7,5,4,6,3,2]) pyplot.plot(x,y) 

How can I display y values on the plot (e.g. print a 5 near the (0,5) point, print a 3 near the (1,3) point, etc.)?

like image 536
Charles Brunet Avatar asked Jun 08 '11 16:06

Charles Brunet


People also ask

How do I show values in matplotlib?

Just call the plot() function and provide your x and y values. Calling the show() function outputs the plot visually.

How do you label a single point in a matplotlib graph in Python?

In single-point annotation we can use matplotlib. pyplot. text and mention the x coordinate of the scatter point and y coordinate + some factor so that text can be distinctly visible from the plot, and then we have to mention the text.


2 Answers

You can use the annotate command to place text annotations at any x and y values you want. To place them exactly at the data points you could do this

import numpy from matplotlib import pyplot  x = numpy.arange(10) y = numpy.array([5,3,4,2,7,5,4,6,3,2])  fig = pyplot.figure() ax = fig.add_subplot(111) ax.set_ylim(0,10) pyplot.plot(x,y) for i,j in zip(x,y):     ax.annotate(str(j),xy=(i,j))  pyplot.show() 

If you want the annotations offset a little, you could change the annotate line to something like

ax.annotate(str(j),xy=(i,j+0.5)) 
like image 53
Stephen Terry Avatar answered Sep 19 '22 17:09

Stephen Terry


Use pyplot.text() (import matplotlib.pyplot as plt)

import matplotlib.pyplot as plt  x=[1,2,3] y=[9,8,7]  plt.plot(x,y) for a,b in zip(x, y):      plt.text(a, b, str(b)) plt.show() 
like image 20
Marvin W Avatar answered Sep 17 '22 17:09

Marvin W