generated from mwc/lab_scatter
	scatterplot.py
This commit is contained in:
		@@ -16,6 +16,7 @@ from plotting import (
 | 
			
		||||
    draw_y_tick,
 | 
			
		||||
    draw_point, 
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
from transform import (
 | 
			
		||||
    maximum, 
 | 
			
		||||
    minimum,
 | 
			
		||||
@@ -28,19 +29,42 @@ from transform import (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
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)
 | 
			
		||||
 | 
			
		||||
def draw_axes(data):
 | 
			
		||||
    "Draws the scatter plot's axes."
 | 
			
		||||
    draw_x_axis()
 | 
			
		||||
    x_values = get_x_values(data)
 | 
			
		||||
    xmin, xmax = bounds(x_values)
 | 
			
		||||
    ticks = get_tick_values(xmin, xmax)
 | 
			
		||||
    for tick in ticks:
 | 
			
		||||
        screen_x_position = scale(tick, xmin, xmax, 0, constants.PLOT_WIDTH)
 | 
			
		||||
        draw_x_tick(screen_x_position, tick)
 | 
			
		||||
   
 | 
			
		||||
    draw_y_axis()
 | 
			
		||||
    y_values = get_y_values(data)
 | 
			
		||||
    ymin, ymax = bounds(y_values)
 | 
			
		||||
    ticks = get_tick_values(ymin, ymax)
 | 
			
		||||
    for tick in ticks:
 | 
			
		||||
        screen_y_position = scale(tick, ymin, ymax, 0, constants.PLOT_HEIGHT)
 | 
			
		||||
        draw_y_tick(screen_y_position, tick)
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
def draw_points(data, color, size):    
 | 
			
		||||
    "Draws the scatter plot's points."
 | 
			
		||||
    x_values = get_x_values(data)
 | 
			
		||||
    xmin, xmax = bounds(x_values)
 | 
			
		||||
    y_values = get_y_values(data)
 | 
			
		||||
    ymin, ymax = bounds(y_values)
 | 
			
		||||
    for x, y in data: # the loop values must match the 
 | 
			
		||||
        # draw_point function parameters in the plotting.py file
 | 
			
		||||
        scaled_x=scale(x, xmin, xmax, 0, constants.PLOT_WIDTH)
 | 
			
		||||
        scaled_y=scale(y, ymin, ymax, 0, constants.PLOT_HEIGHT) 
 | 
			
		||||
        draw_point(x, y, color, size)
 | 
			
		||||
   
 | 
			
		||||
with no_delay():
 | 
			
		||||
    data = generate_data(50, 10, 500, 5, 400, 1000)
 | 
			
		||||
    draw_scatterplot(data, size=5, color="blue")
 | 
			
		||||
    hideturtle()
 | 
			
		||||
 | 
			
		||||
done()
 | 
			
		||||
 
 | 
			
		||||
@@ -34,3 +34,4 @@ test(scale, [4, 0, 10, 0, 100], 40)
 | 
			
		||||
test(scale, [180, 120, 240, 0, 100], 50)
 | 
			
		||||
test(get_x_values, [[[0, 5], [1, 5], [2, 5]]], [0, 1, 2])
 | 
			
		||||
test(get_y_values, [[[0, 5], [1, 5], [2, 5]]], [5, 5, 5])
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										18
									
								
								transform.py
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								transform.py
									
									
									
									
									
								
							@@ -4,7 +4,6 @@
 | 
			
		||||
# The functions in this module transform data. 
 | 
			
		||||
# None of them are finished; this is your job!
 | 
			
		||||
 | 
			
		||||
import sys
 | 
			
		||||
 | 
			
		||||
def maximum(data):
 | 
			
		||||
    highest = None
 | 
			
		||||
@@ -49,8 +48,8 @@ def clamp(value, low, high):
 | 
			
		||||
        return high
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def ratio(value, start, end):
 | 
			
		||||
    ratio = (value - start)/(end - start)
 | 
			
		||||
def ratio(value, domain_min, domain_max):
 | 
			
		||||
    ratio = (value - domain_min)/(domain_max - domain_min)
 | 
			
		||||
    return clamp(ratio,0,1)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -61,12 +60,15 @@ def scale(value, domain_min, domain_max, range_min, range_max):
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_x_values(points):
 | 
			
		||||
    for (x, y) in points:
 | 
			
		||||
        return ([x], )
 | 
			
		||||
    x_values=[]
 | 
			
		||||
    for ([x, y]) in points:
 | 
			
		||||
        x_values.append(x)
 | 
			
		||||
    return x_values
 | 
			
		||||
    
 | 
			
		||||
def get_y_values(points):
 | 
			
		||||
    for (x, y) in points:
 | 
			
		||||
        return ([y], )
 | 
			
		||||
       
 | 
			
		||||
    y_values=[]
 | 
			
		||||
    for ([x, y]) in points:
 | 
			
		||||
        y_values.append(y)
 | 
			
		||||
    return y_values
 | 
			
		||||
  
 | 
			
		||||
        
 | 
			
		||||
		Reference in New Issue
	
	Block a user