简体   繁体   English

如何使用 Python 绘制这些数据?

[英]How can I plot this data using Python?

I need to create a bar graph of the unemployment rate for every month since 1948 using python.我需要使用 python 创建自 1948 年以来每个月的失业率条形图。 I also need to use a comma delimited file (.csv) but the file contains multiple commas and I'm unsure of how to use the split command to separate each variable.我还需要使用逗号分隔的文件 (.csv),但该文件包含多个逗号,我不确定如何使用 split 命令来分隔每个变量。 Below I have pasted my code so far and the file I need to split and sort.到目前为止,我已经粘贴了我的代码以及我需要拆分和排序的文件。 I'm unsure of where to go from here, and any advice would be greatly appreciated.我不确定从这里去哪里,任何建议将不胜感激。 Thanks.谢谢。

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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5.4,5.4,5.5,5.3,5.2,5.1,5.0,5.0,5.0,5.0 2016,4.5.0,4 ,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, , , 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()

I would start by using Numpy to read the data, which is far easier than processing the file manually.我会从使用 Numpy 读取数据开始,这比手动处理文件要容易得多。 A small example using a subset of your data:一个使用数据子集的小例子:

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)

在此处输入图片说明

Or, to put something more useful on the x-axis:或者,在 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
years = f[:,0].astype(np.int)
data  = f[:,1:].reshape(-1)    

# Plot!
pl.figure()
pl.bar(np.arange(data.size), data)

# Set years as x-labels
ax=pl.gca()
ax.set_xticks(np.arange(0,data.size,12))
ax.set_xticklabels(years)

在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM