import pandas as pd
import matplotlib.pyplot as plt
unrate = pd.read_csv('unrate.csv')
unrate['DATE'] = pd.to_datetime(unrate['DATE'])
first_twelve = unrate[0:12]
plt.plot(first_twelve['DATE'], first_twelve['VALUE'])
plt.xticks(rotation=90)
plt.xlabel('Month')
plt.ylabel('Unemployment Rate')
plt.title('Monthly Unemployment Trends, 1948')
plt.show()
import matplotlib.pyplot as plt
fig = plt.figure()#指认默认画图的区间,画图区域
ax1 = fig.add_subplot(3,2,1)#指定三行两列的第一个
ax2 = fig.add_subplot(3,2,2)
ax2 = fig.add_subplot(3,2,6)
plt.show()
import numpy as np
#fig = plt.figure()
fig = plt.figure(figsize=(6, 3))#figsize(长度多大,宽度多大)
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
ax1.plot(np.random.randint(1,5,5), np.arange(5))#对第一个制图进行操作,传入一个随机值,np.random.randint(1,5,5)产生[1,5)之间的五个整数
ax2.plot(np.arange(10)*3, np.arange(10))
plt.show()
unrate['MONTH'] = unrate['DATE'].dt.month
unrate['MONTH'] = unrate['DATE'].dt.month
fig = plt.figure(figsize=(6,3))
plt.plot(unrate[0:12]['MONTH'], unrate[0:12]['VALUE'], c='red')#c函数颜色
plt.plot(unrate[12:24]['MONTH'], unrate[12:24]['VALUE'], c='blue')
plt.show()
fig = plt.figure(figsize=(10,6))
colors = ['red', 'blue', 'green', 'orange', 'black']
for i in range(5):
start_index = i*12
end_index = (i+1)*12
subset = unrate[start_index:end_index]
plt.plot(subset['MONTH'], subset['VALUE'], c=colors[i])
plt.show()
fig = plt.figure(figsize=(10,6))
colors = ['red', 'blue', 'green', 'orange', 'black']
for i in range(5):
start_index = i*12
end_index = (i+1)*12
subset = unrate[start_index:end_index]
label = str(1948 + i)
plt.plot(subset['MONTH'], subset['VALUE'], c=colors[i], label=label)
plt.legend(loc='best')#自己定义框在什么位置
#print help(plt.legend)
plt.show()
fig = plt.figure(figsize=(10,6))
colors = ['red', 'blue', 'green', 'orange', 'black']
for i in range(5):
start_index = i*12
end_index = (i+1)*12
subset = unrate[start_index:end_index]
label = str(1948 + i)
plt.plot(subset['MONTH'], subset['VALUE'], c=colors[i], label=label)
plt.legend(loc='upper left')
plt.xlabel('Month, Integer')
plt.ylabel('Unemployment Rate, Percent')
plt.title('Monthly Unemployment Trends, 1948-1952')
plt.show()
unrate.csv
DATE | VALUE |
1948/1/1 | 3.4 |
1948/2/1 | 3.8 |
1948/3/1 | 4 |
1948/4/1 | 3.9 |
1948/5/1 | 3.5 |
1948/6/1 | 3.6 |
1948/7/1 | 3.6 |
1948/8/1 | 3.9 |
1948/9/1 | 3.8 |
1948/10/1 | 3.7 |
1948/11/1 | 3.8 |
1948/12/1 | 4 |
1949/1/1 | 4.3 |
1949/2/1 | 4.7 |
1949/3/1 | 5 |
1949/4/1 | 5.3 |
1949/5/1 | 6.1 |
1949/6/1 | 6.2 |
1949/7/1 | 6.7 |
1949/8/1 | 6.8 |
1949/9/1 | 6.6 |
1949/10/1 | 7.9 |
1949/11/1 | 6.4 |
1949/12/1 | 6.6 |
1950/1/1 | 6.5 |
1950/2/1 | 6.4 |
1950/3/1 | 6.3 |
1950/4/1 | 5.8 |
1950/5/1 | 5.5 |
1950/6/1 | 5.4 |
1950/7/1 | 5 |
1950/8/1 | 4.5 |
1950/9/1 | 4.4 |
1950/10/1 | 4.2 |
1950/11/1 | 4.2 |
1950/12/1 | 4.3 |
1951/1/1 | 3.7 |
1951/2/1 | 3.4 |
1951/3/1 | 3.4 |
1951/4/1 | 3.1 |
1951/5/1 | 3 |
1951/6/1 | 3.2 |
1951/7/1 | 3.1 |
1951/8/1 | 3.1 |
1951/9/1 | 3.3 |
1951/10/1 | 3.5 |
1951/11/1 | 3.5 |
1951/12/1 | 3.1 |
1952/1/1 | 3.2 |
1952/2/1 | 3.1 |
1952/3/1 | 2.9 |
1952/4/1 | 2.9 |
1952/5/1 | 3 |
1952/6/1 | 3 |
1952/7/1 | 3.2 |
1952/8/1 | 3.4 |
1952/9/1 | 3.1 |
1952/10/1 | 3 |
1952/11/1 | 2.8 |
1952/12/1 | 2.7 |
1953/1/1 | 2.9 |
1953/2/1 | 2.6 |
1953/3/1 | 2.6 |
1953/4/1 | 2.7 |
1953/5/1 | 2.5 |
1953/6/1 | 2.5 |
1953/7/1 | 2.6 |
1953/8/1 | 2.7 |
1953/9/1 | 2.9 |
1953/10/1 | 3.1 |
1953/11/1 | 3.5 |
1953/12/1 | 4.5 |
1954/1/1 | 4.9 |
1954/2/1 | 5.2 |
1954/3/1 | 5.7 |
1954/4/1 | 5.9 |
1954/5/1 | 5.9 |
1954/6/1 | 5.6 |
1954/7/1 | 5.8 |
1954/8/1 | 6 |
1954/9/1 | 6.1 |
1954/10/1 | 5.7 |
1954/11/1 | 5.3 |
1954/12/1 | 5 |
1955/1/1 | 4.9 |
1955/2/1 | 4.7 |
1955/3/1 | 4.6 |
1955/4/1 | 4.7 |
1955/5/1 | 4.3 |
1955/6/1 | 4.2 |
1955/7/1 | 4 |
1955/8/1 | 4.2 |
1955/9/1 | 4.1 |
1955/10/1 | 4.3 |
1955/11/1 | 4.2 |
1955/12/1 | 4.2 |
1956/1/1 | 4 |
1956/2/1 | 3.9 |
1956/3/1 | 4.2 |
1956/4/1 | 4 |
1956/5/1 | 4.3 |
1956/6/1 | 4.3 |
1956/7/1 | 4.4 |
1956/8/1 | 4.1 |
1956/9/1 | 3.9 |
1956/10/1 | 3.9 |
1956/11/1 | 4.3 |
1956/12/1 | 4.2 |
1957/1/1 | 4.2 |
1957/2/1 | 3.9 |
1957/3/1 | 3.7 |
1957/4/1 | 3.9 |
1957/5/1 | 4.1 |
1957/6/1 | 4.3 |
1957/7/1 | 4.2 |
1957/8/1 | 4.1 |
1957/9/1 | 4.4 |
1957/10/1 | 4.5 |
1957/11/1 | 5.1 |
1957/12/1 | 5.2 |