我試圖讓我的代碼儘可能簡單,這絕不是優雅的,但在這裏你去:
import csv
import matplotlib.pyplot as plt
### Making test CSV file ###
data = [['2011/12/15,5'],['2011/12/11,4'],['2011/12/19,2'],['2011/12/16,3'],['2011/12/20,8'],['2011/12/14,4'],['2011/12/10,10'],['2011/12/9,7']]
with open('test.csv', 'wb') as f:
writer = csv.writer(f)
for i in data:
writer.writerow(i)
### Extract data from CSV ###
with open('test.csv', 'rb') as n:
reader = csv.reader(n)
dates = []
freq = []
for row in reader:
values = row[0].split(',')
dates.append(values[0])
freq.append(values[1])
### Do plot ###
false_x = [x for x in range(len(dates))]
plt.plot(false_x,freq, 'o-')
plt.xticks(range(len(dates)), (dates), rotation=45)
# plt.axis([xmin, xmax, ymin, ymax]) - sets axes limits on graph
plt.axis([-1, 8, 0, 11])
plt.show()
這使得:
你想如何表示數據?直方圖?示例數據是從CSV文件複製並粘貼的嗎?我假設你在同一個單元格中有X和Y值? – Harpal 2012-04-17 22:07:02
另外,數據全部在一行上嗎? – Harpal 2012-04-17 22:11:40
Harpal - 感謝您的迴應。理想情況下,將其放在折線圖中會很棒。這對於X和Y來說是正確的,但我可以按照其存儲在CSV中的方式進行修改,使其更容易以其他方式生成。謝謝。 – user1314011 2012-04-17 22:13:51