Back to Math Projects

Python Personal Graphing Application Project

Instructions:

Use the code below to start create your own personal grapher or use this code to start the Calculus Integration projects and videos. I have a video lessons on how to build this grapher from scratch located in my video lessons link in the navigation bar above.

#-------------------------------------------------------------------------------
# Name:        Personal Grapher
# Purpose:     code to copy and paste for projects that need a grapher to start
#
# Author:      David @ ElectricTeaching.com
#
# Created:     3/10/2012
#-------------------------------------------------------------------------------
#My own Personal Grapher 

#import and set of variables
import pygame, math, sys
from pygame import *
from math import *
pygame.init()

font = pygame.font.SysFont('Verdana',16)
font2 = pygame.font.SysFont('Serif',24)
font3 = pygame.font.SysFont('Arial',14)
white = (255,255,255)
black = (0,0,0)
graphcolor = (200,0,200) #purple
gridcolor = (100,250,240) #light blue
titlecolor = (200,0,150) #darker purple
green = (0,200,50)
blue = (0,50,200)

width,height = 400, 400
extraW = 400
screen = pygame.display.set_mode((width+extraW,height))
pygame.display.set_caption("My Own Personal Grapher")

#graph paper function
def graphpaper(k):
    screen.set_clip(0,0,width,height)
    screen.fill(white)

    #draw graph paper
    for i in range(width/k):
        gridx = k*i
        gridy = k*i
        pygame.draw.line(screen, gridcolor, (gridx,0), (gridx,height), 1)
        pygame.draw.line(screen, gridcolor, (0,gridy), (width, gridy), 1)
    # extra thick line
    pygame.draw.line(screen, gridcolor, (width,0), (width, height), 5)

    # graph x and y axis
    midx, midy = width/(2*k), height/(2*k)
    pygame.draw.line(screen, black, (midx*k,0), (midx*k,height), 3)
    pygame.draw.line(screen, black, (0,midy*k), (width,midy*k), 3)

    #clip reset to all the window
    screen.set_clip(None)
    
# the main function that runs the program
def mymain(Graphs,eq):
    #pixle per grid (always use a k that is a factor of width and height)
    k = 25
    
    # start an array that will hold equation
    if Graphs == 1:        
        #clear screen
        screen.fill(white)
        graphpaper(k)
        equation=[]
        eq2= ' '
    else:
        screen.set_clip(width, 0, width+extraW, height)
        screen.fill(white)
        screen.set_clip(None)
        eq2 = eq
        equation=[]
    
    #instructions and results
    title = font2.render("Electric Teaching Personal Grapher",1,titlecolor)
    screen.blit(title, (width+10, 20))

    instruct = font.render("Type in equation. Ex -3*x^2+1",1,black)
    screen.blit(instruct, (width+10, 70))

    instruct = font.render("Select 'Enter' when done or 'q' to start over.",1,black)
    screen.blit(instruct, (width+10, 100))

    instruct = font.render("Select 'Backspace' to clear.",1,black)
    screen.blit(instruct, (width+10, 130))
    
    instruct = font3.render("s=sin(), c=cos(), t=tan(), r=sqrt(), a=abs(), l=log10(), n=log(), e=e, p=pi",1,black)
    screen.blit(instruct, (width+10, 160))

    done=False  

    # active loop     
    active = True
    while active:
        # update the screen
        screen.set_clip(width+10, height-30, width+extraW, height)
        screen.fill(white)

        #join equation array without commas.
        eq = string.join(equation)
        eq = string.replace(eq," ", "")

        #render and blit equation
        eqshow = font.render("Function:  y = "+eq,1,titlecolor)
        screen.blit(eqshow, (width+10,height-30))
        
        pygame.display.update()

        #keyboard and mouse actions
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                active = False
                done = True


            elif event.type == pygame.KEYDOWN:

                #math operations and symbols for the equation
                if event.unicode == u'*':
                    equation.append("*")
                elif event.unicode == u'+':
                    equation.append("+")
                elif event.unicode == u'/':
                    equation.append("/")
                elif event.unicode == u'-':
                    equation.append("-")
                elif event.unicode == u'.':
                    equation.append(".")
                elif event.unicode == u'(':
                    equation.append("(")
                elif event.unicode == u')':
                    equation.append(")")
                elif event.unicode == u'^':
                    equation.append("**")

                    

                # numbers typed in for equation and the x variable
                elif event.key == K_1:
                    equation.append("1")
                elif event.key == K_2:
                    equation.append("2")
                elif event.key == K_3:
                    equation.append("3")
                elif event.key == K_4:
                    equation.append("4")
                elif event.key == K_5:
                    equation.append("5")
                elif event.key == K_6:
                    equation.append("6")
                elif event.key == K_7:
                    equation.append("7")
                elif event.key == K_8:
                    equation.append("8")
                elif event.key == K_9:
                    equation.append("9")
                elif event.key == K_0:
                    equation.append("0")

                #math function commands
                elif event.key == K_s:
                    equation.append("sin(")
                elif event.key == K_c:
                    equation.append("cos(")                
                elif event.key == K_t:
                    equation.append("tan(")                
                elif event.key == K_r:
                    equation.append("sqrt(")
                elif event.key == K_a:
                    equation.append("abs(")                
                elif event.key == K_l:
                    equation.append("log10(")                
                elif event.key == K_n:
                    equation.append("log(")                
                elif event.key == K_e:
                    equation.append("e")                
                elif event.key == K_p:
                    equation.append("pi")                



                elif event.key == K_x:
                    equation.append("x")

                elif event.key == K_RETURN:
                    active = False
                elif event.key == K_BACKSPACE:
                    equation = []
                elif event.key == K_q:
                    mymain(1,' ')
    # an option to quit at entering equation or to graph
    if done:
        # only thing left to do is quit
        pygame.quit()
    else:
        # clip right side of the screen (except equation)
        screen.set_clip(width,0,width+extraW,height-30)
        screen.fill(white)
        screen.set_clip(None)

        #graph equation function called
        GraphEq(eq,eq2,k)
    sys.exit()
def GraphEq(eq,eq2,k):
    

    #graphing of the equation
    for i in range(width):
        try:
            x = (width/2-i)/float(k)
            y = eval(eq)
            pos1 = (width/2+x*k, height/2-y*k)

            nx = x = (width/2-(i+1))/float(k)
            ny = eval(eq)
            pos2 = (width/2+nx*k, height/2-ny*k)

            pygame.draw.line(screen, graphcolor, pos1, pos2, 3)
        except:
            pass
        try:
            x = (width/2-i)/float(k)
            y2 = eval(eq2)
            pos21 = (width/2+x*k, height/2-y2*k)

            nx = x = (width/2-(i+1))/float(k)
            ny2 = eval(eq2)
            pos22 = (width/2+nx*k, height/2-ny2*k)

            pygame.draw.line(screen, graphcolor, pos21, pos22, 3)
        except:
            pass

    #clip the screen to keep text clean
    screen.set_clip(width,0,width+extraW,height-30)
    screen.fill(white)
    #instructions and results
    title = font2.render("Electric Teaching Personal Grapher",1,titlecolor)
    screen.blit(title, (width+10, 20))
    instruct = font.render("Select 'q' to start over.",1,black)
    screen.blit(instruct, (width+10, 70))

    #compute and display y-intercept
    x = 0
    try:
        yint = eval(eq)
        yint = round(yint,2)
    except: yint = 'dne'
    
    instruct = font.render("The y-intercept is at (0,"+str(yint)+")",1,green)
    screen.blit(instruct, (width+10, 100))

    if yint <> 'dne':
        instruct = font.render("Select 'y' to plot the intercept",1,green)
        screen.blit(instruct, (width+20, 120))

    # resize of grid instructions
    instruct = font.render("Select 's' 'm' 'l' 'o' for grid sizes.",1,black)
    screen.blit(instruct, (width+10, height-70))

    #instruction for new graph
    instruct = font.render("Select 'g' to add another graph.",1,black)
    screen.blit(instruct, (width+10, height-100))

    instruct = font.render("Type in value, select 'Enter' to plot the point.",1,blue)
    screen.blit(instruct, (width+20, 150))
    #plotting of x values
    xValue = []
    xVal = '?'
    yVal = '?'  

    
    #reset clip of screen
    screen.set_clip(None)
    
    # run a infinite loop to control the window
    active=True
    while active:
        #clip the screen to show x and y value
        screen.set_clip(width+10,150, width+extraW,200)
        screen.fill(white)
        xDisplay = string.join(xValue)
        xDisplay = string.replace(xDisplay, " ", "")
        plotx = font.render("x = "+str(xDisplay),1,blue)
        screen.blit(plotx, (width+10, 170))
        ploty = font.render("("+str(xVal)+","+str(yVal)+")",1,blue)
        screen.blit(ploty, (width+210, 170))
        screen.set_clip(None)

        #update the screen
        pygame.display.update()

        #keyboard and mouse actions
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                active = False
            # commands for starting over and other features
            elif event.type == pygame.KEYDOWN:
                if event.key == K_q:
                    mymain(1,' ')
                elif event.key == K_y:
                    pygame.draw.circle(screen, green, (width/2,height/2-yint*k),4)

                #resize commands
                elif event.key == K_s:
                    k = 25
                    graphpaper(k)
                    GraphEq(eq,eq2,k)
                elif event.key == K_m:
                    k = 20
                    graphpaper(k)
                    GraphEq(eq,eq2,k)
                elif event.key == K_l:
                    k = 10
                    graphpaper(k)
                    GraphEq(eq,eq2,k)
                elif event.key == K_o:
                    k = 5
                    graphpaper(k)
                    GraphEq(eq,eq2,k)

                #add new graph and plot points
                elif event.key == K_g:
                    mymain(2,eq)
                           
                elif event.key == K_RETURN:
                    try:
                        x = xVal = float(xDisplay)
                        yVal = eval(eq)
                        pygame.draw.circle(screen, blue, (width/2+x*k,height/2-yVal*k),4)
                        xValue=[]
                    except:
                        pass

            # numbers typed in for equation and the x variable
                elif event.key == K_1:
                    xValue.append("1")
                elif event.key == K_2:
                    xValue.append("2")
                elif event.key == K_3:
                    xValue.append("3")
                elif event.key == K_4:
                    xValue.append("4")
                elif event.key == K_5:
                    xValue.append("5")
                elif event.key == K_6:
                    xValue.append("6")
                elif event.key == K_7:
                    xValue.append("7")
                elif event.key == K_8:
                    xValue.append("8")
                elif event.key == K_9:
                    xValue.append("9")
                elif event.key == K_0:
                    xValue.append("0")

                elif event.unicode == u'.':
                    xValue.append(".")
                elif event.unicode == u'-':
                    xValue.append("-")

    #only thing left to do is quit
    pygame.quit()
    sys.exit()
        
        

#Python' way of running a program
    
if __name__=='__main__':
    mymain(1,' ')      

                    
David Johns and Electric Teaching ~ All Rights Reserved © 2013