0

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 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!")
Ch3steR
  • 20,090
  • 4
  • 28
  • 58

1 Answers1

0

IIUC you need to reshape your df first:

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

plt.show()

enter image description here

Henry Yik
  • 22,275
  • 4
  • 18
  • 40
  • Thanks, this is what I need and your suggestion. I am learning and applying those to my code. – YoungBrain Oct 19 '20 at 06:40
  • if I want to change the colour of the graph, how can I do that and can we display the total count report on the right corner in the graph? – YoungBrain Oct 20 '20 at 06:02
  • See [this](https://stackoverflow.com/questions/11927715/how-to-give-a-pandas-matplotlib-bar-graph-custom-colors) and [this](https://stackoverflow.com/questions/33149428/modify-the-legend-of-pandas-bar-plot). – Henry Yik Oct 20 '20 at 06:10