<jupyter_start><jupyter_text>Pie Charts, Box Plots, Scatter Plots,Histograms<jupyter_code>import numpy as np  # useful for many scientific computing in Python
import pandas as pd # primary data structure library
df_can = pd.read_excel('Canada.xlsx',
                       sheet_name='Canada by Citizenship',
                       skiprows=range(20),
                       skipfooter=2
                      )<jupyter_output><empty_output><jupyter_text>Pie Charts A `pie chart` is a circualr graphic that displays numeric proportions by dividing a circle (or pie) into proportional slices. You are most likely already familiar with pie charts as it is widely used in business and media. We can create pie charts in Matplotlib by passing in the `kind=pie` keyword.Let's use a pie chart to explore the proportion (percentage) of new immigrants grouped by continents for the entire time period from 1980 to 2013.  Step 1: Gather data. We will use *pandas* `groupby` method to summarize the immigration data by `Continent`. The general process of `groupby` involves the following steps:<jupyter_code>df_can

df_continents = df_can.groupby('AreaName', axis=0).sum()

print(type(df_can.groupby('AreaName', axis=0)))

df_continents.head()
import matplotlib as mpl
import matplotlib.pyplot as plt
# autopct create %, start angle represent starting point
#year 2004 population immigration percentile
df_continents[2004].plot(kind='pie',
                            figsize=(12, 6),
                            autopct='%1.1f%%', # add in percentages
                            startangle=90,     # start angle 90° 
                            shadow=True,       # add shadow      
                            )

plt.title('Immigration to Canada by Continent [1980 - 2013]')
plt.axis('equal') # Sets the pie chart to look like a circle.

plt.show()
colors_list = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue', 'lightgreen', 'pink']
explode_list = [0.2, 0, 0, 0, 0, 0] # ratio for each continent with which to offset each wedge.
print(df_continents[2004])
df_continents[2004].plot(kind='pie',
                            figsize=(15, 6),
                            autopct='%1.1f%%', 
                            startangle=90,    
                            shadow=True,       
                            labels=None,         # turn off labels on pie chart
                            pctdistance = 1.12,    # the ratio between the center of each pie slice and the start of the text generated by autopct 
                            colors=colors_list,  # add custom colors
                            explode=explode_list # 'explode' lowest 3 continents
                            )

# scale the title up by 12% to match pctdistance
plt.title("Immigration to Canada from Continent's in Year 2004") 

plt.axis('equal') 

# add legend
plt.legend(labels=df_continents.index, loc='upper left') 

plt.show()<jupyter_output>AreaName
Africa                              28192
Asia                               134850
Europe                              38082
Latin America and the Caribbean     22181
Northern America                     6990
Oceania                              1788
Name: 2004, dtype: int64<jupyter_text>Box Plots A `box plot` is a way of statistically representing the *distribution* of the data through five main dimensions:  To make a `box plot`, we can use `kind=box` in `plot` method invoked on a *pandas* series or dataframe.Let's plot the box plot for the Japanese immigrants between 1980 - 2013.<jupyter_code># to get a dataframe, place extra square brackets around 'Japan'.
import pandas as pd
df_bar = pd.read_excel('Canada.xlsx',
                       sheet_name='Canada by Citizenship',
                       skiprows=range(20),
                       skipfooter=2
                      )

df_bar.set_index('OdName',inplace = True)
#df_can.head(5)
#df_can.set_index('OdName',inpla)
years = [i for i in range(1980,2014)]
df_Algeria = df_bar.loc['Algeria', years]
#years = [i for i in range(1980,2014)]



#df_Algeria.shape
df_Algeria
ax = df_Algeria.plot(kind='box', figsize=(10,10))
ax.set_xticklabels(['Algeria'],rotation = 90)
plt.title('Box plot of Algeria Immigrants from 1980 - 2013')
plt.ylabel('Number of Immigrants')
plt.xlabel('Immigration country')
plt.show()
# horizontal box plots
df_Algeria.plot(kind='box', figsize=(10, 7), color='green', vert=False)

plt.title('Box plots of Immigrants from China and India (1980 - 2013)')
plt.xlabel('Number of Immigrants')

plt.show()
fig = plt.figure() # create figure

ax0 = fig.add_subplot(1, 2, 1) # add subplot 1 (1 row, 2 columns, first plot)
ax1 = fig.add_subplot(1, 2, 2) # add subplot 2 (1 row, 2 columns, second plot). See tip below**

# Subplot 1: Box plot
df_Algeria.plot(kind='box', color='blue', vert=False, figsize=(20, 6), ax=ax0) # add to subplot 1
ax0.set_title('Box Plots of Immigrants from China and India (1980 - 2013)')
ax0.set_xlabel('Number of Immigrants')
ax0.set_ylabel('Countries')

# Subplot 2: Line plot
df_Algeria.plot(kind='line', figsize=(20, 6), ax=ax1) # add to subplot 2
ax1.set_title ('Line Plots of Immigrants from China and India (1980 - 2013)')
ax1.set_ylabel('Number of Immigrants')
ax1.set_xlabel('Years')

plt.show()<jupyter_output><empty_output><jupyter_text>Scatter Plots A `scatter plot` (2D) is a useful method of comparing variables against each other. `Scatter` plots look similar to `line plots` in that they both map independent and dependent variables on a 2D graph. While the datapoints are connected together by a line in a line plot, they are not connected in a scatter plot. The data in a scatter plot is considered to express a trend. With further analysis using tools like regression, we can mathematically calculate this relationship and use it to predict trends outside the dataset.Let's start by exploring the following:Using a `scatter plot`, let's visualize the trend of total immigrantion to Canada (all countries combined) for the years 1980 - 2013.<jupyter_code># we can use the sum() method to get the total population per year
df_tot = pd.DataFrame(df_can[years].sum(axis=0))

# change the years to type int (useful for regression later on)
df_tot.index = map(int, df_tot.index)

# reset the index to put in back in as a column in the df_tot dataframe
df_tot.reset_index(inplace = True)

# rename columns
df_tot.columns = ['year', 'total']

# view the final dataframe
df_tot.head()
df_tot.plot(kind='scatter', x='year', y='total', figsize=(10, 10), color='red')

plt.title('Total Immigration to Canada from 1980 - 2013')
plt.xlabel('Year')
plt.ylabel('Number of Immigrants')
plt.ylim(0,200000)
plt.show()<jupyter_output><empty_output><jupyter_text>HistogramsA histogram is a way of representing the *frequency* distribution of numeric dataset.<jupyter_code>df_can[2013].plot(kind='hist', figsize=(8, 5))
plt.title('Histogram of Immigration from 195 Countries in 2013') # add a title to the histogram
plt.ylabel('Number of Countries') # add y-label
plt.xlabel('Number of Immigrants') # add x-label

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09637364187949767, 'fraction_numerical': 0.03527585720333004, 'mean_word_length': 3.1718658034137728, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '334707', 'license': 'Apache-2.0', 'n_tokens_mistral': 2355, 'n_tokens_neox': 2070, 'n_words': 754, 'path': 'Matplotlib_2.ipynb', 'repo_name': 'sajid-a/PythonDemo', 'size': '7087'}