Adding value labels on a matplotlib bar chart

后端 未结 5 1965
广开言路
广开言路 2020-11-22 08:24

I got stuck on something that feels like should be relatively easy. The code I bring below is a sample based on a larger project I\'m working on. I saw no reason to post all

5条回答
  •  终归单人心
    2020-11-22 08:33

    Firstly freq_series.plot returns an axis not a figure so to make my answer a little more clear I've changed your given code to refer to it as ax rather than fig to be more consistent with other code examples.

    You can get the list of the bars produced in the plot from the ax.patches member. Then you can use the technique demonstrated in this matplotlib gallery example to add the labels using the ax.text method.

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    # Bring some raw data.
    frequencies = [6, 16, 75, 160, 244, 260, 145, 73, 16, 4, 1]
    # In my original code I create a series and run on that, 
    # so for consistency I create a series from the list.
    freq_series = pd.Series.from_array(frequencies)
    
    x_labels = [108300.0, 110540.0, 112780.0, 115020.0, 117260.0, 119500.0,
                121740.0, 123980.0, 126220.0, 128460.0, 130700.0]
    
    # Plot the figure.
    plt.figure(figsize=(12, 8))
    ax = freq_series.plot(kind='bar')
    ax.set_title('Amount Frequency')
    ax.set_xlabel('Amount ($)')
    ax.set_ylabel('Frequency')
    ax.set_xticklabels(x_labels)
    
    rects = ax.patches
    
    # Make some labels.
    labels = ["label%d" % i for i in xrange(len(rects))]
    
    for rect, label in zip(rects, labels):
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width() / 2, height + 5, label,
                ha='center', va='bottom')
    

    This produces a labeled plot that looks like:

    enter image description here

提交回复
热议问题