#==================================================
# Lecture 24: Plotting
#--------------------------------------------------
# Five stories, five kinds of picture. Every plot of
# the slides is one function here. Uncomment one line
# at the bottom and run. Close the plot window to let
# the program go on.
#
# Needs matplotlib, once: in Thonny,
#   Tools -> Manage packages... -> matplotlib -> Install
#
# lec24_data.py (next to this file) holds the real
# numbers of all five stories as lists.

import matplotlib.pyplot as plt
from lec24_data import *

#==================================================
# Story 1: John Snow, London 1854
#--------------------------------------------------
# Slide: Two Lists, One Dot Each

def first_dots():
    xs = [1, 2, 4, 5, 7]
    ys = [3, 5, 2, 6, 4]
    plt.scatter(xs, ys)         # one dot for each pair (x, y)
    plt.show()                  # open the window

#--------------------------------------------------
# Slide: Snow's Map in Python

def snow_map():
    plt.scatter(death_xs, death_ys, s=4)        # s: the size of a dot
    plt.scatter(pump_xs, pump_ys, s=80, marker='^', color='red')
    plt.show()

#--------------------------------------------------
# Slides: Day by Day, and A Plot Needs Words

def cases_per_day():
    plt.plot(days, cases)       # joins the points with a line
    plt.title('Cholera in Soho, 1854')
    plt.xlabel('day (1 = 28 August)')
    plt.ylabel('new cases')
    plt.show()
    # The handle came off on day 12 (8 September).


#==================================================
# Story 2: Challenger, 1986
#--------------------------------------------------
# Slide: What the Engineers Showed (only the launches with damage)

def seven_launches():
    some_temps = []
    some_damage = []
    for i in range(len(temps)):
        if damage[i] > 0:
            some_temps.append(temps[i])
            some_damage.append(damage[i])
    plt.scatter(some_temps, some_damage, alpha=0.5)
    plt.xlabel('temperature at launch (°C)')
    plt.ylabel('rings with damage')
    plt.show()

#--------------------------------------------------
# Slide: All 23 Launches

def all_launches():
    # alpha=0.5: see-through dots. Two launches with the same
    # numbers share one dot, and that dot looks darker.
    plt.scatter(temps, damage, alpha=0.5)
    plt.xlabel('temperature at launch (°C)')
    plt.ylabel('rings with damage')
    plt.show()

#--------------------------------------------------
# Slide: Where Is Launch Day?

def where_is_launch_day():
    plt.scatter(temps, damage, alpha=0.5)
    plt.xlim(-5, 30)            # the x axis goes from -5 to 30
    plt.axvline(launch_temp, color='red')   # a line straight up at x = -1
    plt.text(0, 1.5, 'launch day')          # words at the point (0, 1.5)
    plt.xlabel('temperature at launch (°C)')
    plt.ylabel('rings with damage')
    plt.show()


#==================================================
# Story 3: Hubble's 24 galaxies, 1929
#--------------------------------------------------
# Slides: 24 Dots, and A Line Through the Dots

def hubble():
    plt.scatter(distances, speeds, label='24 galaxies')
    # A straight line needs only two points: (0, 0) and (2, 1000).
    plt.plot([0, 2], [0, 1000], color='red', label='speed = 500 x distance')
    plt.xlabel('distance (million parsecs)')
    plt.ylabel('speed (km per second)')
    plt.legend()                # the box that says which is which
    plt.show()

#--------------------------------------------------
# Slide: You Try (the line follows the list)

def tent():
    plt.plot([0, 2, 4], [0, 4, 0])
    plt.show()


#==================================================
# Story 4: Florence Nightingale, 1854 to 1856
#--------------------------------------------------
# Slide: What Killed the Soldiers?

def causes_pie():
    counts = [sum(disease), sum(wounds), sum(other)]
    names = ['disease', 'wounds', 'other']
    plt.pie(counts, labels=names)       # one slice for each count
    plt.show()

#--------------------------------------------------
# Slides: Month by Month, and Bars on Bars

def deaths_per_month():
    plt.bar(months, disease, label='disease')
    # bottom=: each red bar starts on top of the blue bar
    plt.bar(months, wounds, bottom=disease, color='red', label='wounds')
    plt.xlabel('month (1 = April 1854)')
    plt.ylabel('deaths')
    plt.legend()
    plt.show()

#--------------------------------------------------
# Slide: A Rose Diagram in Python

def rose():
    step = 6.28 / 12            # a full turn is 6.28: 12 wedges share it
    angles = []
    for k in range(12):
        angles.append(k * step)         # where each wedge points
    plt.axes(projection='polar')        # a round plot
    plt.bar(angles, disease[:12], width=step)   # the first 12 months
    # Try: the wounds too, in red, as Nightingale did:
    # plt.bar(angles, wounds[:12], width=step, color='red')
    plt.xticks(angles, months[:12])     # the month number at each wedge
    plt.show()


#==================================================
# Story 5: Thirteen tables
#--------------------------------------------------
# Slide: Same Averages

def averages():
    for name in tables:
        xs, ys = tables[name]
        print(name, round(sum(xs) / len(xs), 1), round(sum(ys) / len(ys), 1))

#--------------------------------------------------
# Slide: One List at a Time

def one_list():
    xs, ys = tables['away']     # table 1. Then try 'v_lines': table 3
    plt.hist(xs, bins=20)       # Python counts: 20 piles
    plt.show()

#--------------------------------------------------
# Slide: Thirteen Small Plots

def thirteen_plots():
    k = 1
    for name in tables:
        xs, ys = tables[name]
        plt.subplot(3, 5, k)    # 3 rows, 5 columns: plot number k
        plt.scatter(xs, ys, s=2)
        plt.axis('off')         # hide the axes: the plots are small
        k += 1
    plt.show()

def table_13():
    xs, ys = tables['dino']
    plt.scatter(xs, ys)
    plt.title('table 13')
    plt.show()


#==================================================
# Run one at a time:

first_dots()
# snow_map()
# cases_per_day()
# seven_launches()
# all_launches()
# where_is_launch_day()
# hubble()
# tent()
# causes_pie()
# deaths_per_month()
# rose()
# averages()
# one_list()
# thirteen_plots()
# table_13()
