【问题标题】:Reading data from csv and create a graph从 csv 读取数据并创建图表
【发布时间】:2017-09-11 17:10:05
【问题描述】:

我有一个 csv 文件,其中包含以下格式的数据 -

Issue_Type     DateTime
Issue1          03/07/2011 11:20:44
Issue2          01/05/2011 12:30:34
Issue3          01/01/2011 09:44:21
...             ...

我能够读取此 csv 文件,但我无法实现的是根据数据绘制图表或更确切地说是趋势。

例如 - 我正在尝试绘制一个图表,其中 X 轴为日期时间(仅限月份),Y 轴为 #of Issues。因此,我会用 3 条线表示当月每个类别下的问题模式的线条图趋势。

我真的没有用于绘制图表的代码,因此无法共享任何代码,但到目前为止我只阅读 csv 文件。我不确定如何进一步绘制图表

PS:我不打算使用 python - 因为我之前使用 python 解析了 csv,所以我想使用该语言,但是如果有使用其他语言的更简单的方法 - 我也会开放探索.

【问题讨论】:

  • 如果使用 Python,Matplotlib 将是一个好的开始。
  • 你有代码来计算问题吗?您需要在绘图之前这样做

标签: python csv plot graph trend


【解决方案1】:

一种方法是使用带有 pandas 的数据框。

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

df = pd.read_csv("D:\Programmes Python\Data\Data_csv.txt",sep=";")  #Reads the csv
df.index = pd.to_datetime(df["DateTime"]) #Set the index of the dataframe to the DateTime column
del df["DateTime"] #The DateTime column is now useless

fig, ax = plt.subplots()
ax.plot(df.index,df["Issue_Type"])
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m'))  #This will only show the month number on the graph

这假设 Issue1/2/3 是整数,我假设它们是因为我并不真正理解它们应该是什么。

编辑:这应该可以解决问题,它不漂亮,可能可以优化,但效果很好:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

df = pd.read_csv("D:\Programmes Python\Data\Data_csv.txt",sep=";")
df.index = pd.to_datetime(df["DateTime"])
del df["DateTime"]
list=[]
for Issue in df["Issue_Type"]:
    list.append(int(Issue[5:]))
df["Issue_number"]=list

fig, ax = plt.subplots()
ax.plot(df.index,df["Issue_number"])
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m'))
plt.show()

【讨论】:

  • 我正在尝试使用示例数据运行代码;请原谅我太天真了,但脚本没有打印任何图表。
  • 并且 issueType 不是整数,而是示例 csv 中描述的字符。不过,我试图通过将我的 csv 更改为包含 issueType 下的整数来了解代码的工作原理
  • 我稍微更改了代码,并创建了一个提取问题编号的列(我认为这是您想要做的),我在最后添加了一个 plt.show() ,脚本现在也应该为您工作。 Ps:我在我的csv文件上使用的分隔符是“;”,如果你的不同,在pd.read_csv命令中更改它
【解决方案2】:

您需要做的第一件事是将日期时间字段解析为日期/时间。尝试使用dateutil.parser

接下来,您需要统计每个月每种类型的问题数量。最简单的做法是维护每个问题类型的列表,然后遍历每一列,查看它是哪个月份和哪个问题类型,然后增加相应的计数器。

当您有这样一个按问题类型排序的问题频率计数时,您可以简单地将它们与日期进行对比,如下所示:

import matplotlib.pyplot as plt
import datetime as dt

dates = []
for year in range(starting_year, ending_year):
    for month in range(1, 12):
        dates.append(dt.datetime(year=year, month=month, day=1))

formatted_dates = dates.DateFormatter('%b') # Format dates to only show month names
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(issues[0], dates) # To plot just issues of type 1
ax.plot(issues[1], dates) # To plot just issues of type 2
ax.plot(issues[2], dates) # To plot just issues of type 3
ax.xaxis.set_major_formatter(formatted_dates) # Format X tick labels
plt.show()
plt.close()

【讨论】:

    【解决方案3】:

    老实说,我只会使用 R.check this link out 下载/设置 R & RStudio。

    data <- read.csv(file="c:/yourdatafile.csv", header=TRUE, sep=",")
    attach(data)
    data$Month <- format(as.Date(data$DateTime), "%m")    
    plot(DateTime, Issue_Type)
    

    【讨论】:

      猜你喜欢
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多