<jupyter_start><jupyter_text>Matplotlib  Plotting using sub-library matplotlib.pyplot<jupyter_code>#'Magic' command instructing python to plot in-line
%matplotlib inline
import matplotlib.pyplot as plt
import pandas

# Set up working directory
import os
os.chdir("E:/Msc/Advanced-Programming/Practical7-Pandas/data/")<jupyter_output><empty_output><jupyter_text>Create simple plot using Python lists<jupyter_code># Create data
time = [0, 1, 2, 3]
position = [0, 100, 200, 300]

plt.plot(time, position)
plt.xlabel('Time (hr)')
plt.ylabel('Position (km)')<jupyter_output><empty_output><jupyter_text>Plot data directly from a Pandas dataframe.<jupyter_code># Read in Gapminder Data
data = pandas.read_csv('gapminder_gdp_oceania.csv', index_col='country')

# Extract year from last 4 characters of each column name
years = data.columns.str.strip('gdpPercap_')
# Convert year values to integers, saving results back to dataframe
data.columns = years.astype(int)
# Plot
data.loc['Australia'].plot()<jupyter_output><empty_output><jupyter_text>Select and transform data, then plot it.<jupyter_code># Transform data on fly so years are rows and countries columns, and plot
data.T.plot()
plt.ylabel('GDP per capita')<jupyter_output><empty_output><jupyter_text>Test fancier barplot styles using ggplot<jupyter_code>plt.style.use('ggplot')
data.T.plot(kind='bar')
plt.ylabel('GDP per capita')<jupyter_output><empty_output><jupyter_text>Call matplotlib plot function directly<jupyter_code>#Plot Australian GDP over time in years
years = data.columns
gdp_australia = data.loc['Australia']
# g-- specifies a green dashed line, 
plt.plot(years, gdp_australia, 'g--')<jupyter_output><empty_output><jupyter_text>Plot multiple datasets together<jupyter_code># Select two countries' worth of data.
gdp_australia = data.loc['Australia']
gdp_nz = data.loc['New Zealand']

# Plot with differently-colored markers.
plt.plot(years, gdp_australia, 'b-', label='Australia')
plt.plot(years, gdp_nz, 'g-', label='New Zealand')

# Create legend (uses labels specified earlier)
plt.legend(loc='lower right')
plt.xlabel('Year')
plt.ylabel('GDP per capita ($)')<jupyter_output><empty_output><jupyter_text>Scatter plot correlating the GDP of Australia and New Zealand<jupyter_code>plt.scatter(gdp_australia, gdp_nz)
plt.title("GDP")
plt.xlabel('Australia')
plt.ylabel('New ZeaLand')<jupyter_output><empty_output><jupyter_text>Minimum and maximum GDP per capita over time for all the countries in Europe.<jupyter_code>data_europe = pandas.read_csv('data/gapminder_gdp_europe.csv', index_col='country')
# Minimum
data_europe.min().plot(label='min')
# Maximum
data_europe.max().plot(label='max')
# Formatting
plt.legend(loc='best')
plt.xticks(rotation=90)<jupyter_output><empty_output><jupyter_text>Scatter plot of relationship between minimum and maximum GDP per capita among the countries in Asia each year<jupyter_code>data_asia = pandas.read_csv('data/gapminder_gdp_asia.csv', index_col='country')
data_asia.describe().T.plot(kind='scatter', x='min', y='max', title = "GDP in Asian Countries")<jupyter_output><empty_output><jupyter_text>Plot the maximum values for GDP in each year<jupyter_code>#Take a look at the maximum and the max indexes:
data_asia = pandas.read_csv('gapminder_gdp_asia.csv', index_col='country')
data_asia.max().plot()
#print(data_asia.idxmax()) #  prints  the country in each year which had the max GDP
# print(data_asia.idxmin()) # prints  the country in each year which had the min GDP<jupyter_output><empty_output><jupyter_text>Correlation between GDP and life expectancy for 2007, normalizing marker size by population:<jupyter_code>data_all = pandas.read_csv('gapminder_all.csv', index_col='country')
# Scatter defines it to be a scatter plot
# x specifies the x axis data
# Y specifies the y axis data
# s specifies the size of the marker - in this cased based on the population
data_all.plot(kind='scatter', x='gdpPercap_2007', y='lifeExp_2007',
              s=data_all['pop_2007']/1e6)<jupyter_output><empty_output><jupyter_text>Savings plots to file<jupyter_code># Method 1 - saves current figure to file, refers to global figure variable, must execute before printing fig to screen.
#plt.savefig('my_figure.png')
# Method 2 - save ref to current figure in local variable, call save fig class on that variable.
fig = plt.gcf() # get current figure
data.plot(kind='bar')
fig.savefig('my_figure.png')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11650485436893204, 'fraction_numerical': 0.008579814856626778, 'mean_word_length': 7.128440366972477, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, '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': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '372803', 'license': 'MIT', 'n_tokens_mistral': 1593, 'n_tokens_neox': 1483, 'n_words': 390, 'path': 'Practical7-PandasBokeh/Matplotlib.ipynb', 'repo_name': 'masher92/GEOG_5790', 'size': '4429'}