简体   繁体   中英

Plot bar chart in python using csv data in pandas & Matplotlib

My bar chart is not showing correctly. I have csv data as below. I would like to plot bar chart using group by date and categories Key value with Count. So, every date will be group by and it will categories the key value with their Count. Please assist with below code. I am new and learning python myself.

My csv collect_data.csv data:

Date,Key,Count
14-10-2020,created,5
14-10-2020,moved,3
14-10-2020,modified,3
14-10-2020,deleted,5
17-10-2020,created,25
17-10-2020,moved,6
17-10-2020,modified,13
17-10-2020,deleted,25
18-10-2020,created,13
18-10-2020,modified,7
18-10-2020,moved,1
18-10-2020,deleted,13

My current bar chart:

我当前的条形图

My code:

import matplotlib.pyplot as plt
import pandas as pd

def display_dashboard():
    try:
        df = pd.read_csv("collect_data.csv")
        df.head()
        df['Date'].value_counts().plot(kind='bar')

        plt.title('File System Changed Based on Type of Event')
        plt.ylabel('Total Count of Event Occurred')
        plt.xlabel("DATE")

        plt.show()

    except FileNotFoundError:
        print("Exception error: Missing files!")

IIUC you need to reshape your df first:

df.set_index(["Date", "Key"]).unstack("Key").plot(kind="bar", rot=0)

plt.show()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM