Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: Display value next to each point on chart

Tags:

matplotlib

Is it possible to display each point's value next to it on chart diagram:

Chart

Values shown on points are: [7, 57, 121, 192, 123, 240, 546]

values = list(map(lambda x: x[0], result)) #[7, 57, 121, 192, 123, 240, 546]
labels = list(map(lambda x: x[1], result)) #['1950s', '1960s', '1970s', '1980s', '1990s', '2000s', '2010s']

plt.plot(labels, values, 'bo')
plt.show()

Here's my current code for this chart.

I would like to know each point value shown on graph, currently I can only predict values based on y-axis.

like image 613
Lululu Avatar asked Dec 18 '22 21:12

Lululu


1 Answers

Based on your values, here is one solution using plt.text

fig = plt.figure()
ax = fig.add_subplot(111)
values = [7, 57, 121, 192, 123, 240, 546]
labels = ['1950s', '1960s', '1970s', '1980s', '1990s', '2000s', '2010s']

plt.plot(range(len(labels)), values, 'bo') # Plotting data
plt.xticks(range(len(labels)), labels) # Redefining x-axis labels

for i, v in enumerate(values):
    ax.text(i, v+25, "%d" %v, ha="center")
plt.ylim(-10, 595)

Output

enter image description here

like image 116
Sheldore Avatar answered Mar 09 '23 01:03

Sheldore