我需要使用python创建自1948年以来每个月失业率的条形图。我还需要使用逗号分隔文件(.csv),但该文件包含多个逗号,并且我不确定如何使用split命令分隔每个变量。下面我已经粘贴了我的代码和我需要分割和排序的文件。我不确定从这里去哪里,任何建议都将不胜感激。感谢。
1948,3.4,3.8,4.0,3.9,3.5,3.6,3.6,3.9,3.8,3.7,3.8,4.0 1949,4.3,4.7,5.0,5.3,6.1,6.2,6.7,6.8,6.6,7.9,6.4,6.6 1950,6.5,6.4,6.3,5.8,5.5,5.4,5.0,4.5,4.4,4.2,4.2,4.3 1951,3.7,3.4,3.4,3.1,3.0,3.2,3.1,3.1,3.3,3.5,3.5,3.1 1952,3.2,3.1,2.9,2.9,3.0,3.0,3.2,3.4,3.1,3.0,2.8,2.7 1953,2.9,2.6,2.6,2.7,2.5,2.5,2.6,2.7,2.9,3.1,3.5,4.5 1954,4.9,5.2,5.7,5.9,5.9,5.6,5.8,6.0,6.1,5.7,5.3,5.0 1955,4.9,4.7,4.6,4.7,4.3,4.2,4.0,4.2,4.1,4.3,4.2,4.2 1956,4.0,3.9,4.2,4.0,4.3,4.3,4.4,4.1,3.9,3.9,4.3,4.2 1957,4.2,3.9,3.7,3.9,4.1,4.3,4.2,4.1,4.4,4.5,5.1,5.2 1958,5.8,6.4,6.7,7.4,7.4,7.3,7.5,7.4,7.1,6.7,6.2,6.2 1959,6.0,5.9,5.6,5.2,5.1,5.0,5.1,5.2,5.5,5.7,5.8,5.3 1960,5.2,4.8,5.4,5.2,5.1,5.4,5.5,5.6,5.5,6.1,6.1,6.6 1961,6.6,6.9,6.9,7.0,7.1,6.9,7.0,6.6,6.7,6.5,6.1,6.0 1962,5.8,5.5,5.6,5.6,5.5,5.5,5.4,5.7,5.6,5.4,5.7,5.5 1963,5.7,5.9,5.7,5.7,5.9,5.6,5.6,5.4,5.5,5.5,5.7,5.5 1964,5.6,5.4,5.4,5.3,5.1,5.2,4.9,5.0,5.1,5.1,4.8,5.0 1965,4.9,5.1,4.7,4.8,4.6,4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.9,5.0,5.0,5.0,4.9 2006,4.7,4.8,4.7,4.7,4.6,4.6,4.7,4.7,4.5,4.4,4.5,4.4 2007,4.6,4.5,4.4,4.5,4.4,4.6,4.7,4.6,4.7,4.7,4.7,5.0 2008,5.0,4.9,5.1,5.0,5.4,5.6,5.8,6.1,6.1,6.5,6.8,7.3 2009,7.8,8.3,8.7,9.0,9.4,9.5,9.5,9.6,9.8,10.0,9.9,9.9 2010,9.8,9.8,9.9,9.9,9.6,9.4,9.4,9.5,9.5,9.4,9.8,9.3 2011,9.1,9.0,9.0,9.1,9.0,9.1,9.0,9.0,9.0,8.8,8.6,8.5 2012,8.3,8.3,8.2,8.2,8.2,8.2,8.2,8.1,7.8,7.8,7.7,7.9 2013,8.0,7.7,7.5,7.6,7.5,7.5,7.3,7.3,7.2,7.2,6.9,6.7 2014,6.6,6.7,6.7,6.2,6.3,6.1,6.2,6.2,5.9,5.7,5.8,5.6 2015,5.7,5.5,5.4,5.4,5.5,5.3,5.2,5.1,5.0,5.0,5.0,5.0 2016,4.9,4.9,5.0,5.0,4.7,4.9,4.9,4.9,4.9,4.8,4.6,4.7
2017,4.8,4.7,4.5,4.4,4.3,4.4,4.3,4.4,4.2 ,,,
import matplotlib.pyplot as plt
f_in = ('unemployment.csv', 'rt')
X_labels = []
Y = []
for line in f_in:
line = line.strip()
month, year = line.split(',')
X_labels.append(month)
Y.append(int(year))
f_in.close()
X = list(range(1, len(Y) + 1))
plt.bar(X, Y, tick_label = X_labels, align = 'center')
plt.xlabel('Month')
plt.ylabel('Year')
plt.grid(True)
plt.show()
答案 0 :(得分:1)
我首先使用Numpy来读取数据,这比手动处理文件要容易得多。使用数据子集的一个小例子:
᪻ 1ABB COMBINING PARENTHESES ABOVE
᪾ 1ABE COMBINING PARENTHESES OVERLAY
⁽ 207D SUPERSCRIPT LEFT PARENTHESIS
⁾ 207E SUPERSCRIPT RIGHT PARENTHESIS
₍ 208D SUBSCRIPT LEFT PARENTHESIS
₎ 208E SUBSCRIPT RIGHT PARENTHESIS
⏜ 23DC TOP PARENTHESIS
⏝ 23DD BOTTOM PARENTHESIS
⟮ 27EE MATHEMATICAL LEFT FLATTENED PARENTHESIS
⟯ 27EF MATHEMATICAL RIGHT FLATTENED PARENTHESIS
⦅ 2985 LEFT WHITE PARENTHESIS
⦆ 2986 RIGHT WHITE PARENTHESIS
⸨ 2E28 LEFT DOUBLE PARENTHESIS
⸩ 2E29 RIGHT DOUBLE PARENTHESIS
﴾ FD3E ORNATE LEFT PARENTHESIS
﴿ FD3F ORNATE RIGHT PARENTHESIS
︵ FE35 PRESENTATION FORM FOR VERTICAL LEFT PARENTHESIS
︶ FE36 PRESENTATION FORM FOR VERTICAL RIGHT PARENTHESIS
﹙ FE59 SMALL LEFT PARENTHESIS
﹚ FE5A SMALL RIGHT PARENTHESIS
( FF08 FULLWIDTH LEFT PARENTHESIS
) FF09 FULLWIDTH RIGHT PARENTHESIS
⦅ FF5F FULLWIDTH LEFT WHITE PARENTHESIS
⦆ FF60 FULLWIDTH RIGHT WHITE PARENTHESIS
或者,在x轴上放一些更有用的东西:
import numpy as np
import matplotlib.pylab as pl
# Load file with Numpy as 2D array
f = np.loadtxt('file.txt', delimiter=',')
# Slice array (remove year column), and reshape to 1D
data = f[:,1:].reshape(-1)
# Plot!
pl.figure()
pl.bar(np.arange(data.size), data)