Run to view results
pip install wbdata
Run to view results
pip install wbdata==0.2.0
Run to view results
pip install wbdata==0.2.0
import wbdata
import pandas as pd
import datetime
# Set the start and end date for the data
start_date = datetime.datetime(2020, 1, 1)
end_date = datetime.datetime(2020, 12, 31)
# Define the indicators for patent applications by residents and non-residents
indicators = {'IP.PAT.RESD': 'Patents by residents',
'IP.PAT.NRES': 'Patents by non-residents'}
# Set the countries for which you want to retrieve data
countries = ['USA', 'CAN']
# Retrieve the data from the World Bank API
patents_data = wbdata.get_dataframe(indicators, country=countries, data_date=(start_date, end_date), convert_date=True)
# Reset the index to make 'date' a column
patents_data.reset_index(inplace=True)
# Create a new variable showing the total patents for each country
patents_data['Total_Patents'] = patents_data['Patents by residents'] + patents_data['Patents by non-residents']
# Display the resulting DataFrame
print(patents_data.head())
Run to view results
import matplotlib.pyplot as plt
# Assuming you have a DataFrame named 'patents_data' with columns 'Country', 'GDP_Per_Capita', 'Total_Patents', and 'Year'
# Filter data for the specified years
selected_years = [1990, 1995, 2000, 2010, 2020]
selected_data = patents_data[patents_data['Year'].isin(selected_years)]
# Plot the relation between GDP per capita and total patents using your custom function (my_xy_plot)
for year in selected_years:
data_for_year = selected_data[selected_data['Year'] == year]
# Assuming my_xy_plot takes x, y, title, xlabel, ylabel as parameters
my_xy_plot(data_for_year['GDP_Per_Capita'], data_for_year['Total_Patents'],
title=f'Relation between GDP per capita and Total Patents ({year})',
xlabel='GDP per Capita',
ylabel='Total Patents')
# Show the plot (optional)
plt.show()
Run to view results
import seaborn as sns
import matplotlib.pyplot as plt
# Assuming you have a DataFrame named 'patents_data' with columns 'Country', 'GDP_Per_Capita', 'Total_Patents', 'Income_Group', 'Region', and 'Year'
# Select relevant columns
selected_columns = ['Country', 'GDP_Per_Capita', 'Total_Patents', 'Income_Group', 'Region', 'Year']
# Filter data for selected columns
selected_data = patents_data[selected_columns]
# Separate figures for GDP per capita and total patents by income groups
plt.figure(figsize=(12, 8))
sns.set_palette("husl")
# GDP per capita by income group
sns.lineplot(x='Year', y='GDP_Per_Capita', hue='Income_Group', data=selected_data)
plt.title('Evolution of GDP per Capita by Income Group')
plt.xlabel('Year')
plt.ylabel('GDP per Capita')
plt.legend(loc='best')
plt.grid(True)
plt.savefig('./graphs/gdp_per_capita_by_income_group.png')
plt.show()
# Total patents by income group
plt.figure(figsize=(12, 8))
sns.set_palette("husl")
sns.lineplot(x='Year', y='Total_Patents', hue='Income_Group', data=selected_data)
plt.title('Evolution of Total Patents by Income Group')
plt.xlabel('Year')
plt.ylabel('Total Patents')
plt.legend(loc='best')
plt.grid(True)
plt.savefig('./graphs/total_patents_by_income_group.png')
plt.show()
# Separate figures for GDP per capita and total patents by region
plt.figure(figsize=(12, 8))
sns.set_palette("husl")
# GDP per capita by region
sns.lineplot(x='Year', y='GDP_Per_Capita', hue='Region', data=selected_data)
plt.title('Evolution of GDP per Capita by Region')
plt.xlabel('Year')
plt.ylabel('GDP per Capita')
plt.legend(loc='best')
plt.grid(True)
plt.savefig('./graphs/gdp_per_capita_by_region.png')
plt.show()
# Total patents by region
plt.figure(figsize=(12, 8))
sns.set_palette("husl")
sns.lineplot(x='Year', y='Total_Patents', hue='Region', data=selected_data)
plt.title('Evolution of Total Patents by Region')
plt.xlabel('Year')
plt.ylabel('Total Patents')
plt.legend(loc='best')
plt.grid(True)
plt.savefig('./graphs/total_patents_by_region.png')
plt.show()
Run to view results
import matplotlib.pyplot as plt
# Assuming you have a DataFrame named 'patents_data' with columns 'Country', 'Patents_by_Residents', 'Patents_by_NonResidents', and 'Year'
# Filter data for the year 2015
data_2015 = patents_data[patents_data['Year'] == 2015]
# Create a scatter plot
plt.figure(figsize=(10, 8))
plt.scatter(data_2015['Patents_by_Residents'], data_2015['Patents_by_NonResidents'], alpha=0.7)
# Show the 45-degree line
plt.plot([0, max(data_2015['Patents_by_Residents'])], [0, max(data_2015['Patents_by_Residents'])], color='red', linestyle='--')
plt.title('Relation between Patenting Activity by Residents and Non-Residents (2015)')
plt.xlabel('Patents by Residents')
plt.ylabel('Patents by Non-Residents')
plt.grid(True)
plt.show()
Run to view results
import geopandas as gpd
import matplotlib.pyplot as plt
# Assuming you have a GeoDataFrame named 'world' with geometry and a DataFrame named 'patents_data' with columns 'Country' and 'Patents_2015'
# Merge the GeoDataFrame with patent data
world_patents = world.merge(patents_data, how='left', left_on='ISO_A3', right_on='Country')
# Create a static map
fig, ax = plt.subplots(1, 1, figsize=(15, 10))
world_patents.boundary.plot(ax=ax, linewidth=0.8)
world_patents.plot(column='Patents_2015', cmap='YlGnBu', linewidth=0.8, ax=ax, legend=True,
legend_kwds={'label': "Patents 2015 by Country",
'orientation': "horizontal"})
plt.title('Patenting Activity Across the World (2015)')
plt.show()
Run to view results
import plotly.express as px
# Assuming you have a DataFrame named 'patents_data' with columns 'Country', 'Patents_2015', 'Latitude', and 'Longitude'
# Create a dynamic map
fig = px.scatter_geo(patents_data, locations="Country", locationmode="country names",
color="Patents_2015", size="Patents_2015",
hover_name="Country", size_max=50,
projection="natural earth", title='Patenting Activity Across the World (2015)')
fig.show()
Run to view results
pip install geopandas matplotlib plotly
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Assuming you have a DataFrame named 'patents_data' with columns 'Country', 'Log_GDP_Per_Capita', 'Patents_by_Residents', 'Patents_by_NonResidents', 'Total_Patents'
# Create a new DataFrame for the table
table_data = patents_data[['Country', 'Log_GDP_Per_Capita', 'Patents_by_Residents', 'Patents_by_NonResidents', 'Total_Patents']]
# Display the table
print(table_data)
# Create scatter plots
plt.figure(figsize=(15, 8))
# Scatter plot for residents
plt.subplot(2, 2, 1)
sns.scatterplot(x='Log_GDP_Per_Capita', y='Patents_by_Residents', data=table_data)
plt.title('Residents Patents vs Log GDP per Capita')
# Scatter plot for non-residents
plt.subplot(2, 2, 2)
sns.scatterplot(x='Log_GDP_Per_Capita', y='Patents_by_NonResidents', data=table_data)
plt.title('Non-Residents Patents vs Log GDP per Capita')
# Scatter plot for total patents
plt.subplot(2, 2, 3)
sns.scatterplot(x='Log_GDP_Per_Capita', y='Total_Patents', data=table_data)
plt.title('Total Patents vs Log GDP per Capita')
# Scatter plot for all three on one figure
plt.subplot(2, 2, 4)
sns.scatterplot(x='Log_GDP_Per_Capita', y='Patents_by_Residents', data=table_data, label='Residents', color='blue', alpha=0.7)
sns.scatterplot(x='Log_GDP_Per_Capita', y='Patents_by_NonResidents', data=table_data, label='Non-Residents', color='orange', alpha=0.7)
sns.scatterplot(x='Log_GDP_Per_Capita', y='Total_Patents', data=table_data, label='Total', color='green', alpha=0.7)
plt.title('Patents vs Log GDP per Capita')
plt.legend()
plt.tight_layout()
plt.show()
Run to view results
Run to view results