0

This has been surprisingly difficult to find information on. I have two functions that I want to chart together, enumeration() and betterEnumeration()

import matplotlib.pyplot as plt
import time
import numpy as np
import sympy
from sympy import S, symbols
import random
from math import floor

def enumeration(array):
    max = None
    to_return = (max, 0, 0)
    for i in range(0, len(array) + 1):
        for j in range(0, i):
            currentSum = 0
            for k in range(j, i):
                currentSum += array[k]
                if (max is None) or (currentSum > max):
                    max = currentSum
                    to_return = (max, j, k) 
    return to_return

def betterEnumeration(array):
    max = None
    to_return = (max, 0, 0)
    for i in range(1, len(array) + 1):
        currentSum = 0
        for j in range(i, len(array) + 1):
            currentSum += array[j - 1]
            if (max is None) or (currentSum > max):
                max = currentSum        
                to_return = (max, i-1, j-1)    
    return to_return

I also have two helper functions randomArray() and regressionCurve().

def randomArray(totalNumbers,min,max):
    array = []
    while totalNumbers > 0:
        array.append(random.randrange(min,max))
        totalNumbers -= 1
    return array

def regressionCurve(x,y):
    # calculate polynomial
    p = np.polyfit(x, y, 3)
    f = np.poly1d(p)

    # calculate new x's and y's
    x_new = np.linspace(x[0], x[-1], 50)
    y_new = f(x_new)

    x = symbols("x")
    poly = sum(S("{:6.5f}".format(v))*x**i for i, v in enumerate(p[::-1]))
    eq_latex = sympy.printing.latex(poly)

    plt.plot(x_new, y_new, label="${}$".format(eq_latex))
    plt.legend(fontsize="small")
    plt.show()

I want to plot both of these functions on the same chart, both the raw data points as well as the regression curves. The following code will chart the data points for enumeration() and then make a regression curve for them, but I'm not sure how to plot both enumeration() and betterEnumeration() on the same chart.

def chart():
    nValues = [10,25,50,100,250,500,1000]
    avgExecTimes = []
    for n in nValues: # For each n value
        totals = []
        sum = 0
        avgExecTime = 0
        for i in range(0,10): # Create and test 10 random arrays
            executionTimes = []
            array = randomArray(n,0,10)
            t1 = time.clock()
            enumeration(array)
            t2 = time.clock()
            total = t2-t1
            totals.append(total)
            executionTimes.append(total)
            print("Time elapsed(n=" + str(n) + "): " + str(total))
        for t in totals: # Find avg running time for each n's 10 executions
            sum += t
        avgExecTime = sum/10
        avgExecTimes.append(avgExecTime)
        print("Avg execution time: " + str(avgExecTime))

    # Chart execution times
    plt.plot(nValues,avgExecTimes)
    plt.ylabel('Seconds')
    plt.xlabel('n')
    plt.show()

    # Chart curve that fits
    x = np.array(nValues)
    y = np.array(avgExecTimes)
    regressionCurve(x,y)
123
  • 8,733
  • 14
  • 57
  • 99

1 Answers1

4

To add a line to a plot:

plt.plot(x,y)

so, if you wanted to plot x1, y1 and then add x2,y2:

plt.plot(x1,y1)
plt.plot(x2,y2)

However, that's going to plot the second line in the default color. You're going to want to add a color component:

plt.plot(x1,y1, c='b')
plt.plot(x2,y2, c= 'g')

and if the units are different, you'll want to look into twinx, which will allow you to plot with 2 different y axes but the same x axis.

You're going to want to plot both sets of data from within the same function or both outside of the function. Otherwise, you're running into a local vs. global issue as well.

mauve
  • 2,707
  • 1
  • 20
  • 34