generated from mwc/lab_scatter
With the help of stacy we planned the scatterplot
she explained what every function controlled explained using a language I can understand and we compared the language she used to mine and using her guidance I was able to troubleshoot on my own. Because she had worked previous with Chris she was able to add his perspective to what I was working with and from there I was able to understand my input even better.
This commit is contained in:
parent
98e723db73
commit
1d4422c951
|
@ -0,0 +1,21 @@
|
|||
Planning the scatter plot
|
||||
|
||||
Scatterplot X vs Y
|
||||
|
||||
- Draw a scatter plot.
|
||||
- Draw the axes.
|
||||
- Draw the x-axis ( min: 14, max: 492)
|
||||
|
||||
- ticks at every 100 (100,200,300,400,500)
|
||||
- Draw the line
|
||||
- Plot the ploints
|
||||
-...
|
||||
|
||||
- Draw the y-axis ( min: -927, max: 3710)
|
||||
|
||||
|
||||
- ...ticks every 1000 ( -900, 0, 1000, 2000,300)
|
||||
- Draw the line
|
||||
- Plot the points.
|
||||
- ...
|
||||
use blue dots
|
114
scatterplot.py
114
scatterplot.py
|
@ -3,9 +3,12 @@
|
|||
# By MWC Contributors
|
||||
# Uses lots of helper functions in other modules to draw a scatter plot.
|
||||
|
||||
from math import floor, ceil, log
|
||||
|
||||
from turtle import *
|
||||
from superturtle.movement import no_delay
|
||||
import constants
|
||||
|
||||
from generate_data import generate_data
|
||||
from ticks import get_tick_values
|
||||
from plotting import (
|
||||
|
@ -16,31 +19,124 @@ from plotting import (
|
|||
draw_y_tick,
|
||||
draw_point,
|
||||
)
|
||||
from superturtle.movement import no_delay
|
||||
import constants
|
||||
def flyto(x, y):
|
||||
penup()
|
||||
goto(x, y)
|
||||
pendown()
|
||||
from generate_data import generate_data
|
||||
from ticks import get_tick_values
|
||||
from plotting import (
|
||||
prepare_screen,
|
||||
draw_point,
|
||||
)
|
||||
from transform import (
|
||||
maximum,
|
||||
minimum,
|
||||
bounds,
|
||||
clamp,
|
||||
ratio,
|
||||
bounds,
|
||||
scale,
|
||||
get_x_values,
|
||||
get_y_values,
|
||||
get_y_values
|
||||
|
||||
)
|
||||
|
||||
def draw_x_axis(min_val, max_val):
|
||||
"Draws the x-axis of the scatter plot from min_val to max_val"
|
||||
penup
|
||||
flyto(0,0)
|
||||
goto(constants.PLOT_WIDTH,0)
|
||||
pendown()
|
||||
|
||||
def get_tick_values(low, high):
|
||||
"""Returns a list of values to use for ticks (labeled points along an axis).
|
||||
Includes the lowest value, a bunch of "nice" intermediate values, and the highest value.
|
||||
"""
|
||||
tick_interval = get_tick_interval(high - low)
|
||||
first_tick = ceil(low / tick_interval) * tick_interval
|
||||
return [low] + list(range(first_tick, high, tick_interval)) + [high]
|
||||
|
||||
def get_tick_interval(span):
|
||||
"""Returns a 'nice' interval for ticks across span.
|
||||
The interval is a power of ten (e.g. 1000, 100, 0.1)
|
||||
scaled so that there will be between 0 and 10 internal ticks.
|
||||
"""
|
||||
log_span = log(span, 10)
|
||||
return 10 ** floor(log_span)
|
||||
|
||||
|
||||
def draw_y_axis(min_val, max_val):
|
||||
"Draws the y-axis of the scatterplot from min_val to max_val"
|
||||
penup()
|
||||
flyto(0,0)
|
||||
goto(0,constants.PLOT_HEIGHT)
|
||||
pendown
|
||||
|
||||
|
||||
|
||||
|
||||
def draw_x_tick(position, label):
|
||||
"Draws a tick mark on the x-axis at the specified position"
|
||||
flyto(position, 0)
|
||||
goto(position, -constants.TICK_LENGTH)
|
||||
flyto(position, -constants.TICK_LENGTH - 10)
|
||||
write(label, align='center')
|
||||
|
||||
|
||||
|
||||
def draw_y_tick(position, label):
|
||||
"Draws a tick mark on the y-axis at the specified position"
|
||||
flyto(0, position)
|
||||
goto(-constants.TICK_LENGTH, position)
|
||||
write(label, align='right')
|
||||
|
||||
|
||||
def get_tick_values(low, high):
|
||||
"""Returns a list of values to use for ticks (labeled points along an axis).
|
||||
Includes the lowest value, a bunch of "nice" intermediate values, and the highest value.
|
||||
"""
|
||||
tick_interval = get_tick_interval(high - low)
|
||||
first_tick = ceil(low / tick_interval) * tick_interval
|
||||
return [low] + list(range(first_tick, high, tick_interval)) + [high]
|
||||
|
||||
|
||||
|
||||
def draw_scatterplot(data, size=5, color="black"):
|
||||
"Draws a scatter plot, showing the data"
|
||||
prepare_screen()
|
||||
draw_axes(data)
|
||||
draw_points(data, color, size)
|
||||
|
||||
draw_points(data, size, color)
|
||||
|
||||
def draw_axes(data):
|
||||
"Draws the scatter plot's axes."
|
||||
x_values = get_x_values(data)
|
||||
y_values = get_y_values(data)
|
||||
xmin, xmax = bounds(x_values)
|
||||
ymin, ymax = bounds(y_values)
|
||||
draw_x_axis(xmin, xmax)
|
||||
draw_y_axis(ymin, ymax)
|
||||
|
||||
def draw_points(data, color, size):
|
||||
|
||||
|
||||
def draw_points(x,y, color="blue", size=5):
|
||||
"Draws the scatter plot's points."
|
||||
x_values = get_x_values(data)
|
||||
y_values = get_y_values(data)
|
||||
xmin, xmax = bounds(x_values)
|
||||
ymin, ymax = bounds(y_values)
|
||||
|
||||
for point in data:
|
||||
x, y = point
|
||||
|
||||
screen_x = scale(x, xmin, xmax, 0, constants.PLOT_WIDTH)
|
||||
screen_y = scale(y, ymin, ymax, 0, constants.PLOT_HEIGHT)
|
||||
|
||||
|
||||
draw_point(screen_x, screen_y, color, size)
|
||||
|
||||
|
||||
|
||||
with no_delay():
|
||||
data = generate_data(50, 10, 500, 5, 400, 1000)
|
||||
draw_scatterplot(data, size=5, color="blue")
|
||||
hideturtle()
|
||||
done()
|
||||
|
||||
|
|
63
transform.py
63
transform.py
|
@ -6,38 +6,83 @@
|
|||
|
||||
def maximum(data):
|
||||
"Returns the largest number in data"
|
||||
raise NotImplementedError
|
||||
highest = None
|
||||
for number in data:
|
||||
if highest is None:
|
||||
highest = number
|
||||
if number > highest:
|
||||
highest = number
|
||||
return highest
|
||||
|
||||
|
||||
|
||||
def minimum(data):
|
||||
"Returns the smallest number in data"
|
||||
raise NotImplementedError
|
||||
lowest = None
|
||||
for number in data:
|
||||
if lowest is None:
|
||||
lowest = number
|
||||
if number < lowest:
|
||||
lowest = number
|
||||
return lowest
|
||||
|
||||
def bounds(data):
|
||||
"Returns a list of the smallest and largest numbers in data"
|
||||
raise NotImplementedError
|
||||
return [minimum(data), maximum(data)]
|
||||
|
||||
|
||||
|
||||
def clamp(value, low, high):
|
||||
"""Clamps a value to a range from low to high.
|
||||
Returns value if it is between low and high.
|
||||
If value is lower than low, returns low. If value is higher than high, returns high.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
if low < value and value < high:
|
||||
return value
|
||||
if value <= low:
|
||||
return low
|
||||
if value >= high:
|
||||
return high
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def ratio(value, start, end):
|
||||
"""Returns a number from 0.0 to 1.0, representing how far along value is from start to end.
|
||||
The return value is clamped to [0, 1], so even if value is lower than start, the return
|
||||
value will not be lower than 0.0.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
r= (value - start)/ (end-start)
|
||||
return clamp (r, 0,1)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def scale(value, domain_min, domain_max, range_min, range_max):
|
||||
"Given a value within a domain, returns the scaled equivalent within range."
|
||||
raise NotImplementedError
|
||||
"Given a value within a domain, returns the scaled equivalent within range."
|
||||
return (range_min + ((ratio(value,domain_min,domain_max)) * (range_max - range_min)))
|
||||
|
||||
|
||||
|
||||
def get_x_values(points):
|
||||
"Returns the first value for each point in points."
|
||||
raise NotImplementedError
|
||||
x_values = []
|
||||
for x,y in points:
|
||||
x_values.append(x)
|
||||
|
||||
return x_values
|
||||
|
||||
|
||||
|
||||
|
||||
def get_y_values(points):
|
||||
"Returns the second value for each point in points."
|
||||
raise NotImplementedError
|
||||
y_values = []
|
||||
for x,y in points:
|
||||
y_values.append(y)
|
||||
|
||||
return y_values
|
||||
|
|
Loading…
Reference in New Issue