Perfect Plots: Subplots

Feel free to read the code on GitHub

In [1]:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
 

Economics

In [2]:
df=pd.read_csv('c:/1/economics.txt')
df.head()
Out[2]:
  date pce pop psavert uempmed unemploy
0 1967-07-01 507.4 198712 12.5 4.5 2944
1 1967-08-01 510.5 198911 12.5 4.7 2945
2 1967-09-01 516.3 199113 11.7 4.6 2958
3 1967-10-01 512.9 199311 12.5 4.9 3143
4 1967-11-01 518.1 199498 12.5 4.7 3066
In [3]:
df.dtypes
Out[3]:
date         object
pce         float64
pop           int64
psavert     float64
uempmed     float64
unemploy      int64
dtype: object
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()
 
 

WorldHappinessReport_2005-2019

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]:
  Unnamed: 0 Country name Year Life Ladder Log GDP per capita Social support Healthy life expectancy at birth Freedom to make life choices Generosity Perceptions of corruption … GINI index (World Bank estimate) GINI index (World Bank estimate), average 2000-16 gini of household income reported in Gallup, by wp5-year Most people can be trusted, Gallup Most people can be trusted, WVS round 1981-1984 Most people can be trusted, WVS round 1989-1993 Most people can be trusted, WVS round 1994-1998 Most people can be trusted, WVS round 1999-2004 Most people can be trusted, WVS round 2005-2009 Most people can be trusted, WVS round 2010-2014
678 678 Iraq 2008 4.589845 9.410621 0.744366 58.320000 0.385769 -0.097140 0.909882 … NaN 0.2905 NaN NaN NaN NaN NaN 0.464239 0.39137 0.3
679 679 Iraq 2009 4.775317 9.417306 0.861746 58.959999 0.431468 -0.234837 0.854340 … NaN 0.2905 0.330139 0.160026 NaN NaN NaN 0.464239 0.39137 0.3
680 680 Iraq 2010 5.065462 9.450742 0.854118 59.599998 0.419064 -0.159973 0.858735 … NaN 0.2905 0.279507 NaN NaN NaN NaN 0.464239 0.39137 0.3
681 681 Iraq 2011 4.725366 9.492628 0.750749 59.360001 0.347414 -0.105158 0.780027 … NaN 0.2905 0.264834 NaN NaN NaN NaN 0.464239 0.39137 0.3
682 682 Iraq 2012 4.659509 9.590554 0.730118 59.119999 0.314565 -0.056443 0.789191 … 0.295 0.2905 0.337229 NaN NaN NaN NaN 0.464239 0.39137 0.3
683 683 Iraq 2013 4.725017 9.630601 0.728285 58.880001 NaN -0.086607 0.709726 … NaN 0.2905 0.447405 NaN NaN NaN NaN 0.464239 0.39137 0.3
684 684 Iraq 2014 4.541502 9.604973 0.725151 58.639999 0.646007 -0.038037 0.726008 … NaN 0.2905 0.570643 NaN NaN NaN NaN 0.464239 0.39137 0.3
685 685 Iraq 2015 4.493377 9.620651 0.684435 58.400002 0.599460 -0.019349 0.762167 … NaN 0.2905 0.631784 NaN NaN NaN NaN 0.464239 0.39137 0.3
686 686 Iraq 2016 4.412537 9.695360 0.718957 59.000000 0.666160 -0.088390 0.798866 … NaN 0.2905 0.548373 NaN NaN NaN NaN 0.464239 0.39137 0.3
687 687 Iraq 2017 4.462399 9.659120 0.695109 59.599998 0.627722 -0.037715 0.757109 … NaN 0.2905 0.550368 NaN NaN NaN NaN 0.464239 0.39137 0.3

10 rows × 27 columns

 

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']
C:ProgramDataAnaconda3libsite-packagespandascoregeneric.py:6130: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._update_inplace(new_data)
Out[8]:
678    0.385769
679    0.431468
680    0.419064
681    0.347414
682    0.314565
683    0.314565
684    0.646007
685    0.599460
686    0.666160
687    0.627722
Name: Freedom to make life choices, dtype: float64
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()
 

ufo reports

In [11]:
df4 = pd.read_csv('c:/1/uforeports.csv')
df4.head()
Out[11]:
  Unnamed: 0 City Colors Reported Shape Reported State Time
0 0 Ithaca NaN TRIANGLE NY 6/1/1930 22:00
1 1 Willingboro NaN OTHER NJ 6/30/1930 20:00
2 2 Holyoke NaN OVAL CO 2/15/1931 14:00
3 3 Abilene NaN DISK KS 6/1/1931 13:00
4 4 New York Worlds Fair NaN LIGHT NY 4/18/1933 19:00
In [12]:
df4['Time'].isnull().sum()
Out[12]:
0
In [13]:
df4['Time'] = pd.to_datetime(df4.Time)
df4['Time'].head(3)
Out[13]:
0   1930-06-01 22:00:00
1   1930-06-30 20:00:00
2   1931-02-15 14:00:00
Name: Time, dtype: datetime64[ns]
In [14]:
df4['Year'] = df4['Time'].dt.year
df4['Year'].head()
Out[14]:
0    1930
1    1930
2    1931
3    1931
4    1933
Name: Year, dtype: int64
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]:
  NO-Circle
Year  
1930 1
1942 1
1943 1
In [17]:
result3 = pd.concat([CII, NOCII], axis=1, sort=False)
In [18]:
result3.head(4)
Out[18]:
  Circle NO-Circle
Year    
1930 NaN 1.0
1931 2.0 NaN
1934 2.0 NaN
1935 2.0 NaN
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()
 

Shenyang air pollution

Source: https://zhuanlan.zhihu.com/p/61513436

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]:
  No year month day hour season PM_Taiyuanjie PM_US Post PM_Xiaoheyan DEWP HUMI PRES TEMP cbwd Iws precipitation Iprec
0 1 2010 1 1 0 4 NaN NaN NaN -26.0 69.79 1024.0 -22.0 NE 1.0289 NaN NaN
1 2 2010 1 1 1 4 NaN NaN NaN -26.0 76.26 1024.0 -23.0 NE 2.5722 NaN NaN
2 3 2010 1 1 2 4 NaN NaN NaN -27.0 69.56 1023.0 -23.0 NE 5.1444 NaN NaN
In [26]:
df7.shape
Out[26]:
(52584, 17)
In [27]:
df.reset_index(inplace=True)
df7.index
Out[27]:
RangeIndex(start=0, stop=52584, step=1)
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]:
  No year month day hour season PM_Taiyuanjie PM_US Post PM_Xiaoheyan DEWP HUMI PRES TEMP cbwd Iws precipitation Iprec
0 1 2010 1 1 0 4 NaN NaN NaN -26.0 69.79 1024.0 -22.0 NE 1.0289 NaN NaN
1 2 2010 1 1 1 4 NaN NaN NaN -26.0 76.26 1024.0 -23.0 NE 2.5722 NaN NaN
2 3 2010 1 1 2 4 NaN NaN NaN -27.0 69.56 1023.0 -23.0 NE 5.1444 NaN NaN
3 4 2010 1 1 3 4 NaN NaN NaN -27.0 69.56 1023.0 -23.0 NE 7.7166 NaN NaN
4 5 2010 1 1 4 4 NaN NaN NaN -27.0 69.56 1022.0 -23.0 NE 9.7744 NaN NaN
In [30]:
df7['Date2'] = pd.to_datetime(df7[['year','month','day']])
df7.head()
Out[30]:
  No year month day hour season PM_Taiyuanjie PM_US Post PM_Xiaoheyan DEWP HUMI PRES TEMP cbwd Iws precipitation Iprec Date2
0 1 2010 1 1 0 4 NaN NaN NaN -26.0 69.79 1024.0 -22.0 NE 1.0289 NaN NaN 2010-01-01
1 2 2010 1 1 1 4 NaN NaN NaN -26.0 76.26 1024.0 -23.0 NE 2.5722 NaN NaN 2010-01-01
2 3 2010 1 1 2 4 NaN NaN NaN -27.0 69.56 1023.0 -23.0 NE 5.1444 NaN NaN 2010-01-01
3 4 2010 1 1 3 4 NaN NaN NaN -27.0 69.56 1023.0 -23.0 NE 7.7166 NaN NaN 2010-01-01
4 5 2010 1 1 4 4 NaN NaN NaN -27.0 69.56 1022.0 -23.0 NE 9.7744 NaN NaN 2010-01-01
In [31]:
#df7.reset_index()
df7.set_index('Date2')
Out[31]:
  No year month day hour season PM_Taiyuanjie PM_US Post PM_Xiaoheyan DEWP HUMI PRES TEMP cbwd Iws precipitation Iprec
Date2                                  
2010-01-01 1 2010 1 1 0 4 NaN NaN NaN -26.0 69.79 1024.0 -22.0 NE 1.0289 NaN NaN
2010-01-01 2 2010 1 1 1 4 NaN NaN NaN -26.0 76.26 1024.0 -23.0 NE 2.5722 NaN NaN
2010-01-01 3 2010 1 1 2 4 NaN NaN NaN -27.0 69.56 1023.0 -23.0 NE 5.1444 NaN NaN
2010-01-01 4 2010 1 1 3 4 NaN NaN NaN -27.0 69.56 1023.0 -23.0 NE 7.7166 NaN NaN
2010-01-01 5 2010 1 1 4 4 NaN NaN NaN -27.0 69.56 1022.0 -23.0 NE 9.7744 NaN NaN
2010-01-01 6 2010 1 1 5 4 NaN NaN NaN -26.0 76.26 1022.0 -23.0 NE 11.8322 NaN NaN
2010-01-01 7 2010 1 1 6 4 NaN NaN NaN -25.0 76.46 1021.0 -22.0 NE 14.4044 NaN NaN
2010-01-01 8 2010 1 1 7 4 NaN NaN NaN -24.0 70.26 1021.0 -20.0 NE 16.9766 NaN NaN
2010-01-01 9 2010 1 1 8 4 NaN NaN NaN -23.0 70.49 1021.0 -19.0 NE 19.0344 NaN NaN
2010-01-01 10 2010 1 1 9 4 NaN NaN NaN -22.0 70.71 1021.0 -18.0 NE 21.6066 NaN NaN
2010-01-01 11 2010 1 1 10 4 NaN NaN NaN -20.0 77.39 1022.0 -17.0 NE 24.1788 NaN NaN
2010-01-01 12 2010 1 1 11 4 NaN NaN NaN -18.0 77.75 1021.0 -15.0 NE 27.2655 NaN NaN
2010-01-01 13 2010 1 1 12 4 NaN NaN NaN -17.0 77.92 1020.0 -14.0 NE 29.8377 NaN NaN
2010-01-01 14 2010 1 1 13 4 NaN NaN NaN -16.0 78.10 1019.0 -13.0 NE 32.9244 NaN NaN
2010-01-01 15 2010 1 1 14 4 NaN NaN NaN -15.0 84.87 1019.0 -13.0 NE 35.4966 NaN NaN
2010-01-01 16 2010 1 1 15 4 NaN NaN NaN -15.0 78.27 1019.0 -12.0 NE 38.5833 NaN NaN
2010-01-01 17 2010 1 1 16 4 NaN NaN NaN -15.0 78.27 1019.0 -12.0 NE 41.1555 NaN NaN
2010-01-01 18 2010 1 1 17 4 NaN NaN NaN -15.0 78.27 1020.0 -12.0 NE 43.2133 NaN NaN
2010-01-01 19 2010 1 1 18 4 NaN NaN NaN -16.0 78.10 1020.0 -13.0 NE 45.7855 NaN NaN
2010-01-01 20 2010 1 1 19 4 NaN NaN NaN -17.0 77.92 1021.0 -14.0 NE 48.3577 NaN NaN
2010-01-01 21 2010 1 1 20 4 NaN NaN NaN -17.0 84.62 1021.0 -15.0 NE 50.4155 NaN NaN
2010-01-01 22 2010 1 1 21 4 NaN NaN NaN -19.0 77.57 1022.0 -16.0 NE 51.9588 NaN NaN
2010-01-01 23 2010 1 1 22 4 NaN NaN NaN -20.0 77.39 1022.0 -17.0 NE 53.5021 NaN NaN
2010-01-01 24 2010 1 1 23 4 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2010-01-02 25 2010 1 2 0 4 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2010-01-02 26 2010 1 2 1 4 NaN NaN NaN -23.0 70.49 1023.0 -19.0 NE 1.5433 NaN NaN
2010-01-02 27 2010 1 2 2 4 NaN NaN NaN -24.0 70.26 1023.0 -20.0 NE 2.5722 NaN NaN
2010-01-02 28 2010 1 2 3 4 NaN NaN NaN -24.0 76.65 1023.0 -21.0 NE 3.6011 NaN NaN
2010-01-02 29 2010 1 2 4 4 NaN NaN NaN -24.0 76.65 1024.0 -21.0 NE 4.6300 NaN NaN
2010-01-02 30 2010 1 2 5 4 NaN NaN NaN -24.0 70.26 1024.0 -20.0 NE 6.1733 NaN NaN
… … … … … … … … … … … … … … … … … …
2015-12-30 52555 2015 12 30 18 4 190.0 204.0 207.0 -9.0 73.45 1030.0 -5.0 NW 28.0000 0.0 0.0
2015-12-30 52556 2015 12 30 19 4 198.0 209.0 213.0 -10.0 67.88 1031.0 -5.0 NW 32.0000 0.0 0.0
2015-12-30 52557 2015 12 30 20 4 202.0 208.0 222.0 -11.0 67.65 1031.0 -6.0 NW 35.0000 0.0 0.0
2015-12-30 52558 2015 12 30 21 4 192.0 211.0 227.0 -11.0 73.05 1032.0 -7.0 NW 37.0000 0.0 0.0
2015-12-30 52559 2015 12 30 22 4 186.0 206.0 202.0 -11.0 78.94 1032.0 -8.0 NE 1.0000 0.0 0.0
2015-12-30 52560 2015 12 30 23 4 183.0 206.0 192.0 -13.0 78.61 1032.0 -10.0 cv 1.0000 0.0 0.0
2015-12-31 52561 2015 12 31 0 4 167.0 196.0 206.0 -13.0 92.22 1032.0 -12.0 SW 1.0000 0.0 0.0
2015-12-31 52562 2015 12 31 1 4 171.0 185.0 224.0 -13.0 92.22 1032.0 -12.0 cv 1.0000 0.0 0.0
2015-12-31 52563 2015 12 31 2 4 193.0 188.0 253.0 -14.0 84.99 1033.0 -12.0 SE 2.0000 0.0 0.0
2015-12-31 52564 2015 12 31 3 4 214.0 199.0 302.0 -15.0 84.87 1032.0 -13.0 SE 4.0000 0.0 0.0
2015-12-31 52565 2015 12 31 4 4 258.0 260.0 304.0 -15.0 92.09 1032.0 -14.0 SE 6.0000 0.0 0.0
2015-12-31 52566 2015 12 31 5 4 287.0 290.0 295.0 -17.0 84.62 1033.0 -15.0 SW 1.0000 0.0 0.0
2015-12-31 52567 2015 12 31 6 4 305.0 323.0 316.0 -16.0 92.02 1033.0 -15.0 cv 1.0000 0.0 0.0
2015-12-31 52568 2015 12 31 7 4 312.0 352.0 328.0 -17.0 91.95 1033.0 -16.0 SE 1.0000 0.0 0.0
2015-12-31 52569 2015 12 31 8 4 281.0 338.0 380.0 -17.0 84.62 1034.0 -15.0 cv 0.0000 0.0 0.0
2015-12-31 52570 2015 12 31 9 4 351.0 368.0 365.0 -13.0 92.22 1034.0 -12.0 SE 2.0000 0.0 0.0
2015-12-31 52571 2015 12 31 10 4 362.0 435.0 368.0 -11.0 85.35 1034.0 -9.0 SE 4.0000 0.0 0.0
2015-12-31 52572 2015 12 31 11 4 212.0 391.0 351.0 -9.0 79.26 1033.0 -6.0 SW 2.0000 0.0 0.0
2015-12-31 52573 2015 12 31 12 4 149.0 279.0 187.0 -8.0 79.42 1033.0 -5.0 SW 4.0000 0.0 0.0
2015-12-31 52574 2015 12 31 13 4 159.0 199.0 142.0 -9.0 63.20 1032.0 -3.0 SW 6.0000 0.0 0.0
2015-12-31 52575 2015 12 31 14 4 148.0 180.0 117.0 -10.0 58.41 1032.0 -3.0 SW 8.0000 0.0 0.0
2015-12-31 52576 2015 12 31 15 4 148.0 152.0 118.0 -10.0 54.23 1031.0 -2.0 SW 10.0000 0.0 0.0
2015-12-31 52577 2015 12 31 16 4 134.0 171.0 124.0 -10.0 58.41 1031.0 -3.0 SE 1.0000 0.0 0.0
2015-12-31 52578 2015 12 31 17 4 124.0 163.0 204.0 -9.0 68.11 1030.0 -4.0 SE 3.0000 0.0 0.0
2015-12-31 52579 2015 12 31 18 4 148.0 157.0 331.0 -11.0 78.94 1031.0 -8.0 cv 0.0000 0.0 0.0
2015-12-31 52580 2015 12 31 19 4 162.0 166.0 435.0 -10.0 92.42 1031.0 -9.0 SE 2.0000 0.0 0.0
2015-12-31 52581 2015 12 31 20 4 255.0 259.0 429.0 -10.0 79.10 1030.0 -7.0 SE 5.0000 0.0 0.0
2015-12-31 52582 2015 12 31 21 4 266.0 368.0 361.0 -10.0 79.10 1030.0 -7.0 SE 8.0000 0.0 0.0
2015-12-31 52583 2015 12 31 22 4 202.0 319.0 342.0 -10.0 79.10 1028.0 -7.0 SE 11.0000 NaN NaN
2015-12-31 52584 2015 12 31 23 4 NaN 275.0 NaN -9.0 79.26 1028.0 -6.0 SE 12.0000 0.0 0.0

52584 rows × 17 columns

In [32]:
df7.sample(8)
Out[32]:
  No year month day hour season PM_Taiyuanjie PM_US Post PM_Xiaoheyan DEWP HUMI PRES TEMP cbwd Iws precipitation Iprec Date2
21742 21743 2012 6 24 22 2 NaN NaN NaN 20.0 88.45 1006.0 22.0 SE 2.0 NaN NaN 2012-06-24
36038 36039 2014 2 10 14 4 41.0 45.0 46.0 -23.0 24.59 1033.0 -6.0 NE 82.0 0.0 0.0 2014-02-10
30139 30140 2013 6 9 19 2 11.0 5.0 9.0 11.0 46.78 1009.0 23.0 NW 23.0 0.0 0.0 2013-06-09
14929 14930 2011 9 15 1 3 NaN NaN NaN 17.0 93.89 1010.0 18.0 NE 2.0 0.0 0.0 2011-09-15
26707 26708 2013 1 17 19 4 89.0 NaN 188.0 -20.0 71.16 1036.0 -16.0 SE 3.0 0.0 0.0 2013-01-17
15165 15166 2011 9 24 21 3 NaN NaN NaN 9.0 59.29 1014.0 17.0 SE 3.0 0.0 0.0 2011-09-24
23018 23019 2012 8 17 2 2 NaN NaN NaN 19.0 93.98 1010.0 20.0 SE 12.0 0.0 0.0 2012-08-17
35752 35753 2014 1 29 16 4 68.0 44.0 46.0 -8.0 44.19 1008.0 3.0 SW 87.0 0.0 0.0 2014-01-29
In [33]:
PKP = df7.pivot_table(index='Date2', values=['TEMP','PRES'], aggfunc='mean').reset_index()
PKP.head()
Out[33]:
  Date2 PRES TEMP
0 2010-01-01 1021.173913 -17.347826
1 2010-01-02 1026.173913 -20.086957
2 2010-01-03 1026.833333 -21.416667
3 2010-01-04 1024.375000 -18.833333
4 2010-01-05 1029.375000 -20.416667
In [34]:
PKP.dtypes
Out[34]:
Date2    datetime64[ns]
PRES            float64
TEMP            float64
dtype: object
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()
C:ProgramDataAnaconda3libsite-packagespandasplotting_converter.py:129: FutureWarning: Using an implicitly registered datetime converter for a matplotlib plotting method. The converter was registered by pandas on import. Future versions of pandas will require you to explicitly register matplotlib converters.

To register the converters:
	>>> from pandas.plotting import register_matplotlib_converters
	>>> register_matplotlib_converters()
  warnings.warn(msg, FutureWarning)
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]:
  Date2 PM_Xiaoheyan TEMP
0 2015-01-01 30.000000 -13.208333
1 2015-01-02 44.375000 -12.416667
2 2015-01-03 57.333333 -4.208333
3 2015-01-04 90.076923 -5.000000
4 2015-01-05 81.125000 -3.833333
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()