Udpated
This commit is contained in:
121
scatterplot.py
121
scatterplot.py
@@ -1,22 +1,31 @@
|
||||
# What needs to be taught?
|
||||
# - return values
|
||||
# - unpacking multiple return values
|
||||
# - list comprehensions
|
||||
# - named function arguments
|
||||
# - after positional
|
||||
# - like dictionaries
|
||||
# -
|
||||
# - modules
|
||||
|
||||
# Parameters:
|
||||
# - the actual drawing will be
|
||||
# scatterplot.py
|
||||
# ------------
|
||||
# By MWC Contributors
|
||||
# Uses lots of helper functions in other modules to draw a scatter plot.
|
||||
|
||||
from turtle import *
|
||||
from math import floor, ceil, log
|
||||
import constants
|
||||
from movement import flyto
|
||||
from superturtle.movement import no_delay
|
||||
import constants
|
||||
from generate_data import generate_data
|
||||
from ticks import get_tick_values
|
||||
from plotting import (
|
||||
prepare_screen,
|
||||
draw_x_axis,
|
||||
draw_y_axis,
|
||||
draw_x_tick,
|
||||
draw_y_tick,
|
||||
draw_point,
|
||||
)
|
||||
from transform import (
|
||||
maximum,
|
||||
minimum,
|
||||
bounds,
|
||||
clamp,
|
||||
ratio,
|
||||
scale,
|
||||
get_x_values,
|
||||
get_y_values,
|
||||
)
|
||||
|
||||
def draw_scatterplot(data, size=5, color="black"):
|
||||
"Draws a scatter plot, showing the data"
|
||||
@@ -24,90 +33,14 @@ def draw_scatterplot(data, size=5, color="black"):
|
||||
draw_axes(data)
|
||||
draw_points(data, color, size)
|
||||
|
||||
def prepare_screen():
|
||||
"""Sets up the
|
||||
"""
|
||||
screensize(400, 400)
|
||||
setworldcoordinates(-20, -20, 420, 420)
|
||||
|
||||
def draw_axes(data):
|
||||
"""Draws the scatter plot's axes.
|
||||
"""
|
||||
flyto(0, 0)
|
||||
goto(0, constants.PLOT_HEIGHT)
|
||||
flyto(0, 0)
|
||||
goto(constants.PLOT_WIDTH, 0)
|
||||
|
||||
y_values = get_y_values(data)
|
||||
ymin, ymax = get_bounds(y_values)
|
||||
draw_y_tick(0, ymin)
|
||||
draw_y_tick(constants.PLOT_HEIGHT, ymax)
|
||||
for tick in get_tick_range(y_values):
|
||||
position = scale(tick, ymin, ymax, 0, constants.PLOT_HEIGHT)
|
||||
draw_y_tick(position, tick)
|
||||
|
||||
x_values = get_x_values(data)
|
||||
xmin, xmax = get_bounds(x_values)
|
||||
draw_x_tick(0, xmin)
|
||||
draw_x_tick(constants.PLOT_WIDTH, xmax)
|
||||
for tick in get_tick_range(x_values):
|
||||
position = scale(tick, xmin, xmax, 0, constants.PLOT_WIDTH)
|
||||
draw_x_tick(position, tick)
|
||||
|
||||
def get_tick_range(values):
|
||||
"Returns a range of positions for ticks"
|
||||
vmin, vmax = get_bounds(values)
|
||||
tick_interval = get_tick_interval(values)
|
||||
first_tick = ceil(vmin / tick_interval) * tick_interval
|
||||
print(tick_interval)
|
||||
print(first_tick)
|
||||
print(vmax)
|
||||
return range(first_tick, vmax, tick_interval)
|
||||
|
||||
def get_tick_interval(values):
|
||||
""
|
||||
vmin, vmax = get_bounds(values)
|
||||
log_span = log(vmax - vmin, 10)
|
||||
return 10 ** floor(log_span)
|
||||
"Draws the scatter plot's axes."
|
||||
|
||||
def draw_points(data, color, size):
|
||||
xmin, xmax = get_bounds(get_x_values(data))
|
||||
ymin, ymax = get_bounds(get_y_values(data))
|
||||
for x, y in data:
|
||||
sx = scale(x, xmin, xmax, 0, constants.PLOT_WIDTH)
|
||||
sy = scale(y, ymin, ymax, 0, constants.PLOT_HEIGHT)
|
||||
draw_point(sx, sy, color, size)
|
||||
|
||||
def draw_point(x, y, color, size):
|
||||
flyto(x, y)
|
||||
dot(size, color)
|
||||
|
||||
def get_bounds(data):
|
||||
return min(data), max(data)
|
||||
|
||||
def get_x_values(data):
|
||||
return [x for x, y, in data]
|
||||
|
||||
def get_y_values(data):
|
||||
return [y for x, y in data]
|
||||
|
||||
def draw_y_tick(position, label):
|
||||
flyto(0, position)
|
||||
goto(-constants.TICK_LENGTH, position)
|
||||
write(label, align='right')
|
||||
|
||||
def draw_x_tick(position, label):
|
||||
flyto(position, 0)
|
||||
goto(position, -constants.TICK_LENGTH)
|
||||
flyto(position, -constants.TICK_LENGTH - 10)
|
||||
write(label, align='center')
|
||||
|
||||
def scale(value, domain_min, domain_max, range_min, range_max):
|
||||
ratio = (value - domain_min) / (domain_max - domain_min)
|
||||
return range_min + ratio * (range_max - range_min)
|
||||
"Draws the scatter plot's points."
|
||||
|
||||
with no_delay():
|
||||
data = generate_data(50, 10, 500, 5, 400, 1000)
|
||||
draw_scatterplot(data, size=5, color="blue")
|
||||
hideturtle()
|
||||
input()
|
||||
done()
|
||||
|
||||
Reference in New Issue
Block a user