Draw Axes With Tick Marks

File: draw_axes.py

# -------------------------------------------------------------------
# ---- function: draw X,Y axes with tick marks
# -------------------------------------------------------------------

from graphics import *

def draw_xy_axes(win,tickmarks,linewidth=1,linecolor="black"):

    wx  = win.width            # window width
    wy  = win.height           # window height
    wcx = round(wx/2.0)        # window center X
    wcy = round(wy/2.0)        # window center Y

    # ---- X axis

    xl = Line(Point(0,wcy),Point(wx-1,wcy))
    xl.setWidth(linewidth)
    xl.setFill(linecolor)
    xl.draw(win)

    # ---- Y axis

    yl = Line(Point(wcx,0),Point(wcx,wy-1))
    yl.setWidth(linewidth)
    yl.setFill(linecolor)
    yl.draw(win)

    # ---- tick marks?

    if not tickmarks:
        return

    x1 = wcx                   # X axis center
    x2 = wcx                   # X axis center
    y1 = wcy                   # Y axis center
    y2 = wcy                   # Y axis center

    xt1 = wcy - 5              # X axis tick start
    xt2 = wcy + 6              # x axis tick end
    yt1 = wcx - 5              # y axis tick start
    yt2 = wcx + 6              # Y axis tick end

    for _ in range(7):

        x1 += 50
        t = Line(Point(x1,xt1),Point(x1,xt2))
        t.setWidth(linewidth)
        t.setFill(linecolor)
        t.draw(win)

        x2 -= 50 
        t = Line(Point(x2,xt1),Point(x2,xt2))
        t.setWidth(linewidth)
        t.setFill(linecolor)
        t.draw(win)

        y1 += 50
        t = Line(Point(xt1,y1),Point(xt2,y1))
        t.setWidth(linewidth)
        t.setFill(linecolor)
        t.draw(win)

        y2 -= 50
        t = Line(Point(xt1,y2),Point(xt2,y2))
        t.setWidth(linewidth)
        t.setFill(linecolor)
        t.draw(win)

    return