Simple Line plot¶
In [17]:
unrate = pd.read_csv('unrate.csv')
unrate['DATE'] = pd.to_datetime(unrate['DATE'])
plt.plot(unrate["DATE"][0:12], unrate["VALUE"][0:12])
plt.xticks(rotation = 90)
plt.xlabel("Month")
plt.ylabel("Unemployment Rate")
plt.title("Monthly Unemployment Trends,1948")
plt.show()
Subplot¶
In [19]:
import matplotlib.pyplot as plt
fig = plt.figure(figsize = (10,8))
ax1 = fig.add_subplot(3,3,1)
ax2 = fig.add_subplot(3,3,2)
ax3 = fig.add_subplot(3,3,3)
ax4 = fig.add_subplot(3,3,4)
ax5 = fig.add_subplot(3,3,5)
Sample multiple plot¶
In [15]:
fig = plt.figure(figsize=(12, 6))
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
ax1.plot(unrate["DATE"].iloc[0:12],unrate["VALUE"].iloc[0:12])
ax2.plot(unrate["DATE"].iloc[12:24],unrate["VALUE"].iloc[12:24])
plt.show()
Comparing across more years¶
In [27]:
fig = plt.figure(figsize = ( 12,12))
ax1 = fig.add_subplot(5,1,1)
ax2 = fig.add_subplot(5,1,2)
ax3 = fig.add_subplot(5,1,3)
ax4 = fig.add_subplot(5,1,4)
ax5 = fig.add_subplot(5,1,5)
ax1.plot(unrate["DATE"][0:12], unrate["VALUE"][0:12])
ax2.plot(unrate["DATE"][12:24], unrate["VALUE"][12:24])
ax3.plot(unrate["DATE"][24:36], unrate["VALUE"][24:36])
ax4.plot(unrate["DATE"][36:48], unrate["VALUE"][36:48])
ax5.plot(unrate["DATE"][48:60], unrate["VALUE"][48:60])
Out[27]:
Overlaying Line Charts¶
In [31]:
# Fixing date values
unrate['MONTH'] = unrate['DATE'].dt.month
fig = plt.figure(figsize=(10,6))
plt.plot(unrate["MONTH"][0:12], unrate["VALUE"][0:12], c = "red")
plt.plot(unrate["MONTH"][12:24], unrate["VALUE"][12:24], c = "blue")
plt.plot(unrate["MONTH"][24:36], unrate["VALUE"][24:36], c = "green")
plt.plot(unrate["MONTH"][36:48], unrate["VALUE"][36:48], c = "orange")
plt.plot(unrate["MONTH"][48:60], unrate["VALUE"][48:60], c = "black")
Out[31]:
Adding a Legend¶
In [33]:
fig = plt.figure(figsize=(10,6))
plt.plot(unrate["MONTH"][0:12], unrate["VALUE"][0:12], c = "red", label = "1948")
plt.plot(unrate["MONTH"][12:24], unrate["VALUE"][12:24], c = "blue", label = "1949")
plt.plot(unrate["MONTH"][24:36], unrate["VALUE"][24:36], c = "green", label = "1950")
plt.plot(unrate["MONTH"][36:48], unrate["VALUE"][36:48], c = "orange", label = "1951")
plt.plot(unrate["MONTH"][48:60], unrate["VALUE"][48:60], c = "black", label = "1952")
plt.legend(loc = "upper left")
Out[33]:
Another nice way :¶
In [35]:
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.title("Monthly Unemployment Trends, 1948-1952")
plt.xlabel("Month, Integer")
plt.ylabel("Unemployment Rate, Percent")
plt.show()
No comments :
Post a Comment