pythonmatplotlib

Matplotlib: Display value next to each point on chart


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.


Solution

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

    import matplotlib.pyplot as plt
    
    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)
    plt.show()
    

    Output

    enter image description here