diff --git a/constants.py b/constants.py index d77003b..4f7b192 100644 --- a/constants.py +++ b/constants.py @@ -2,3 +2,5 @@ PLOT_HEIGHT = 400 PLOT_WIDTH = 400 PLOT_PADDING = 20 TICK_LENGTH = 5 + +"""Write in all caps.""" \ No newline at end of file diff --git a/planning_scatter_corrected.md b/planning_scatter_corrected.md new file mode 100644 index 0000000..607afe2 --- /dev/null +++ b/planning_scatter_corrected.md @@ -0,0 +1,51 @@ +# Planning the scatter plot + +# 9.12.2023_Watched the video "August 2_Scatter plot lab live-coding" +# ??? Was my pseudocode ok, going into it in more detail? + +# My pseudocode here, below is your pseudocode from video +- from turtle import * +- Draw a scatter plot. + - Draw the axes. + - Draw the x-axis. + - Draw the horizontal line. + - Draw 6 ticks (short dashed lines) touching beneath the baseline of x-axis at minimum to maximum + location points "14 100 200 300 400 492" evenly spaced across at every 100. + - Name x axis labels "14 100 200 300 400 492" centered underneath the x axis ticks. + - Draw the y-axis. + - Draw the vertical line. + - Draw 6 ticks (short dashed lines) touching to the left side of the y-axis line at minimum to + maximum location points "-927 0 1000 2000 3000 3710" evenly spaced across at every 1,000. + - Name the y-axis labels to the left of the y axis ticks. + - Plot the data points. + Creating a visualization of graph with dots representing data points at x-y locations. + - ?Determine the screen size. + - Set the x-axis value ranges from 14 to 492. + _ Set the y-axis value ranges from -927 to 3710. + - Define the "data_points" function. + - Set the dot style. + - Set the dot size. + - Set the dot color. + - Define a scatter function to plot the "x, y" data points. + - ? Set a function to show the plot. + +# Your pseudocode from video +- from turtle import * +- Draw a scappter plot. + - Draw the axes. + - Draw the x-axis. + - Draw the line. + - Figure out where the ticks should go + - For each tick, + - Draw the tick. +- Draw the y-axis. + - Draw the line. + - Figure out where the ticks should go + - For each tick, + - Draw the tick. +- Plot the points. + - For each point, + - Draw the point at the appropriate location. + + + \ No newline at end of file diff --git a/poetry.lock b/poetry.lock index 303a15d..619181e 100644 --- a/poetry.lock +++ b/poetry.lock @@ -159,13 +159,13 @@ files = [ [[package]] name = "furo" -version = "2023.8.19" +version = "2023.9.10" description = "A clean customisable Sphinx documentation theme." optional = false python-versions = ">=3.8" files = [ - {file = "furo-2023.8.19-py3-none-any.whl", hash = "sha256:12f99f87a1873b6746228cfde18f77244e6c1ffb85d7fed95e638aae70d80590"}, - {file = "furo-2023.8.19.tar.gz", hash = "sha256:e671ee638ab3f1b472f4033b0167f502ab407830e0db0f843b1c1028119c9cd1"}, + {file = "furo-2023.9.10-py3-none-any.whl", hash = "sha256:513092538537dc5c596691da06e3c370714ec99bc438680edc1debffb73e5bfc"}, + {file = "furo-2023.9.10.tar.gz", hash = "sha256:5707530a476d2a63b8cad83b4f961f3739a69f4b058bcf38a03a39fa537195b2"}, ] [package.dependencies] diff --git a/scatterplot.py b/scatterplot.py index 37964e9..853796d 100644 --- a/scatterplot.py +++ b/scatterplot.py @@ -33,6 +33,9 @@ def draw_scatterplot(data, size=5, color="black"): draw_axes(data) draw_points(data, color, size) +"""I understood from instructions and 1st try how to draw_axes for both x and y values. +Oh, to use the PLOT.HEIGHT for y because it's the vertical.""" + def draw_axes(data): "Draws the scatter plot's axes." draw_x_axis() @@ -49,12 +52,33 @@ def draw_axes(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_WIDTH) + 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." + """THE PLAN + Find the x-bounds and the y-bounds of the data. You'll need these for scaling. + Can shorten by using: + xmin, xmax = bounds(get_x_values(data)) + ymin, ymax = bounds(get_y_values(data)) + For each point in the data + for points in data: or for x, y in data: + Get the x and y value from the point. + Find the scaled x-position for the point. + Find the scaled y-position for the point. + Use draw_point(scaled_x, scaled_y, color, size) to draw the point.""" + + 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: + scaled_x = scale(x, xmin, xmax, 0, constants.PLOT_WIDTH) + scaled_y = scale(y, ymin, ymax, 0, constants.PLOT_HEIGHT) + draw_point(scaled_x, scaled_y, color, size) + with no_delay(): data = generate_data(50, 10, 500, 5, 400, 1000) draw_scatterplot(data, size=5, color="blue") diff --git a/scatterplot_1st try before video.py b/scatterplot_1st try before video.py new file mode 100644 index 0000000..37964e9 --- /dev/null +++ b/scatterplot_1st try before video.py @@ -0,0 +1,62 @@ +# scatterplot.py +# ------------ +# By MWC Contributors +# Uses lots of helper functions in other modules to draw a scatter plot. + +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 ( + 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" + 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() + "Draws the scatter plot's axes." + 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_WIDTH) + draw_y_tick(screen_y_position, tick) + +def draw_points(data, color, size): + "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() +done() diff --git a/transform.py b/transform.py index 5152272..a4625a7 100644 --- a/transform.py +++ b/transform.py @@ -4,6 +4,8 @@ # The functions in this module transform data. # None of them are finished; this is your job! +# Corrected on 9.12.2023 from watching video, August 2_Scatter plot lab live-coding + def maximum(data): "Returns the largest number in data" largest = None @@ -14,6 +16,15 @@ def maximum(data): largest = number return largest +"""Find minimum Data is a list of numbers, such as [3, 5, 7, 6, 4, 2] + number = ? + smallest = ? + example: if number = 3 + example: if 3 is the only number, than smallest = 3 + then you go through the numbers in the list [3, 5, 7, 6, 4, 2] + so number = 2 + lowest = 2, because it's smaller than 3, now 2 becomes the smallest number + For a considerable large amount of numbers you will use the code for the smallest number""" def minimum(data): "Returns the smallest number in data" smallest = None @@ -24,22 +35,31 @@ def minimum(data): smallest = number return smallest + +""" + The "scope of a variable" doesn't relate to the above defined functions, it's separate in it's on function. + smallest and largest changed from "None" to "minimum(data)" and "maximum(data)" + I didn't need to use "for and if else statments. Keep it simple. + I was on track with Last line need to add brackets [ ] + + Also, you can define the function on 1 line. + return[minimum(data), maximum(data)] + """ def bounds(data): "Returns a list of the smallest and largest numbers in data" - smallest = None - largest = None - for numbers in data: - if numbers < smallest: - smallest = numbers - elif numbers > largest: - largest = numbers - return smallest, largest + smallest = minimum(data) + largest = maximum(data) + return [smallest, largest] 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. """ + + """I did this correctly. + I learned the if, else if, else statement from Java classes. I like using "elif". """ + if value < low: return low elif value > high: @@ -52,34 +72,46 @@ def ratio(value, start, 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. """ + """ratio(-1, 0, 10) + -0.1""" + """Ratio is function to find how much what percentage is finished from start to end. + Find out what's the relationship between how far you've gone from start + to the whole distance you have to go. + unclamped_ratio = (value - start) / (end - start) correct except for unclamped_ratio, I used value. + return amount_already_finished / total_distance_to_cover + I get confused with the wording such as using "clamp and unclamped_ratio." """ - """??? Is this correct?""" - value = (value - start) / (end - start) - if value >= 0: - return 0 - elif value <= 1.0: - return 1.0 - else: - return value + unclamped_ratio = (value - start) / (end - start) + return clamp(unclamped_ratio, 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." - """???? I'm unsure how to code def scale""" + """This is correct, but for using return range value = range_min + r * (range_max - range_min) + I had to define what "r" is. I understand. """ - return value + r = ratio(value, domain_min, domain_max) + return range_min + r * (range_max - range_min) +"""Points: [[1, 1], [2, 2], [4, 3], [7, 3]] + List of x and y points on a plane. You're probably going plot on a grid. + Can begin with + for x, y points in points: -It's unpacking, the x and y values + ... get the x value, and add it to the x_values + the ".append" goes through each x value.""" def get_x_values(points): "Returns the first value for each point in points." - - """???? I'm unsure how to code def get_x_values""" - points = x_values - return value + 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." - """???? I'm unsure how to code def get_y_values""" - points = y_values - return value + y_values = [] + for x, y in points: + y_values.append(y) + return y_values + diff --git a/transform_1st try before video.py b/transform_1st try before video.py new file mode 100644 index 0000000..5152272 --- /dev/null +++ b/transform_1st try before video.py @@ -0,0 +1,85 @@ +# transform.py +# ------------ +# By MWC Contributors +# The functions in this module transform data. +# None of them are finished; this is your job! + +def maximum(data): + "Returns the largest number in data" + largest = None + for number in data: + if largest is None: + largest = number + if number > largest: + largest = number + return largest + +def minimum(data): + "Returns the smallest number in data" + smallest = None + for number in data: + if smallest is None: + smallest = number + if number < smallest: + smallest = number + return smallest + +def bounds(data): + "Returns a list of the smallest and largest numbers in data" + smallest = None + largest = None + for numbers in data: + if numbers < smallest: + smallest = numbers + elif numbers > largest: + largest = numbers + return smallest, largest + +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. + """ + if value < low: + return low + elif value > high: + return high + else: + return value + +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. + """ + + """??? Is this correct?""" + value = (value - start) / (end - start) + if value >= 0: + return 0 + elif value <= 1.0: + return 1.0 + else: + return value + +def scale(value, domain_min, domain_max, range_min, range_max): + "Given a value within a domain, returns the scaled equivalent within range." + + """???? I'm unsure how to code def scale""" + value = range_min + r * (range_max - range_min) + + return value + +def get_x_values(points): + "Returns the first value for each point in points." + + """???? I'm unsure how to code def get_x_values""" + points = x_values + return value + +def get_y_values(points): + "Returns the second value for each point in points." + + """???? I'm unsure how to code def get_y_values""" + points = y_values + return value