简体   繁体   English

使用pandas和Matplotlib中的csv数据在python中绘制条形图

[英]Plot bar chart in python using csv data in pandas & Matplotlib

My bar chart is not showing correctly.我的条形图显示不正确。 I have csv data as below.我有如下的 csv 数据。 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.我是新手,自己在学习 python。

My csv collect_data.csv data:我的 csv collect_data.csv数据:

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: IIUC 你需要先重塑你的 df:

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

plt.show()

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 python 中使用 matplotlib 和 pandas 绘制 CSV 数据 - How to plot CSV data using matplotlib and pandas in python plot a stacked bar chart using matplotlib keeping the pandas dataframe order as it is using python - plot a stacked bar chart using matplotlib keeping the pandas dataframe order as it is using python matplotlib:在条形图上绘制多列熊猫数据框 - matplotlib: plot multiple columns of pandas data frame on the bar chart 如何在 python 中使用 matplotlib 对时间序列数据进行 plot 堆积条形图? - How to plot a stacked bar chart on time series data using matplotlib in python? 如何在python中使用matplotlib绘制叠加条形图? - How to plot a superimposed bar chart using matplotlib in python? 使用 matplotlib 和 CSV 文件中的数据制作条形图 - Making a bar chart by using matplotlib with data from CSV file 使用matplotlib在python中绘制漂亮的条形图 - Pretty plot bar chart in python with matplotlib 在 python 中使用 matplotlib 绘制多个分组条形图 - Plot multiple grouped bar chart with matplotlib in python 使用Pandas DataFrame和Matplotlib将CSV文件中的数据处理到绘图中 - Using pandas dataframe and matplotlib to manipulate data from a csv file into a plot 有没有办法使用 Python 中的 matplotlib/pandas 模块根据 csv 文件提供的数据更改条形图的颜色? - Is there a way to change the color of a bar graph based on the data provided from a csv file, using the matplotlib/pandas module in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM