绘制图表并找到最大值
plot graph and find maximum value
我正在尝试打开包含 java 中两列 "time and G" 的 excel 文件(也尝试使用 python)并绘制 G 与时间之间的图表.并找出 G 在什么时候最大和最小。请帮帮我!!!
您似乎没有太多编程经验,所以我尝试创建一个小示例。你说你想从 excel 获取数据,因为我不知道如何在 python 中读取 excel 我假设你可以为你的值创建一个 csv 文件。
import matplotlib
import matplotlib.pyplot as plt
from datetime import datetime
with open("pathtoyourfiel/data.csv") as f:
#read file to a list of lines
content = f.readlines()
#split each line at ","
data = [line.split(",") for line in content]
#convert date string to datetime (assuming format is correct)
dateStrings = [datetime.strptime(d[0], "%d %b %Y") for d in data]
#convert datetime to matplotlib.date
dates = matplotlib.dates.date2num(dateStrings)
#extract G value from split list, cast to int
g = [int(d[1]) for d in data]
#get max and min
maxValue = max(g)
minValue = min(g)
#get the date at the index of max/min
maxDate = dateStrings[g.index(maxValue)]
minDate = dateStrings[g.index(minValue)]
#set formatter for the date on the x axis
plt.gca().xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%m/%d/%Y'))
plt.plot(dates, g)
plt.show()
我的数据如下所示:
2015 年 6 月 15 日,14
2015 年 7 月 24 日,1
2015 年 10 月 30 日,5
我正在尝试打开包含 java 中两列 "time and G" 的 excel 文件(也尝试使用 python)并绘制 G 与时间之间的图表.并找出 G 在什么时候最大和最小。请帮帮我!!!
您似乎没有太多编程经验,所以我尝试创建一个小示例。你说你想从 excel 获取数据,因为我不知道如何在 python 中读取 excel 我假设你可以为你的值创建一个 csv 文件。
import matplotlib
import matplotlib.pyplot as plt
from datetime import datetime
with open("pathtoyourfiel/data.csv") as f:
#read file to a list of lines
content = f.readlines()
#split each line at ","
data = [line.split(",") for line in content]
#convert date string to datetime (assuming format is correct)
dateStrings = [datetime.strptime(d[0], "%d %b %Y") for d in data]
#convert datetime to matplotlib.date
dates = matplotlib.dates.date2num(dateStrings)
#extract G value from split list, cast to int
g = [int(d[1]) for d in data]
#get max and min
maxValue = max(g)
minValue = min(g)
#get the date at the index of max/min
maxDate = dateStrings[g.index(maxValue)]
minDate = dateStrings[g.index(minValue)]
#set formatter for the date on the x axis
plt.gca().xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%m/%d/%Y'))
plt.plot(dates, g)
plt.show()
我的数据如下所示:
2015 年 6 月 15 日,14
2015 年 7 月 24 日,1
2015 年 10 月 30 日,5