UBC ECON 323 Project

Zihao Wang

U.S.-China Trade War Analysis

Data Visualization & Prediction based on Political Variable Score

Gravity Model, Machine Learning, Lasso, Ridge, Neural Networks

***This project is a purely experiment, it does not have any intention to express any of my political idea or bias, nor create any of the reader's political interest

Given the time constraint, I was not able to get the precise Tariff Data. It was extremely exhausting to download the country-paired tariff data in a time-series manner, and it was even harder to merge in a short period of time. I then took a detour to examine and predictions based on some political indicators that may bring some new interests. The content can be more fruitful if I make the gravity model computable, such as CES, or other functions with homothetic features that can predict the trade flow by each commodity. I'll continue to enhance the flavor/richness of this project in the very near future.

Abstract

This project includes several visualizations on international trade flow, U.S. and China partner trade share in aggregated and disaggregated level by commodity code and state. Three detailed the tariff war timelines are also presented. Some simple regression and machine learning method are applied to answer the question how will he bilateral trade flow change if the country was assigned some different political scores. The dataset is extracted mainly from CEPII database and the USITC dynamic gravity model database. The merged data frame contains 194884 pairs of trade flows from 2005 to 2013. A precise U.S.-China bilateral trade flow and tariff data from 1992 to 2013 was also examined.

For the simple tariff analysis, the prediction is based on the current aggregate tariff level 21% for both countries, for which scenario, China's export decreased more than the U.S.'s, a valid reason from the given data is that China has never been levied high tariff by the U.S. for the past decade. Though, Similarly, for policy variables, China's export is also hurt more compared to the U.S. if higher hostility scores and trade sanction being assigned. By the end, I also present the USITC GME package designed for international trade analysis which I discovered by the end of finishing this project.

The detailed summary table is presented in the Conclusion Section.

Content

1. U.S. and China Top Trade Partners by Pie Chart

2.The Relative Trade Deficit Position, 45 Degree Line Plot

3.Trade War in Detail

3.1 5-digit End Use Code Goods Trade Value during the Trade War
3.2 Timeline and Lag
3.3 Visuliaztion on Tariff Trend and Adjustment Lag

4. Exports to China from the U.S. by States

4.1 Exports Value by States in 2018
4.2 Goods Exports (non-service) Relative Change in 2019 by State

5. Global Trade Network Visulization

6. Small Dataset with Precise Aggregate Tariff Data

6.1 Data Cleaning
6.2 Small Dataset Regression 

7. Large Dataset Gravity, Lasso, Neural Networks and Classification

7.1 Cleaning Data
7.2 Gravity Model Linear regression
7.3 Variance Inflation Factor (VIF)
7.4 Ridge Model
7.5 Lasso Regression
        Lasso Path
7.7 Gravity Linear V.S. Regularization (Ridge V.S. Lasso)
7.8 Test and Train
7.9 Neural Networks
        Visualization
        Data vs Test_n_Train vs Neural Networks vs Gravity Multilinear vs Lasso vs Simple Linear
7.10 Political Score
7.11 Hostility Level
7.12 Classfication

8. Model Estimation by USITC GME Package

9. Prediction Summary

10. Conclusion

11. Reference

Figure

matplotlib and Bokeh Pie Charts,

5-Digit End Use line Plot

Trade Balance Position Plot

Bokeh U.S. Exports Heat Map

Networkx Trade Network Plot

Trade War Timeline and Tariff line Plots

Model Selection Contractions Plots

Classification Plots

Install some pip.

In [ ]:
# Uncomment following line to install on colab.
#! pip install qeds fiona geopandas xgboost gensim folium pyLDAvis descartes
In [ ]:
# uncomment to install jupyterlab-manager. 
#!jupyter labextension install @jupyter-widgets/jupyterlab-manager
In [483]:
#install  jupyter_bokeh extension
#!jupyter labextension install @bokeh/jupyter_bokeh
Building jupyterlab assets (build:prod:minimize)
In [1]:
#!pip install gme
Collecting gme
  Downloading gme-1.2.tar.gz (21 kB)
Requirement already satisfied: pandas in ./opt/anaconda3/lib/python3.7/site-packages (from gme) (1.0.1)
Requirement already satisfied: statsmodels in ./opt/anaconda3/lib/python3.7/site-packages (from gme) (0.11.0)
Requirement already satisfied: scipy in ./opt/anaconda3/lib/python3.7/site-packages (from gme) (1.4.1)
Requirement already satisfied: patsy in ./opt/anaconda3/lib/python3.7/site-packages (from gme) (0.5.1)
Requirement already satisfied: python-dateutil>=2.6.1 in ./opt/anaconda3/lib/python3.7/site-packages (from pandas->gme) (2.8.1)
Requirement already satisfied: pytz>=2017.2 in ./opt/anaconda3/lib/python3.7/site-packages (from pandas->gme) (2019.3)
Requirement already satisfied: numpy>=1.13.3 in ./opt/anaconda3/lib/python3.7/site-packages (from pandas->gme) (1.18.1)
Requirement already satisfied: six in ./opt/anaconda3/lib/python3.7/site-packages (from patsy->gme) (1.14.0)
Building wheels for collected packages: gme
  Building wheel for gme (setup.py) ... done
  Created wheel for gme: filename=gme-1.2-py3-none-any.whl size=27290 sha256=7a1c230c9ee1836a9d82824b93cba8d57d4d32fe19ec4275c97a47a529ce4ca9
  Stored in directory: /Users/zihao/Library/Caches/pip/wheels/2e/dd/b4/f7f3d707ebd7a89d1c4a15751c0878251365296ceadbe5952b
Successfully built gme
Installing collected packages: gme
Successfully installed gme-1.2
In [404]:
#!pip install geopandas
Out[404]:
['Requirement already satisfied: geopandas in ./opt/anaconda3/lib/python3.7/site-packages (0.7.0)',
 'Requirement already satisfied: pyproj>=2.2.0 in ./opt/anaconda3/lib/python3.7/site-packages (from geopandas) (2.6.0)',
 'Requirement already satisfied: shapely in ./opt/anaconda3/lib/python3.7/site-packages (from geopandas) (1.7.0)',
 'Requirement already satisfied: fiona in ./opt/anaconda3/lib/python3.7/site-packages (from geopandas) (1.8.13.post1)',
 'Requirement already satisfied: pandas>=0.23.0 in ./opt/anaconda3/lib/python3.7/site-packages (from geopandas) (1.0.1)',
 'Requirement already satisfied: click-plugins>=1.0 in ./opt/anaconda3/lib/python3.7/site-packages (from fiona->geopandas) (1.1.1)',
 'Requirement already satisfied: cligj>=0.5 in ./opt/anaconda3/lib/python3.7/site-packages (from fiona->geopandas) (0.5.0)',
 'Requirement already satisfied: click<8,>=4.0 in ./opt/anaconda3/lib/python3.7/site-packages (from fiona->geopandas) (7.0)',
 'Requirement already satisfied: munch in ./opt/anaconda3/lib/python3.7/site-packages (from fiona->geopandas) (2.5.0)',
 'Requirement already satisfied: attrs>=17 in ./opt/anaconda3/lib/python3.7/site-packages (from fiona->geopandas) (19.3.0)',
 'Requirement already satisfied: six>=1.7 in ./opt/anaconda3/lib/python3.7/site-packages (from fiona->geopandas) (1.14.0)',
 'Requirement already satisfied: python-dateutil>=2.6.1 in ./opt/anaconda3/lib/python3.7/site-packages (from pandas>=0.23.0->geopandas) (2.8.1)',
 'Requirement already satisfied: numpy>=1.13.3 in ./opt/anaconda3/lib/python3.7/site-packages (from pandas>=0.23.0->geopandas) (1.18.1)',
 'Requirement already satisfied: pytz>=2017.2 in ./opt/anaconda3/lib/python3.7/site-packages (from pandas>=0.23.0->geopandas) (2019.3)']
In [481]:
import pandas as pd
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import (
    linear_model, metrics, neural_network, pipeline, model_selection
)
from sklearn.metrics import mean_squared_error, r2_score
import gme as gme
import qeds
qeds.themes.mpl_style();
plotly_template = qeds.themes.plotly_template()
colors = qeds.themes.COLOR_CYCLE




%matplotlib inline
# activate plot theme

import yaml

from bokeh.layouts import column
from bokeh.models import ColumnDataSource, Slider
from bokeh.plotting import figure
from bokeh.themes import Theme
from bokeh.io import show, output_notebook

Imports many datasets

In [356]:
df = pd.read_excel (r'/Users/zihao/Desktop/UBC/Econ 323/Project/TRADHIST_BITRADE_BITARIFF_3.xlsx')
df_2018_exports = pd.read_excel(r'/Users/zihao/Desktop/UBC/Econ 323/Project/2018_exports_all.xlsx')
df_2018_imports = pd.read_excel(r'/Users/zihao/Desktop/UBC/Econ 323/Project/2018_imports_all.xlsx')
df_tariff = pd.read_excel(r'/Users/zihao/Desktop/UBC/Econ 323/Project/U.S. China Average Tariff Change.xlsx')
df_us_partner = pd.read_excel(r'/Users/zihao/Desktop/UBC/Econ 323/Project/U.S. Trade Partner 2018.xlsx')
df_china_partner = pd.read_excel(r'/Users/zihao/Desktop/UBC/Econ 323/Project/China Trade Partner 2018.xlsx')
df_us_tariff_time = pd.read_excel(r'/Users/zihao/Desktop/UBC/Econ 323/Project/U.S. tariff on China.xlsx')
In [357]:
df_us_tariff_time = pd.read_excel(r'/Users/zihao/Desktop/UBC/Econ 323/Project/U.S. tariff on China.xlsx')
In [358]:
df_china_tariff_time =  pd.read_excel(r'/Users/zihao/Desktop/UBC/Econ 323/Project/China tariif on U.S.xlsx')
In [359]:
df_gravity_model = pd.read_csv(r'/Users/zihao/Desktop/UBC/Econ 323/Project/gravity dataset/DATASET/2004-2016 gravity.csv')
In [360]:
df_gravity_model2 = pd.read_stata(r'/Users/zihao/Desktop/UBC/Econ 323/Project/gravity dataset/DATASET/gravdata_cepii/gravdata.dta')
In [238]:
import numpy as np
background_1 = tuple(np.array([253, 238, 222]) / 255)

Let's firstly look at the bilateral trade data of the United States

In [221]:
df_us_partner
Out[221]:
Reporter Name Partner Name Export Share in Total Products (%) Import Share in Total Products (%) Trade Balance (US$ Thousand) Export (US$ Thousand) Import (US$ Thousand) Import Partner Share (%) Export Partner Share (%) AHS Simple Average (%) ... MFN Dutiable Tariff Lines Share (%) MFN Duty Free Tariff Lines Share (%) MFN Specific Tariff Lines Share (%) MFN AVE Tariff Lines Share (%) MFN MaxRate (%) MFN MinRate (%) MFN SpecificDuty Imports (US$ Thousand) MFN Dutiable Imports (US$ Thousand) MFN Duty Free Imports (US$ Thousand) No Of Tariff Agreement
0 United States Aruba 37.99 1.41 570308.23 601974.40 31666.17 0.00 0.04 0.74 ... 51.22 46.34 0.00 2.44 90.03 0 0.00 5422.55 4333.60 1.0
1 United States Afghanistan 24.22 2.91 1197093.01 1226885.15 29792.15 0.00 0.07 1.47 ... 56.18 30.19 0.21 13.42 76.52 0 1.06 16866.93 10514.38 2.0
2 United States Angola 18.71 0.40 -2243739.33 525185.65 2768924.98 0.11 0.03 1.03 ... 37.84 56.76 5.41 0.00 16.50 0 2487662.61 2553366.97 210040.74 3.0
3 United States Anguila 19.99 0.62 110551.09 115705.45 5154.36 0.00 0.01 0.73 ... 42.65 54.41 0.00 2.94 16.50 0 0.00 2396.21 2567.91 1.0
4 United States Albania 6.71 5.19 -11206.99 59200.12 70407.11 0.00 0.00 6.75 ... 65.92 24.97 0.12 8.99 350.00 0 38.91 65995.63 2639.05 1.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
226 United States Yemen 4.39 0.79 189077.98 191307.70 2229.72 0.00 0.01 3.11 ... 61.81 20.83 0.00 17.36 37.11 0 0.00 341.11 1852.67 2.0
227 United States South Africa 56.83 31.65 -3132357.73 5517377.25 8649734.98 0.33 0.33 0.63 ... 54.17 37.57 1.47 6.79 350.00 0 38849.58 3567909.24 4859075.84 3.0
228 United States Congo, Dem. Rep. 9.75 1.41 27678.64 78002.32 50323.68 0.00 0.00 0.69 ... 39.52 60.00 0.00 0.48 19.70 0 0.00 20667.85 28685.94 2.0
229 United States Zambia 10.35 1.08 4537.42 195379.27 190841.84 0.01 0.01 2.18 ... 59.60 36.36 0.00 4.04 350.00 0 0.00 138367.28 51342.50 4.0
230 United States Zimbabwe 5.78 1.70 -45324.59 33737.88 79062.47 0.00 0.00 3.85 ... 58.82 32.20 0.93 8.05 350.00 0 6198.63 70412.93 8226.68 1.0

231 rows × 34 columns

The U.S.'s trade balance with the world is

In [362]:
us_world_blance = df_us_partner.iloc[223]['Trade Balance (US$ Thousand)']
us_world_blance
Out[362]:
-946129553.57

1. U.S. and China Top Trade Partners by Pie Chart

Then look at the U.S.'s and China's top trade partners in pie charts. Also observe the source of the U.S. trade deficit

In [363]:
#clean 

#pull out some redudant aggregate regions from the dataset
region = ["World","Latin America & Caribbean","North America",'East Asia & Pacific',
          'Europe & Central Asia','Middle East & North Africa','Sub-Saharan Africa','Other Asia, nes','South Asia']
#U.S. top 10 exports destination 
df_us_top10_destination = df_us_partner.nlargest(16,'Export Partner Share (%)')
df_us_top10_destination_1 = df_us_top10_destination[['Partner Name','Export Partner Share (%)']]
df_us_top10_destination_1.drop(df_us_top10_destination_1.
                               loc[df_us_top10_destination_1['Partner Name'].isin(region)].index, inplace=True)
df_us_top10_destination_2 = df_us_top10_destination_1.drop(223).reset_index().drop('index',axis=1)
others_us_share = 100 - df_us_top10_destination_2['Export Partner Share (%)'].sum()
other_us_row = pd.DataFrame([['Others',others_us_share]], columns=['Partner Name','Export Partner Share (%)'])
df_us_top10_destination_3 = pd.concat([other_us_row,df_us_top10_destination_2]).set_index('Partner Name')



#U.S. top 10 imports origin
df_us_top10_origin = df_us_partner.nlargest(16,'Import Partner Share (%)')
df_us_top10_origin_1 = df_us_top10_origin[['Partner Name','Import Partner Share (%)']]
df_us_top10_origin_1.drop(df_us_top10_origin_1.
                        loc[df_us_top10_origin_1['Partner Name'].isin(region)].index, inplace=True)
df_us_top10_origin_2 = df_us_top10_origin_1.drop(223).reset_index().drop('index',axis=1)
others_share_imports = 100 - df_us_top10_origin_2['Import Partner Share (%)'].sum()
other_row_imports = pd.DataFrame([['Others',others_share_imports]], columns=['Partner Name','Import Partner Share (%)'])
df_us_top10_origin_3 = pd.concat([other_row_imports,df_us_top10_origin_2]).set_index('Partner Name')

#China top 10 exports destination 
df_china_top10_destination = df_china_partner.nlargest(16,'Export Partner Share (%)')
df_china_top10_destination_1 = df_china_top10_destination[['Partner Name','Export Partner Share (%)']]
df_china_top10_destination_1.drop(df_china_top10_destination_1.
                               loc[df_china_top10_destination_1['Partner Name'].isin(region)].index, inplace=True)
df_china_top10_destination_2 = df_china_top10_destination_1.drop(215).reset_index().drop('index',axis=1)
others_china_share = 100 - df_china_top10_destination_2['Export Partner Share (%)'].sum()
other_china_row = pd.DataFrame([['Others',others_china_share]], columns=['Partner Name','Export Partner Share (%)'])
df_china_top10_destination_3 = pd.concat([other_china_row,df_china_top10_destination_2]).set_index('Partner Name')

#China top 10 imports origin
df_china_top10_origin = df_china_partner.nlargest(17,'Import Partner Share (%)')
df_china_top10_origin_1 = df_china_top10_origin[['Partner Name','Import Partner Share (%)']]
df_china_top10_origin_1.drop(df_china_top10_origin_1.
                        loc[df_china_top10_origin_1['Partner Name'].isin(region)].index, inplace=True)
df_china_top10_origin_2 = df_china_top10_origin_1.drop(215).reset_index().drop('index',axis=1)
others_share_china_import = 100 - df_china_top10_origin_2['Import Partner Share (%)'].sum()
other_row_china_imports = pd.DataFrame([['Others',others_share_china_import]], columns=['Partner Name','Import Partner Share (%)'])
df_china_top10_origin_3 = pd.concat([other_row_china_imports,df_china_top10_origin_2]).set_index('Partner Name')

#we also clean the data of u.s. trade balance top 10 list 
df_us_balance = df_us_partner.nsmallest(12,'Trade Balance (US$ Thousand)')
df_deficit = df_us_balance[['Partner Name','Trade Balance (US$ Thousand)']]
df_deficit.drop(df_deficit.loc[df_deficit['Partner Name'].isin(region)].index, inplace=True)
df_us_balance_1 = df_deficit.drop(223).reset_index().drop('index',axis=1)
balance_other_country = us_world_blance - df_us_balance_1['Trade Balance (US$ Thousand)'].sum()
other_us_blance = pd.DataFrame([['Deficit From Other Countries',balance_other_country]], columns=['Partner Name','Trade Balance (US$ Thousand)'])
df_us_deficit = pd.concat([other_us_blance,df_us_balance_1]).set_index('Partner Name')

#for the sake of better plotting in bokeh I put others on the top
df_us_top10_destination_3
Out[363]:
Export Partner Share (%)
Partner Name
Others 38.22
Canada 18.00
Mexico 15.94
China 7.21
Japan 4.52
United Kingdom 3.98
Germany 3.44
Korea, Rep. 3.39
Netherlands 2.92
Brazil 2.38
In [364]:
df_us_top10_origin_3
Out[364]:
Import Partner Share (%)
Partner Name
Others 32.44
China 21.57
Mexico 13.37
Canada 12.47
Japan 5.59
Germany 4.91
Korea, Rep. 2.92
United Kingdom 2.36
Ireland 2.21
India 2.16
In [477]:
df_china_top10_destination_3
Out[477]:
Export Partner Share (%)
Partner Name
Others 38.19
United States 19.23
Hong Kong, China 12.15
Japan 5.90
Korea, Rep. 4.37
Vietnam 3.37
Germany 3.12
India 3.08
Netherlands 2.93
In [365]:
df_china_top10_origin_3
Out[365]:
Import Partner Share (%)
Partner Name
Others 52.42
Korea, Rep. 9.58
Japan 8.45
United States 7.31
Germany 4.98
Australia 4.92
Brazil 3.61
Vietnam 3.00
Malaysia 2.97
Russian Federation 2.76
In [366]:
#prep for bokeh some dictionaries
us_top10_destination = df_us_top10_destination_3['Export Partner Share (%)'].to_dict()
us_top10_origin = df_us_top10_origin_3['Import Partner Share (%)'].to_dict()
china_top10_destination = df_china_top10_destination_3['Export Partner Share (%)'].to_dict()
china_top10_origin = df_china_top10_origin_3['Import Partner Share (%)'].to_dict()
us_top10_destination
Out[366]:
{'Others': 38.21999999999999,
 'Canada': 18.0,
 'Mexico': 15.94,
 'China': 7.21,
 'Japan': 4.52,
 'United Kingdom': 3.98,
 'Germany': 3.44,
 'Korea, Rep.': 3.39,
 'Netherlands': 2.92,
 'Brazil': 2.38}
In [367]:
df_us_deficit = df_us_deficit.reindex(['Deficit From Other Countries', 'Mexico', 'Germany', 'Japan','Ireland','Vietnam','Italy','China'])
In [368]:
#reorder prepare for a better visualization for pie chart
df_us_deficit
Out[368]:
Trade Balance (US$ Thousand)
Partner Name
Deficit From Other Countries -1.556063e+08
Mexico -8.376046e+07
Germany -7.101312e+07
Japan -7.067617e+07
Ireland -4.695435e+07
Vietnam -4.160220e+07
Italy -3.346167e+07
China -4.430553e+08

The bokeh pie chart below is based on the example given by the bokeh library.

reference from the bokeh lib http://docs.bokeh.org/en/latest/docs/gallery/pie_chart.htm

In [449]:
#I manually looped over 

from math import pi

from bokeh.io import output_notebook, show
from bokeh.palettes import Category20c
from bokeh.plotting import figure
from bokeh.transform import cumsum
from bokeh.models import LabelSet, ColumnDataSource
from bokeh.layouts import row
from bokeh.layouts import gridplot


#1st U.S. destination plot
x = us_top10_destination
data = pd.Series(x).reset_index(name='value').rename(columns={'index':'country'})
data['angle'] = data['value']/data['value'].sum() * 2*pi
data['color'] = Category20c[len(x)]

s = figure(plot_height=350, title="U.S. Top 10 Export Destination by Percentage Point in 2018", toolbar_location=None,
           tools="hover", tooltips="@country: @value", x_range=(-0.5, 1.0))

s.wedge(x=0, y=1, radius=0.4,
        start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
        line_color="white", fill_color='color', legend_field='country', source=data)
data["country"] = data['country'].astype(str)
data["country"] = data["country"].str.pad(28, side = "left")
source = ColumnDataSource(data)

labels = LabelSet(x=0, y=1, text='country',
        angle=cumsum('angle', include_zero=True), source=source, render_mode='canvas')

s.add_layout(labels)



s.axis.axis_label=None
s.axis.visible=False
s.grid.grid_line_color = None


#2nd U.S. origin plot
x1 = us_top10_origin
data1 = pd.Series(x1).reset_index(name='value').rename(columns={'index':'country'})
data1['angle'] = data1['value']/data1['value'].sum() * 2*pi
data1['color'] = Category20c[len(x1)]

s1 = figure(plot_height=350, title="U.S. Top 10 Imports Country of Origin by Percentage Point in 2018", toolbar_location=None,
           tools="hover", tooltips="@country: @value", x_range=(-0.5, 1.0))

s1.wedge(x=0, y=1, radius=0.4,
        start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
        line_color="white", fill_color='color', legend_field='country', source=data1)
data1["country"] = data1['country'].astype(str)
data1["country"] = data1["country"].str.pad(28, side = "left")
source1 = ColumnDataSource(data1)

labels1 = LabelSet(x=0, y=1, text='country',
        angle=cumsum('angle', include_zero=True), source=source1, render_mode='canvas')

s1.add_layout(labels1)



s1.axis.axis_label=None
s1.axis.visible=False
s1.grid.grid_line_color = None

#3rd China Destination Plot
x2 = china_top10_destination
data2 = pd.Series(x2).reset_index(name='value').rename(columns={'index':'country'})
data2['angle'] = data2['value']/data2['value'].sum() * 2*pi
data2['color'] = Category20c[len(x2)]

s2 = figure(plot_height=350, title="China Top 10 Export Destination by Percentage Point in 2018", toolbar_location=None,
           tools="hover", tooltips="@country: @value", x_range=(-0.5, 1.0))

s2.wedge(x=0, y=1, radius=0.4,
        start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
        line_color="white", fill_color='color', legend_field='country', source=data2)
data2["country"] = data2['country'].astype(str)
data2["country"] = data2["country"].str.pad(28, side = "left")
source2 = ColumnDataSource(data2)

labels2 = LabelSet(x=0, y=1, text='country',
        angle=cumsum('angle', include_zero=True), source=source2, render_mode='canvas')

s2.add_layout(labels2)



s2.axis.axis_label=None
s2.axis.visible=False
s2.grid.grid_line_color = None

#4th China Origin Plot

x3 = china_top10_origin
data3 = pd.Series(x3).reset_index(name='value').rename(columns={'index':'country'})
data3['angle'] = data3['value']/data3['value'].sum() * 2*pi
data3['color'] = Category20c[len(x3)]

s3 = figure(plot_height=350, title="China Top 10 Imports Country of Origin by Percentage Point in 2018", toolbar_location=None,
           tools="hover", tooltips="@country: @value", x_range=(-0.5, 1.0))

s3.wedge(x=0, y=1, radius=0.4,
        start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
        line_color="white", fill_color='color', legend_field='country', source=data3)
data3["country"] = data3['country'].astype(str)
data3["country"] = data3["country"].str.pad(28, side = "left")
source3 = ColumnDataSource(data3)

labels3 = LabelSet(x=0, y=1, text='country',
        angle=cumsum('angle', include_zero=True), source=source3, render_mode='canvas')

s3.add_layout(labels3)



s3.axis.axis_label=None
s3.axis.visible=False
s3.grid.grid_line_color = None
##now we use grid to show results




#show the result by a 2*2 grid
grid = gridplot([s, s1, s2,s3], ncols=2)


# show the results
#show(row(s, s1))

output_notebook()

#reference from the bokeh lib http://docs.bokeh.org/en/latest/docs/gallery/pie_chart.html   
show(grid)

You can also check the bokeh video I attached to see the detail of the plot

In [459]:
from IPython.display import Image
Image("/Users/zihao/Desktop/UBC/Econ 323/Project/Pie Share.png")
Out[459]:

Top 8 deficit, slice China out.

In [381]:
#I chose a better visualization pallette?
labels = df_us_deficit.index.tolist()
df_normalize_percentage= df_us_deficit['Trade Balance (US$ Thousand)']/us_world_blance
sizes = df_normalize_percentage.tolist()
colors = ['#003f5c','#2f4b7c','#665191','#a05195','#d45087','#f95d6a','#ff7c43','#ffa600']

#split china out
explode = (0, 0, 0, 0,0,0,0,0.1)
fig, ax = plt.subplots(figsize = (15,10))
ax.pie(sizes, labels=labels,explode=explode, colors = colors, autopct='%1.1f%%',
        shadow=True, startangle=90)
ax.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.
ax.set_title('U.S. 2018 Trade Deficit Top 8 Partners',size = 20)

import matplotlib as mpl
mpl.rcParams['font.size'] = 15

2.The Relative Trade Deficit Position, 45 Degree Line Plot

In [382]:
df_2018_ex_im = pd.merge(df_2018_exports, df_2018_imports, on="Reporter Name")
df_2018_ex_im
Out[382]:
Reporter Name Exports Imports
0 Afghanistan 1.769009e+06 1.481318e+07
1 Albania 2.875860e+06 5.941287e+06
2 Algeria NaN NaN
3 Andorra 1.294482e+05 1.609394e+06
4 Angola 3.768468e+07 1.635306e+07
... ... ... ...
198 Venezuela NaN NaN
199 Vietnam NaN NaN
200 Yemen NaN NaN
201 Zambia 9.052165e+06 9.461739e+06
202 Zimbabwe 4.037203e+06 6.258856e+06

203 rows × 3 columns

In [383]:
df_2018_ex_im.set_index("Reporter Name",inplace=True)
In [384]:
df_2018_ex_im.loc["United States"]
Out[384]:
Exports    1.665303e+09
Imports    2.611432e+09
Name: United States, dtype: float64
In [385]:
df_2018_clean = df_2018_ex_im.dropna(axis=0)
df_2018_clean 
Out[385]:
Exports Imports
Reporter Name
Afghanistan 1.769009e+06 1.481318e+07
Albania 2.875860e+06 5.941287e+06
Andorra 1.294482e+05 1.609394e+06
Angola 3.768468e+07 1.635306e+07
Antigua and Barbuda 2.584108e+04 5.691552e+05
... ... ...
United States 1.665303e+09 2.611432e+09
Uruguay 7.498005e+06 8.893246e+06
Uzbekistan 1.091901e+07 1.731395e+07
Zambia 9.052165e+06 9.461739e+06
Zimbabwe 4.037203e+06 6.258856e+06

131 rows × 2 columns

In [386]:
null_sum_clean = df_2018_clean.isnull().sum()
null_sum_clean
Out[386]:
Exports    0
Imports    0
dtype: int64
In [387]:
df_2018_clean  = df_2018_clean / 1000000
In [388]:
df_2018_clean.loc["China"]["Exports"]
Out[388]:
2494.230194966
In [389]:
background_1 = tuple(np.array([253, 238, 222]) / 255)
def scatterlabels(xvar, yvar, ax):
    df_2018_clean.plot(xvar,yvar, kind="scatter", color= "blue", alpha=0.8, ax=ax, s=50)
    for idx in df_2018_clean.index :
        if idx == "China" :
            
            ax.annotate("China", (df_2018_clean.loc["China",xvar], df_2018_clean.loc["China",yvar]), color="black",size=20)
        
        elif idx == "United States" :
            
            ax.annotate("United States", (df_2018_clean.loc["United States",xvar], df_2018_clean.loc["United States",yvar]), color="black",size=20)
       
           
        
    return(ax)

fig, ax = plt.subplots(figsize=(20,10))
ax = scatterlabels("Exports","Imports",ax)
ax.set_xlabel('Exports',size=20)
ax.set_ylabel('Imports', size =20)
ax.set_title("2018 Imports Against Exports by Nations, by Million USD",size =20)
lims = [
    np.min([ax.get_xlim(), ax.get_ylim()]),  # min of both axes
    np.max([ax.get_xlim(), ax.get_ylim()]),  # max of both axes
]
# now plot both limits against eachother
# a 45 degree line 
ax.plot(lims, lims, 'k-', alpha=0.5, zorder=0)
ax.set_aspect('equal')
ax.set_xlim(lims)
ax.set_ylim(lims)
ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)
ax.annotate(
    "45 degree line",
    xy=(10, 20), xycoords="data", xytext=(2000,2000),
    annotation_clip=False,
    horizontalalignment="left",
    arrowprops={"visible": False},
    fontsize=12, fontweight="semibold", color = tuple(np.array([139,0,0]) / 255),size =10)

fig.tight_layout()

3.Trade War in Detail

3.1 5-digit End Use Code Goods Trade Value during the Trade War

In [628]:
import requests


url = 'https://www.census.gov/foreign-trade/statistics/product/enduse/imports/c5700.html?'
html = requests.get(url).content
df_list = pd.read_html(html)
df_end_use = df_list[-1]

df_end_use.to_csv('my data.csv')
In [649]:
df_end_use = df_end_use[(df_end_use != 0).all(1)]
df_end_use_1 =  df_end_use.nlargest(70,'Value 2019')
df_end_use_1 
Out[649]:
End-Use Code Value 2010 Value 2011 Value 2012 Value 2013 Value 2014 Value 2015 Value 2016 Value 2017 Value 2018 Value 2019
141 TOTAL 364952634 399371233 425619083 440430020 468474895 483201655 462419992 505220233 539675591 452243440
121 (41050) Cell phones and other household goods,... 32612041 40437973 52820617 58708992 64102970 64538065 61465082 70339967 71872376 64552117
84 (21300) Computers 39343305 47212554 47638124 47154020 46246719 43804679 40407803 45400673 47178858 42379802
124 (41120) Toys, games, and sporting goods 27817414 25306178 25115136 24632369 24432816 26355499 25044156 26726633 28222683 26512710
108 (40020) Apparel, textiles, nonwool or cotton 17317807 19754755 20619014 21554715 23924860 25645126 24143354 24114281 25140494 24453425
... ... ... ... ... ... ... ... ... ... ... ...
126 (41140) Musical instruments 655151 660364 674404 647305 708923 703271 708904 717117 777382 816483
122 (41100) Motorcycles and parts 133870 181086 229314 254189 260398 478429 466435 568479 842384 724828
74 (21110) Food, tobacco machinery 430348 509068 551661 585582 671607 725335 755540 806258 796777 681196
92 (22010) Parts-civilian aircraft 303580 413779 445631 509563 535630 534547 522628 600075 676175 651946
11 (00180) Other foods 339091 431406 495041 534875 475411 496359 513580 598441 711488 642395

70 rows × 11 columns

In [650]:
df_end_use1 = df_end_use_1.set_index('End-Use Code').T.astype(float).drop(['TOTAL'],axis=1)
df_end_use1
Out[650]:
End-Use Code (41050) Cell phones and other household goods, n.e.c. (21300) Computers (41120) Toys, games, and sporting goods (40020) Apparel, textiles, nonwool or cotton (21400) Telecommunications equipment (21301) Computer accessories (41000) Furniture, household goods, etc. (41030) Household appliances (30230) Other parts and accessories of vehicles (20005) Electric apparatus ... (12070) Tobacco, waxes, etc. (42110) Gem stones, other (50040) Other (movies, miscellaneous imports, and special transactions) (12500) Plastic materials (13020) Stone, sand, cement, etc. (41140) Musical instruments (41100) Motorcycles and parts (21110) Food, tobacco machinery (22010) Parts-civilian aircraft (00180) Other foods
Value 2010 32612041.0 39343305.0 27817414.0 17317807.0 16619084.0 28195127.0 13561429.0 9390599.0 7361394.0 11044289.0 ... 644778.0 1512192.0 161174.0 571061.0 782220.0 655151.0 133870.0 430348.0 303580.0 339091.0
Value 2011 40437973.0 47212554.0 25306178.0 19754755.0 17336848.0 27663608.0 13753109.0 10210570.0 9029238.0 12946856.0 ... 760624.0 1577880.0 203950.0 808861.0 799358.0 660364.0 181086.0 509068.0 413779.0 431406.0
Value 2012 52820617.0 47638124.0 25115136.0 20619014.0 19145602.0 28418685.0 14779785.0 11145330.0 11172211.0 11590260.0 ... 899706.0 1661028.0 225795.0 812741.0 853707.0 674404.0 229314.0 551661.0 445631.0 495041.0
Value 2013 58708992.0 47154020.0 24632369.0 21554715.0 20951280.0 28979113.0 15179327.0 12482507.0 11681762.0 12696414.0 ... 945442.0 1613486.0 199275.0 903012.0 995017.0 647305.0 254189.0 585582.0 509563.0 534875.0
Value 2014 64102970.0 46246719.0 24432816.0 23924860.0 22537644.0 31191054.0 16052809.0 13244246.0 13464650.0 13333796.0 ... 964357.0 1486404.0 204473.0 1183041.0 1088456.0 708923.0 260398.0 671607.0 535630.0 475411.0
Value 2015 64538065.0 43804679.0 26355499.0 25645126.0 27085100.0 30456135.0 17894345.0 14217862.0 14867660.0 13627422.0 ... 998928.0 1354299.0 249775.0 1041041.0 1301605.0 703271.0 478429.0 725335.0 534547.0 496359.0
Value 2016 61465082.0 40407803.0 25044156.0 24143354.0 29036921.0 28252467.0 18644665.0 13715844.0 14231925.0 13132186.0 ... 942742.0 1170479.0 285428.0 972701.0 1469631.0 708904.0 466435.0 755540.0 522628.0 513580.0
Value 2017 70339967.0 45400673.0 26726633.0 24114281.0 33441172.0 31650136.0 20669159.0 14147002.0 14387447.0 14171418.0 ... 1014030.0 1034977.0 293207.0 1311665.0 1714360.0 717117.0 568479.0 806258.0 600075.0 598441.0
Value 2018 71872376.0 47178858.0 28222683.0 25140494.0 33882831.0 32501200.0 22694251.0 16047425.0 16382779.0 16088867.0 ... 1152272.0 1073617.0 421308.0 1596564.0 1924222.0 777382.0 842384.0 796777.0 676175.0 711488.0
Value 2019 64552117.0 42379802.0 26512710.0 24453425.0 24443054.0 18709286.0 16920257.0 14069356.0 13012552.0 12948998.0 ... 967714.0 937668.0 925406.0 877651.0 868285.0 816483.0 724828.0 681196.0 651946.0 642395.0

10 rows × 69 columns

In [699]:
#let's apply the shift funciton, see which chinese industry got hurt the most. We can clearly see the last row is negative
df_end_use2=((df_end_use1 - df_end_use1.shift()) / df_end_use1.shift())
df_end_use_trans= df_end_use2.T
df_end_use_trans.columns = ['2010', '2011', '2012','2013','2014','2015','2016','2017','2018','2019']
df_china_end_use = df_end_use_trans.T 
#growth rate
df_end_use_trans
Out[699]:
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
End-Use Code
(41050) Cell phones and other household goods, n.e.c. NaN 0.239971 0.306213 0.111479 0.091877 0.006787 -0.047615 0.144389 0.021786 -0.101851
(21300) Computers NaN 0.200015 0.009014 -0.010162 -0.019241 -0.052805 -0.077546 0.123562 0.039166 -0.101720
(41120) Toys, games, and sporting goods NaN -0.090276 -0.007549 -0.019222 -0.008101 0.078693 -0.049756 0.067180 0.055976 -0.060589
(40020) Apparel, textiles, nonwool or cotton NaN 0.140719 0.043749 0.045380 0.109959 0.071903 -0.058560 -0.001204 0.042556 -0.027329
(21400) Telecommunications equipment NaN 0.043189 0.104330 0.094313 0.075717 0.201772 0.072063 0.151678 0.013207 -0.278601
... ... ... ... ... ... ... ... ... ... ...
(41140) Musical instruments NaN 0.007957 0.021261 -0.040182 0.095192 -0.007973 0.008010 0.011585 0.084038 0.050298
(41100) Motorcycles and parts NaN 0.352700 0.266326 0.108476 0.024427 0.837299 -0.025070 0.218774 0.481821 -0.139552
(21110) Food, tobacco machinery NaN 0.182922 0.083669 0.061489 0.146905 0.079999 0.041643 0.067128 -0.011759 -0.145061
(22010) Parts-civilian aircraft NaN 0.362998 0.076978 0.143464 0.051156 -0.002022 -0.022297 0.148188 0.126817 -0.035832
(00180) Other foods NaN 0.272243 0.147506 0.080466 -0.111174 0.044063 0.034695 0.165234 0.188902 -0.097111

69 rows × 10 columns

In [719]:
#There's one outlier that ruins the whole graph 
df_end_use_trans['2016'].idxmax()
Out[719]:
'(30000) Passenger cars, new and used'
In [683]:
#Let's see what happens to the Chinese Product exports 
most_china_list = df_end_use_trans.nsmallest(10,'2019').index.tolist()
most_china_list
Out[683]:
['(13020) Stone, sand, cement, etc.',
 '(30220) Automotive tires and tubes',
 '(21320) Semiconductors',
 '(12500) Plastic materials',
 '(21301) Computer accessories',
 '(12135) Synthetic cloth',
 '(01000) Fish and shellfish',
 '(21160) Measuring, testing, control instruments',
 '(12550) Chemicals-other, n.e.c.',
 '(15000) Iron and steel products, n.e.c.']
In [720]:
not_bad = list(df_end_use1.columns)

for i in not_bad:
    if i in most_china_list:
        not_bad.remove(i)
not_bad.remove(df_end_use_trans['2016'].idxmax())
Out[720]:
['(41050) Cell phones and other household goods, n.e.c.',
 '(21300) Computers',
 '(41120) Toys, games, and sporting goods',
 '(40020) Apparel, textiles, nonwool or cotton',
 '(21400) Telecommunications equipment',
 '(41000) Furniture, household goods, etc.',
 '(41030) Household appliances',
 '(30230) Other parts and accessories of vehicles',
 '(20005) Electric apparatus',
 '(40040) Footwear',
 '(40000) Apparel, household goods - cotton',
 '(21180) Industrial machines, other',
 '(21190) Photo, service industry machinery',
 '(41200) Televisions and video equipment',
 '(41020) Cookware, cutlery, tools',
 '(16120) Industrial supplies, other',
 '(20000) Generators, accessories',
 '(40140) Other consumer nondurables',
 '(12540) Chemicals-organic',
 '(21610) Medicinal equipment',
 '(40050) Camping apparel and gear',
 '(21100) Industrial engines',
 '(15200) Finished metal shapes',
 '(40030) Apparel,household goods-nontextile',
 '(41210) Stereo equipment, etc',
 '(50030) Minimum value shipments',
 '(13120) Nontextile floor tiles',
 '(15100) Iron and steel, advanced',
 '(50020) U.S. goods returned, and reimports',
 '(21170) Materials handling equipment',
 '(40100) Pharmaceutical preparations',
 '(13110) Shingles, wallboard',
 '(41320) Artwork, antiques, stamps, etc.',
 '(41130) Photo equipment',
 '(40110) Books, printed matter',
 '(41310) Jewelry',
 '(30200) Engines and engine parts (carburetors, pistons, rings, and valves)',
 '(21030) Excavating machinery',
 '(40120) Toiletries and cosmetics',
 '(41010) Glassware, chinaware',
 '(21500) Business machines and equipment',
 '(21200) Agricultural machinery, equipment',
 '(21140) Wood, glass, plastic',
 '(12530) Chemicals-inorganic',
 '(12510) Chemicals-fertilizers',
 '(12150) Finished textile supplies',
 '(21120) Metalworking machine tools',
 '(40010) Apparel, household goods - wool',
 '(12550) Chemicals-other, n.e.c.',
 '(21600) Laboratory testing instruments',
 '(13100) Glass-plate, sheet, etc.',
 '(12070) Tobacco, waxes, etc.',
 '(42110) Gem stones, other',
 '(50040) Other (movies, miscellaneous imports, and special transactions)',
 '(13020) Stone, sand, cement, etc.',
 '(41140) Musical instruments',
 '(41100) Motorcycles and parts',
 '(21110) Food, tobacco machinery',
 '(22010) Parts-civilian aircraft',
 '(00180) Other foods']
In [741]:
# now read and clean the U.S. exports file 

url = 'https://www.census.gov/foreign-trade/statistics/product/enduse/exports/c5700.html'
html = requests.get(url).content
df_list_us = pd.read_html(html)
df_end_use_us= df_list_us[-1]

df_end_use_us.to_csv('my data us.csv')
df_end_use_us = df_end_use_us[(df_end_use_us != 0).all(1)]
df_end_use_1_us =  df_end_use_us.nlargest(70,'Value 2019')
df_end_use_1_us
Out[741]:
End-Use Code Value 2010 Value 2011 Value 2012 Value 2013 Value 2014 Value 2015 Value 2016 Value 2017 Value 2018 Value 2019
133 TOTAL 91911081 104121524 110516616 121746189 123657203 115873365 115594784 129797587 120148141 106626775
88 (22090) Civilian aircraft, engines, equipment,... 5754879 6388494 8357556 12586701 13927126 15439704 14577081 16264439 18220787 10458939
83 (21320) Semiconductors 6410995 4607132 3892964 4737314 5510760 5976385 5955948 6076755 7117197 9113628
2 (00100) Soybeans 10864318 10508369 14879547 13304176 14485397 10494079 14212335 12230585 3129041 7991950
94 (30000) Passenger cars, new and used 3441170 5305700 5698113 8522223 11178484 9055568 8842569 10208474 6638100 7231631
... ... ... ... ... ... ... ... ... ... ... ...
111 (41030) Household appliances 172171 163815 161100 173186 156649 160376 162870 182692 205756 194372
55 (12750) Industrial rubber products 163349 181810 172857 204646 213004 195600 186298 210638 212998 191699
5 (00210) Sorghum, barley, oats 3877 241 284 96643 1467583 2116876 1030781 838817 521577 191684
33 (12100) Iron and steel mill products 175218 206232 204275 186074 216657 201045 161389 217445 220335 184759
68 (21030) Excavating machinery 273877 372710 339271 304497 248859 183482 163866 191132 184523 182237

70 rows × 11 columns

In [775]:
df_end_use1_us = df_end_use_1_us.set_index('End-Use Code').T.astype(float).drop(['TOTAL'],axis=1)
#let's apply the shift funciton, see which chinese industry got hurt the most. We can clearly see the last row is negative
df_end_use2_us=((df_end_use1_us - df_end_use1_us.shift()) / df_end_use1_us.shift())
df_end_use_trans_us= df_end_use2_us.T
df_end_use_trans_us.columns = ['2010', '2011', '2012','2013','2014','2015','2016','2017','2018','2019']
df_china_end_use_us = df_end_use_trans_us.T 
#growth rate
df_end_use_trans_us['2013'].idxmax()
Out[775]:
'(00210) Sorghum, barley, oats'

We seclect the top 10 HS2 commodities that were most affected by the tariff escalation

In [745]:
#China exports to U.S.
df_end_use_trans.nsmallest(10,'2019')
Out[745]:
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
End-Use Code
(13020) Stone, sand, cement, etc. NaN 0.021909 0.067991 0.165525 0.093907 0.195827 0.129091 0.166524 0.122414 -0.548760
(30220) Automotive tires and tubes NaN 0.185733 0.284274 0.272377 0.168855 -0.288444 -0.257422 -0.059118 0.237382 -0.500792
(21320) Semiconductors NaN 0.555661 -0.013246 -0.023967 0.206043 0.144985 0.055277 0.127307 -0.078197 -0.454291
(12500) Plastic materials NaN 0.416418 0.004797 0.111070 0.310106 -0.120030 -0.065646 0.348477 0.217204 -0.450288
(21301) Computer accessories NaN -0.018851 0.027295 0.019720 0.076329 -0.023562 -0.072355 0.120261 0.026890 -0.424351
(12135) Synthetic cloth NaN 0.281204 0.093669 0.069574 0.056051 0.062712 -0.058729 0.027167 0.082951 -0.364455
(01000) Fish and shellfish NaN 0.110230 0.002442 0.011203 0.070062 -0.097249 -0.033415 0.076939 0.078985 -0.342140
(21160) Measuring, testing, control instruments NaN 0.139496 0.139968 0.087184 0.061042 0.067711 0.031598 0.096015 0.016002 -0.295595
(12550) Chemicals-other, n.e.c. NaN 0.551198 0.059874 -0.030072 0.038569 -0.051580 -0.052498 0.048094 0.366366 -0.294860
(15000) Iron and steel products, n.e.c. NaN 0.358181 0.116406 -0.090753 0.158544 -0.032102 -0.069929 0.092477 0.153588 -0.286197
In [744]:
#U.S. exports to China 
df_end_use_trans_us.nsmallest(10,'2019')
Out[744]:
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
End-Use Code
(12210) Copper NaN 0.284117 0.017078 -0.117972 -0.136723 -0.419547 -0.033447 0.199860 -0.213935 -0.748894
(00210) Sorghum, barley, oats NaN -0.937839 0.178423 339.292254 14.185611 0.442423 -0.513065 -0.186232 -0.378199 -0.632491
(13100) Logs and lumber NaN 0.662561 -0.151574 0.412149 0.160002 -0.229387 0.247170 0.269094 -0.107772 -0.450961
(11120) Petroleum products, other NaN 0.574671 0.074208 0.376012 -0.270624 -0.105296 -0.036773 0.243342 -0.179879 -0.435605
(22090) Civilian aircraft, engines, equipment, and parts NaN 0.110100 0.308220 0.506027 0.106495 0.108607 -0.055870 0.115754 0.120284 -0.425989
(12700) Synthetic rubber-primary NaN 0.085737 0.080699 -0.140991 -0.100860 -0.293144 -0.103461 0.087465 -0.001362 -0.402623
(00310) Dairy products and eggs NaN 0.524074 0.027630 1.013803 -0.032044 -0.427603 -0.112393 0.503283 -0.223736 -0.387536
(12200) Aluminum and alumina NaN 0.336770 -0.101715 -0.049950 -0.204781 -0.232125 -0.263336 0.206854 -0.355763 -0.373764
(30230) Other parts and accessories of vehicles NaN 0.153988 0.042625 0.761972 0.270673 -0.110203 0.248302 0.166169 0.052884 -0.322354
(10120) Hides and skins NaN 0.232361 0.127227 0.226956 -0.095561 -0.152762 -0.233429 -0.017189 -0.364362 -0.321571
In [762]:
most_us_list = df_end_use_trans_us.nsmallest(10,'2019').index.tolist()
most_us_list
Out[762]:
['(12210) Copper',
 '(00210) Sorghum, barley, oats',
 '(13100) Logs and lumber',
 '(11120) Petroleum products, other',
 '(22090) Civilian aircraft, engines, equipment, and parts',
 '(12700) Synthetic rubber-primary',
 '(00310) Dairy products and eggs',
 '(12200) Aluminum and alumina',
 '(30230) Other parts and accessories of vehicles',
 '(10120) Hides and skins']
In [777]:
not_bad_us = list(df_end_use1_us.columns)

for i in not_bad_us:
    if i in most_us_list:
        not_bad_us.remove(i)
most_us_list.remove(df_end_use_trans_us['2013'].idxmax())
In [790]:
fig, ax = plt.subplots(2,figsize=(30,40))

df_china_end_use[not_bad].plot(ax=ax[0], color=[(0.7, 0.7, 0.7)], lw=0.4, legend=False)


df_china_end_use[most_china_list].plot(ax=ax[0] ,lw=2.5, legend=True,alpha = 0.8)

df_china_end_use_us[not_bad_us].plot(ax=ax[1], color=[(0.7, 0.7, 0.7)], lw=0.4, legend=False)

df_china_end_use_us[most_us_list].plot(ax=ax[1] ,lw=2.5, legend=True,alpha = 0.8)


ax[0].set_title("China Exports to U.S. Growth Rate, by 5-digit-End-Use Code",size = 25)

ax[0].text(8, 0.25, "Other Products")
ax[1].set_title("U.S. Exports to China Growth Rate, by 5-digit-End-Use Code",size = 25)

ax[1].text(8, 0.25, "Other Products")
ax[1].text(3.5, 1.5, "note: (00210) Sorghum, barley, oats should also be included, but flutuates too much on the graph")

for j in range(len(ax)):
    ax[j].set_facecolor(background_1)
    ax[j].spines["right"].set_visible(False)
    ax[j].spines["top"].set_visible(False)
    ax[j].set_ylabel("Growth Rate",size = 20)
    ax[j].set_xlabel('Year')
fig.set_facecolor(background_1)