Feel free to read the code on GitHub
In [1]:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
In [2]:
df=pd.read_csv('c:/1/economics.txt')
df.head()
Out[2]:
In [3]:
df.dtypes
Out[3]:
In [4]:
x = df['date']
y1 = df['psavert']
y2 = df['unemploy']
In [6]:
# Plot Line1 (Left Y Axis)
fig, ax1 = plt.subplots(1,1,figsize=(16,9), dpi= 80)
ax1.plot(x, y1, color='tab:red')
# Plot Line2 (Right Y Axis)
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
ax2.plot(x, y2, color='tab:blue')
# Decorations
# ax1 (left Y axis)
ax1.set_xlabel('Year', fontsize=20)
ax1.tick_params(axis='x', rotation=0, labelsize=12)
ax1.set_ylabel('Personal Savings Rate', color='tab:red', fontsize=20)
ax1.tick_params(axis='y', rotation=0, labelcolor='tab:red' )
ax1.grid(alpha=.4)
# ax2 (right Y axis)
ax2.set_ylabel("# Unemployed (1000's)", color='tab:blue', fontsize=20)
ax2.tick_params(axis='y', labelcolor='tab:blue')
ax2.set_xticks(np.arange(0, len(x), 60))
ax2.set_xticklabels(x[::60], rotation=90, fontdict={'fontsize':10})
ax2.set_title("Personal Savings Rate vs Unemployed: Plotting in Secondary Y Axis", fontsize=22)
fig.tight_layout()
plt.show()
In [52]:
x = df['date']
AA = df['psavert']
BB = df['unemploy']
In [53]:
from matplotlib import rc
rc('mathtext', default='regular')
ax = fig.add_subplot(111)
ax.plot(x, AA, '-', label = 'Year')
# Plot Line1 (Left Y Axis)
fig, ax1 = plt.subplots(1,1,figsize=(8,4), dpi= 280)
ax1.plot(x, AA, color='red', alpha=0.4)
# Plot Line2 (Right Y Axis)
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
ax2.plot(x, BB, color='grey', alpha=0.4)
# Decorations
# ax1 (left Y axis)
ax1.set_xlabel('Year', fontsize=20)
ax1.tick_params(axis='x', rotation=90, labelsize=8)
ax1.set_ylabel('Personal Savings Rate', color='red', fontsize=12, alpha=0.4)
ax1.tick_params(axis='y', rotation=0, labelcolor='red' )
ax1.grid(alpha=.4)
ax1.set_ylim(0,18)
# ax2 (right Y axis)
ax2.set_ylabel("Perceptions of corruption", color='grey', fontsize=12, alpha=0.4)
ax2.tick_params(axis='y', labelcolor='grey')
ax2.set_xticks(np.arange(0, len(x), 60))
ax2.set_xticklabels(x[::60], rotation=90, fontdict={'fontsize':10})
ax2.set_title("Iraq 2008-2017", fontsize=15, alpha=0.4)
fig.tight_layout()
ax2.set_ylim(0, 16000)
plt.show()
In [7]:
df2 = pd.read_csv('c:/1/WorldHappinessReport_2005-2019.csv')
IRQ = df2[df2['Country name']=='Iraq']
IRQ
#df2['Country name'].value_counts()
Out[7]:
I fill in the data gaps
In [8]:
IRQ['Freedom to make life choices'].fillna(method='ffill', inplace=True)
IRQ['Freedom to make life choices']
Out[8]:
In [46]:
x = IRQ['Year']
y1 = IRQ['Freedom to make life choices']
y2 = IRQ['Perceptions of corruption']
In [50]:
# Plot Line1 (Left Y Axis)
fig, ax1 = plt.subplots(1,1,figsize=(8,4), dpi= 280)
ax1.plot(x, y1,'rs-',color='red')
# Plot Line2 (Right Y Axis)
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
ax2.plot(x, y2,'go-', color='blue')
# Decorations
# ax1 (left Y axis)
ax1.set_xlabel('Year', fontsize=20)
ax1.tick_params(axis='x', rotation=0, labelsize=9)
ax1.set_ylabel('Freedom to make life choices', color='red', fontsize=12)
ax1.tick_params(axis='y', rotation=0, labelcolor='red' )
ax1.grid(alpha=.4)
# ax2 (right Y axis)
ax2.set_ylabel("Perceptions of corruption", color='blue', fontsize=12)
ax2.tick_params(axis='y', labelcolor='blue')
ax2.set_xticks(np.arange(0, len(x), 60))
ax2.set_xticklabels(x[::60], rotation=0, fontdict={'fontsize':10})
ax2.set_title("Iraq 2008-2017", fontsize=22)
fig.tight_layout()
plt.show()
In [11]:
df4 = pd.read_csv('c:/1/uforeports.csv')
df4.head()
Out[11]:
In [12]:
df4['Time'].isnull().sum()
Out[12]:
In [13]:
df4['Time'] = pd.to_datetime(df4.Time)
df4['Time'].head(3)
Out[13]:
In [14]:
df4['Year'] = df4['Time'].dt.year
df4['Year'].head()
Out[14]:
In [15]:
circle = ['DISK', 'CIRCLE', 'SPHERE', 'FIREBALL', 'OVAL', 'TEARDROP']
nocircle = ['TRIANGLE','RECTANGLE','DIAMOND','CHEVRON', ]
CI = df4[df4['Shape Reported'].isin(circle)]
NOCI = df4[df4['Shape Reported'].isin(nocircle)]
In [16]:
CII = CI.pivot_table(index='Year', values='Time', aggfunc='count').reset_index().set_index('Year')
CII.rename(columns = {'Time': 'Circle'}, inplace=True)
NOCII = NOCI.pivot_table(index='Year', values='Time', aggfunc='count').reset_index().set_index('Year')
NOCII.rename(columns = {'Time': 'NO-Circle'}, inplace=True)
NOCII.head(3)
Out[16]:
In [17]:
result3 = pd.concat([CII, NOCII], axis=1, sort=False)
In [18]:
result3.head(4)
Out[18]:
In [19]:
x = result3.index
y1 = result3['Circle']
y2 = result3['NO-Circle']
In [20]:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular')
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y1, '-', label = 'Swdown')
ax2 = ax.twinx()
ax2.plot(x, y2, '-r', label = 'temp')
ax.legend(loc=0)
ax.grid()
ax.set_xlabel("Time (h)")
ax.set_ylabel(r"Cyrkle")
ax2.set_ylabel(r"NO-Circle")
ax2.set_ylim(0, 800)
ax.set_ylim(0,800)
plt.show()
In [25]:
import pandas as pd
import seaborn as sns
import matplotlib as plt
df7 = pd.read_csv('c:/8/ShenyangPM20100101_20151231.csv')
df7.head(3)
Out[25]:
In [26]:
df7.shape
Out[26]:
In [27]:
df.reset_index(inplace=True)
df7.index
Out[27]:
In [28]:
#from datetime import datetime
#df7['Date'] = df7.apply(lambda row: datetime(row['year'], row['month'], row['day']), axis=1)
In [29]:
df7.head()
Out[29]:
In [30]:
df7['Date2'] = pd.to_datetime(df7[['year','month','day']])
df7.head()
Out[30]:
In [31]:
#df7.reset_index()
df7.set_index('Date2')
Out[31]:
In [32]:
df7.sample(8)
Out[32]:
In [33]:
PKP = df7.pivot_table(index='Date2', values=['TEMP','PRES'], aggfunc='mean').reset_index()
PKP.head()
Out[33]:
In [34]:
PKP.dtypes
Out[34]:
In [35]:
x = PKP['Date2']
y1 = PKP['PRES']
y2 = PKP['TEMP']
In [36]:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular')
fig = plt.figure(figsize=(8,4), dpi= 280)
ax = fig.add_subplot(111)
ax.plot(x, y1, '-', label = 'Pressure')
ax2 = ax.twinx()
ax2.plot(x, y2, '-r', label = 'temp')
ax.legend(loc=0)
ax.grid()
ax.set_xlabel("Days")
ax.set_ylabel(r"Pressure")
ax2.set_ylabel(r"Temperature")
#ax2.set_ylim(0, 800)
#ax.set_ylim(0,800)
plt.show()
In [37]:
PKP2 = PKP[PKP['Date2'].dt.year==2011]
In [38]:
x = PKP2['Date2']
y1 = PKP2['PRES']
y2 = PKP2['TEMP']
In [39]:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular')
fig = plt.figure(figsize=(8,4), dpi= 280)
ax = fig.add_subplot(111)
ax.plot(x, y1, '-', label = 'Pressure')
ax2 = ax.twinx()
ax2.plot(x, y2, '-r', label = 'temp')
ax.legend(loc=0)
ax.grid()
ax.set_xlabel("Days")
ax.set_ylabel(r"Pressure")
ax2.set_ylabel(r"Temperature")
#ax2.set_ylim(0, 800)
#ax.set_ylim(0,800)
plt.show()
In [40]:
PRL = df7[df7['Date2'].dt.year==2015]
PRL2 = PRL.pivot_table(index='Date2', values=['TEMP','PM_Xiaoheyan'], aggfunc='mean').reset_index()
PRL2.head()
Out[40]:
In [41]:
x = PRL2['Date2']
y1 = PRL2['PM_Xiaoheyan']
y2 = PRL2['TEMP']
In [42]:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular')
fig = plt.figure(figsize=(8,4), dpi= 280)
ax = fig.add_subplot(111)
ax.plot(x, y1, '-', label = 'PM_Xiaoheyan', color='black')
ax2 = ax.twinx()
ax2.plot(x, y2, '-r', label = 'temp', color='red')
ax.legend(loc=0)
ax.grid()
ax.set_xlabel("Days")
ax.set_ylabel(r"PM_Xiaoheyan")
ax2.set_ylabel(r"Temperature")
#ax2.set_ylim(0, 800)
ax.set_ylim(0,300)
plt.show()
In [43]:
x = PRL2['Date2']
y1 = PRL2['PM_Xiaoheyan']
y2 = PRL2['TEMP']
In [44]:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular')
fig = plt.figure(figsize=(8,4), dpi= 280)
ax = fig.add_subplot(111)
ax.plot(x, y1, '-', label = 'PM_Xiaoheyan',color='grey')
ax2 = ax.twinx()
ax2.plot(x, y2, '--', label = 'temp',color='red')
ax.legend(loc=0)
ax.grid()
ax.set_xlabel("months")
ax.set_ylabel(r"PM_Xiaoheyan")
ax2.set_ylabel(r"Temperature")
#ax2.set_ylim(0, 100)
#ax.set_ylim(-500,300)
plt.show()





