How to plot bar graphs with same X coordinates side by side ('dodged')

后端 未结 5 1871
慢半拍i
慢半拍i 2020-12-05 02:12
import matplotlib.pyplot as plt

gridnumber = range(1,4)

b1 = plt.bar(gridnumber, [0.2, 0.3, 0.1], width=0.4,
                label=\"Bar 1\", align=\"center\")

b2         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-05 03:07

    Below answer will explain each and every line of code in the simplest manner possible:

    # Numbers of pairs of bars you want
    N = 3
    
    # Data on X-axis
    
    # Specify the values of blue bars (height)
    blue_bar = (23, 25, 17)
    # Specify the values of orange bars (height)
    orange_bar = (19, 18, 14)
    
    # Position of bars on x-axis
    ind = np.arange(N)
    
    # Figure size
    plt.figure(figsize=(10,5))
    
    # Width of a bar 
    width = 0.3       
    
    # Plotting
    plt.bar(ind, blue_bar , width, label='Blue bar label')
    plt.bar(ind + width, orange_bar, width, label='Orange bar label')
    
    plt.xlabel('Here goes x-axis label')
    plt.ylabel('Here goes y-axis label')
    plt.title('Here goes title of the plot')
    
    # xticks()
    # First argument - A list of positions at which ticks should be placed
    # Second argument -  A list of labels to place at the given locations
    plt.xticks(ind + width / 2, ('Xtick1', 'Xtick3', 'Xtick3'))
    
    # Finding the best position for legends and putting it
    plt.legend(loc='best')
    plt.show()
    

提交回复
热议问题