簡體   English   中英

如何使用 Python 繪制這些數據?

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

我需要使用 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.4.0 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.5.3.5 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.3.5,3.7 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.4,2.4.3 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.5.2,4.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.8,5.2 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.6.7,6.5 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. 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.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 2006,4.7,4.8.8 ,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,9,5.0,4.4 ,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,9.9,9.8 ,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.2.2,8 ,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.7,6 ,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.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,,,,

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()

我會從使用 Numpy 讀取數據開始,這比手動處理文件要容易得多。 一個使用數據子集的小例子:

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)

在此處輸入圖片說明

或者,在 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