Python Create Bar Chart Comparing 2 sets of data

前端 未结 1 1366
旧巷少年郎
旧巷少年郎 2021-01-13 00:40

I have a notebook with 2* bar charts, one is winter data & one is summer data. I have counted the total of all the crimes and plotted them in a bar chart, using code:

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-13 01:10

    The following generates dummies of your data and does the grouped bar chart you wanted:

    import random
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    s = "Crime Type Summer|Crime Type Winter".split("|")
    
    # Generate dummy data into a dataframe
    j = {x: [random.choice(["ASB", "Violence", "Theft", "Public Order", "Drugs"]
                           ) for j in range(300)] for x in s}
    df = pd.DataFrame(j)
    
    index = np.arange(5)
    bar_width = 0.35
    
    fig, ax = plt.subplots()
    summer = ax.bar(index, df["Crime Type Summer"].value_counts(), bar_width,
                    label="Summer")
    
    winter = ax.bar(index+bar_width, df["Crime Type Winter"].value_counts(),
                     bar_width, label="Winter")
    
    ax.set_xlabel('Category')
    ax.set_ylabel('Incidence')
    ax.set_title('Crime incidence by season, type')
    ax.set_xticks(index + bar_width / 2)
    ax.set_xticklabels(["ASB", "Violence", "Theft", "Public Order", "Drugs"])
    ax.legend()
    
    plt.show()
    

    With this script I got:

    You can check out the demo in the matplotlib docs here: https://matplotlib.org/gallery/statistics/barchart_demo.html

    The important thing to note is the index!

    index = np.arange(5) # Set an index of n crime types
    ...
    summer = ax.bar(index, ...)
    winter = ax.bar(index+bar_width, ...)
    ...
    ax.set_xticks(index + bar_width / 2)
    

    These are the lines that arrange the bars on the horizontal axis so that they are grouped together.

    0 讨论(0)
提交回复
热议问题