In matplotlib.pyplot, how to plot two datasets using interleaved bar graphs?

后端 未结 4 1765
一向
一向 2021-01-02 02:07

I am going through Think Stats and I would like to compare multiple data sets visually. I can see from the book examples that it is possible to generate an interleaved bar g

4条回答
  •  执念已碎
    2021-01-02 02:34

    Matplotlib's example code for interleaved bar charts works nicely for arbitrary real-valued x coordinates (as mentioned by @db42).

    However, if your x coordinates are categorical values (like in the case of dictionaries in the linked question), the conversion from categorical x coordinates to real x coordinates is cumbersome and unnecessary.

    You can plot two dictionaries side-by-side directly using matplotlib's api. The trick for plotting two bar charts with an offset to each other is to set align=edge and a positive width (+width) for plotting one bar chart, whereas a negative width (-width) for plotting the other one.

    The example code modified for plotting two dictionaries looks like the following then:

    """
    ========
    Barchart
    ========
    
    A bar plot with errorbars and height labels on individual bars
    """
    import matplotlib.pyplot as plt
    
    # Uncomment the following line if you use ipython notebook
    # %matplotlib inline
    
    width = 0.35       # the width of the bars
    
    men_means = {'G1': 20, 'G2': 35, 'G3': 30, 'G4': 35, 'G5': 27}
    men_std = {'G1': 2, 'G2': 3, 'G3': 4, 'G4': 1, 'G5': 2}
    
    rects1 = plt.bar(men_means.keys(), men_means.values(), -width, align='edge',
                    yerr=men_std.values(), color='r', label='Men')
    
    women_means = {'G1': 25, 'G2': 32, 'G3': 34, 'G4': 20, 'G5': 25}
    women_std = {'G1': 3, 'G2': 5, 'G3': 2, 'G4': 3, 'G5': 3}
    
    rects2 = plt.bar(women_means.keys(), women_means.values(), +width, align='edge',
                    yerr=women_std.values(), color='y', label='Women')
    
    # add some text for labels, title and axes ticks
    plt.xlabel('Groups')
    plt.ylabel('Scores')
    plt.title('Scores by group and gender')
    plt.legend()
    
    def autolabel(rects):
        """
        Attach a text label above each bar displaying its height
        """
        for rect in rects:
            height = rect.get_height()
            plt.text(rect.get_x() + rect.get_width()/2., 1.05*height,
                    '%d' % int(height),
                    ha='center', va='bottom')
    
    autolabel(rects1)
    autolabel(rects2)
    
    plt.show()
    

    The result:

提交回复
热议问题