Løsningsforslag 12

Oppgave 1
#Oppgave A

import matplotlib.pyplot as plt
from math import sin


xs = [n / 10 for n in range(101)]

ys_1 = [sin(x) for x in xs]
ys_2 = [3 * sin(x) for x in xs]

plt.plot(xs, ys_1, "-.g", label="sin(x)")
plt.plot(xs, ys_2, ".-m", label="3*sin(x)")


plt.xlabel("x-verdier")
plt.ylabel("y-verdier")


plt.title("Graf av sin(x) og 3*sin(x)")


plt.legend()

plt.savefig("test.png")
plt.savefig("test.pdf")

plt.show() 
#Oppgave B
import matplotlib.pyplot as plt
from math import sin

xs = [n / 10 for n in range(101)]
ys_1 = [sin(x) for x in xs]
ys_2 = [3 * sin(x) for x in xs]


plt.plot(xs, ys_1, "og", label="sin(x)")
plt.stem(xs, ys_2, "-m", label="3*sin(x)")


plt.xlabel("x-verdier")
plt.ylabel("y-verdier")

plt.title("Stem-plot av sin(x) og 3*sin(x)")

plt.legend()

plt.savefig("test.png")
plt.savefig("test.pdf")

plt.show() 
#Oppgave C

import matplotlib.pyplot as plt
from math import sin

xs = [n / 10 for n in range(101)]
ys_1 = [sin(x) for x in xs]
ys_2 = [3 * sin(x) for x in xs]


intersections = []

for i in range(0,len(ys_1) + 1):
    try:
        if ys_1[i] * ys_1[i+1] < 0:
            if ys_2[i] * ys_2[i+1] < 0:
                intersections.append((xs[i],ys_1[i]))

    except IndexError:
        pass

plt.plot(xs, ys_1, "--g", label="sin(x)")
plt.plot(xs, ys_2, "-.m", label="3*sin(x)")

for i in intersections:
    x_t = round(i[0],2)
    y_t = round(i[1],2)
    ant_text = f"{(x_t,y_t)}"
    plt.annotate(ant_text, xy=(i[0], i[1]), xytext=(i[0] - 0.10, i[1] - 1.2),color="r",
            arrowprops=dict(arrowstyle="->",facecolor='black',connectionstyle="angle3"),
            horizontalalignment='left',
            verticalalignment='bottom')

plt.xlabel("x-verdier")
plt.ylabel("y-verdier")

plt.title("Stem-plot av sin(x) og 3*sin(x)")

plt.legend()

plt.savefig("test.png")
plt.savefig("test.pdf")

plt.show() 
Oppgave 2
import json
import matplotlib.pyplot as plt

filnavn = "norway.json"
with open(filnavn) as f:
    content = json.loads(f.read())

coords = content['features'][0]['geometry']['coordinates']

for poly in coords:
    for part in poly:
        xs, ys = [], []
        for x,y in part:
            xs.append(x)
            ys.append(y)
        plt.plot(xs,ys,'-')

plt.show()
Oppgave 3

import csv
import matplotlib.pyplot as plt


#colors and sizes for the airport dots
medium_airport_color = "lightblue"
medium_airport_size = 10
large_airport_color = "blue"
large_airport_size = 30

with open("airport-codes-small.csv", newline="", encoding="utf-8") as csvfile:
    reader = csv.reader(csvfile)
    next(reader)  # skip the header row
    
    latitude_list = []
    longitude_list = []
    airport_size_list = []
    iata_code_list = []

    for row in reader:
        airport_size = row[1]
        iata_code = row[9]
        coordinates = row[11]
        latitude, longitude = map(float, coordinates.split(","))

        latitude_list.append(latitude)
        longitude_list.append(longitude)
        airport_size_list.append(airport_size)
        iata_code_list.append(iata_code)
    
    # Create a scatter plot of the airport positions
    fig, ax = plt.subplots()
    for lat, lon, size in zip(latitude_list, longitude_list, airport_size_list):
        if size == "medium_airport":
            ax.scatter(lon, lat, color=medium_airport_color, s=medium_airport_size)
        if size == "large_airport":
            ax.scatter(lon, lat, color=medium_airport_color, s=large_airport_size)    
        


    for lat, lon, size, iata_code in zip(latitude_list, longitude_list, airport_size_list, iata_code_list):
        if size == "large_airport":
            ax.annotate(iata_code, (lon, lat))
    
    
    ax.set_xlabel("Longitude")
    ax.set_ylabel("Latitude")
    ax.set_title("Airport Positions")
    
    plt.show()