Day 5¶

Graphing¶

Python is a great tool for graphing data.

A Simple Global River Bankfull Width and Depth Database: Africa

Dependencies (Dependencias)¶

In [ ]:
%pip install geopandas
%pip install matplotlib
%pip intall numpy
%pip install rtree
In [ ]:
import geopandas as gdp
import matplotlib.pyplot as plt
In [ ]:
africa_rivers = gdp.read_file("Datasets/AfricaWaterShed/afrivs.shp")
print(africa_rivers)
In [ ]:
lakes = gdp.read_file("Datasets/AfricaLakes/ne_10m_lakes.shp")
country = gdp.read_file("Datasets/Countries/ne_10m_admin_0_countries.shp")
africa = country.loc[country['CONTINENT'] == 'Africa']
africa_lakes = gdp.sjoin(lakes, africa, op='intersects')
africa_lakes = lakes.loc[lakes.index.isin(africa_lakes.index.tolist())]
excluded_rivers = gdp.sjoin(africa_rivers, africa_lakes, op='within')
africa_rivers = africa_rivers.loc[~africa_rivers.index.isin(excluded_rivers.index.tolist())]

fig, ax = plt.subplots(facecolor='#FFFFFFFF')
fig.set_size_inches(15,20)

africa_rivers.plot(ax=ax, color='blue', lw=0.1)
africa_lakes.plot(ax=ax, color='#FFFFFFFF', alpha=1)

ax.axis('off')

plt.show()

Population Density Picture

European Railroads Picture

Orbital Launches Picture

A Simple Graph (Un Grafico Sencillo)¶

In the graph below we are just plotting four linear equations.

En el siguiente gráfico, estamos representando cuatro ecuaciones lineales.

In [ ]:
import matplotlib.pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = (16,16) # Size of plot

x = np.linspace(-5,5,100)
plt.plot(x, 2*x+1, '-r', label='y=2x+1') # Plot(x, equation, linestyle/color, label)
plt.plot(x, 2*x+2, '-.g', label='y=2x+2') 
plt.plot(x, 2*x+3, '--b', label='y=2x+3') 
plt.plot(x, 2*x+4, ':m', label='y=2x+4') 

# Colors: b,g,r,c,m,y,k,w, o(color), ^(color) 
# LineStyles: (:), (-.), (-), (--)

plt.title('Graph of Linear Equations') # Title of graph (Titulo de grafica)
plt.xlabel('x', color='#000000')  # X axis title and color
plt.ylabel('y', color='#000000')  # Y axis titel and color
plt.legend(loc='upper left') # Location of the legend

plt.grid()
plt.show()

We would like you to replicate the code above and change it to create a diamond, change the linestyles, and change the title of the graph to your name.

Nos gustaría que replique el código anterior y lo cambie para crear un diamante, cambie los estilos de línea y cambie el título del gráfico a su nombre.

In [ ]:
import matplotlib.pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = (16,16) # Size of plot

x = np.linspace(-5,5,100)


## Your Code Here (Escribe Aqui)





##

# Colors: b,g,r,c,m,y,k,w, o(color), ^(color) 
# LineStyles: (:), (-.), (-), (--)

plt.title('Graph of Linear Equations') # Title of graph (Titulo de grafica)
plt.xlabel('x', color='#000000')  # X axis title and color
plt.ylabel('y', color='#000000')  # Y axis titel and color
plt.legend(loc='upper left') # Location of the legend

plt.grid()
plt.show()

Sorting Algorithms¶

Bubble Sort¶

This is one of the simpler sorting algorithms that compares adjacent elements and swaps them accordingly. Lets start with an array of elements.

{3 8 19 2 16 10}

First Pass:

{3 8 19 2 16 10} -> {3 8 19 2 16 10}
{3 8 19 2 16 10} -> {3 8 19 2 16 10}
{3 8 19 2 16 10} -> {3 8 2 19 16 10} SWAP!
{3 8 2 19 16 10} -> {3 8 2 16 19 10} SWAP!
{3 8 2 16 19 10} -> {3 8 2 16 10 19} SWAP!

Second Pass:

{3 8 2 16 10 19} -> {3 8 2 16 10 19}
{3 8 2 16 10 19} -> {3 2 8 16 10 19} SWAP!
{3 2 8 16 10 19} -> {3 2 8 16 10 19}
{3 2 8 16 10 19} -> {3 2 8 10 16 19} SWAP!
{3 2 8 10 16 19} -> {3 2 8 10 16 19}

Third Pass:

{3 2 8 10 16 19} -> {2 3 8 10 16 19} SWAP!
{2 3 8 10 16 19} -> {2 3 8 10 16 19}
{2 3 8 10 16 19} -> {2 3 8 10 16 19}
{2 3 8 10 16 19} -> {2 3 8 10 16 19}
{2 3 8 10 16 19} -> {2 3 8 10 16 19}

Result = {2 3 8 10 16 19}

Este es uno de los algoritmos de clasificación más simples que compara elementos adyacentes y los intercambia en ordden. Empezamos con un matriz de elementos

{3 8 19 2 16 10}

Primer Pase:

{3 8 19 2 16 10} -> {3 8 19 2 16 10}
{3 8 19 2 16 10} -> {3 8 19 2 16 10}
{3 8 19 2 16 10} -> {3 8 2 19 16 10} Intercambiar!
{3 8 2 19 16 10} -> {3 8 2 16 19 10} Intercambiar!
{3 8 2 16 19 10} -> {3 8 2 16 10 19} Intercambiar!

Segundo Pase:

{3 8 2 16 10 19} -> {3 8 2 16 10 19}
{3 8 2 16 10 19} -> {3 2 8 16 10 19} Intercambiar!
{3 2 8 16 10 19} -> {3 2 8 16 10 19}
{3 2 8 16 10 19} -> {3 2 8 10 16 19} Intercambiar!
{3 2 8 10 16 19} -> {3 2 8 10 16 19}

Tercer Pase:

{3 2 8 10 16 19} -> {2 3 8 10 16 19} Intercambiar!
{2 3 8 10 16 19} -> {2 3 8 10 16 19}
{2 3 8 10 16 19} -> {2 3 8 10 16 19}
{2 3 8 10 16 19} -> {2 3 8 10 16 19}
{2 3 8 10 16 19} -> {2 3 8 10 16 19}

Resultado = {2 3 8 10 16 19}

BubbleSort Gif

Insertion Sort¶

This is another simple sorting algorithm that works by making two arrays. One of the arrays is the original unsorted array. The second array is a empty array which acts as the sorted array as elements from the first array are placed in sorted order. Let's look at an example.

Steps:

  1. Select the first unsorted element
  2. Swap other elements to the right to place element in correct position
  3. Move marker to the right

{2 8 5 3 9 4}

First Pass:

Marker = 1

{2 8 5 3 9 4}

Second Pass:

Marker = 2

{2 8 5 3 9 4} -> {2 5 8 3 9 4} SWAP!

Third Pass:

Marker = 3

{2 5 8 3 9 4} -> {2 5 3 8 9 4} SWAP!
{2 5 3 8 9 4} -> {2 3 5 8 9 4} SWAP!

Fourth Pass:

Marker = 4

{2 3 5 8 9 4}

Fifth Pass:

Marker = 5

{2 3 5 8 9 4} -> {2 3 5 8 4 9} SWAP!
{2 3 5 8 4 9} -> {2 3 5 4 8 9} SWAP!
{2 3 5 4 8 9} -> {2 3 4 5 8 9} SWAP!

Result: {2 3 5 4 8 9}

InsertionSort Gif

Selection Sort¶

SelectionSort Gif

Merge Sort¶

MergeSort Gif