Dylan.Rockefeller
Dylan.Rockefeller

Reputation: 87

Plotting multiple lines from two lists with matplotlib

In the following code, a,b,c represent three expressions: 10x+7y=200, 11x-8y=63, and x+y=42. I would like to graph each of these expressions and I'm not sure what the best way to go about it is.

When I yield the following code:

import matplotlib.pyplot as plt

#Set minimum graph boundary
xMin = 0
yMin = 0

#a,b,c variables pulled from multiple expressions (ax+by=c)
a = [10,11,1]
b = [7,-8,1]
c = [200,63,42]

def init():
    #Create x,y lists // These will contain x,y plots
    x = []
    y = []

    def findxy(a,b,c):
    #Analyzes instances of (ax+by=c) and returns x,y; appends them to lists

        #Finds x,y for ax+by=c
        x.append((-b*yMin)/a + c/a)
        y.append((-a*xMin)/b + c/b)

    def printxy(x,y):
        #Prints results of findxy, followed by "z = 15x + 15y"
        if x >= xMin:
            print '(%s, %s)' % (x,yMin), 15 * x + 15 * yMin
        if y >= yMin:
            print '(%s, %s)' % (xMin,y), 15 * xMin + 15 * y

map(findxy,a,b,c)
map(printxy,x,y)

plt.plot(x,y)
plt.show()

... I get the following result:

>>> 
(20, 0) 300
(0, 28) 420
(5, 0) 75
(42, 0) 630
(0, 42) 630

... where (20,0),(0,28) represent the first expression, 10x+7y=200; (5,0) represents the second expression, omitting one ordered pair because it violates the x≥0 condition (although appending it to x,y, respectively), and (42,0),(0,42) represent the final expression.

How can I convert each of these expressions to its own line to be printed with matplotlib? I've considered creating a new list, line[], that each pass through findxy() will append x,y to line[n+1], but I'm not certain if that's a good way to go.

Upvotes: 1

Views: 5383

Answers (2)

M4rtini
M4rtini

Reputation: 13539

Using numpy:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0,10,100)

a = [10,11,1]
b = [7,-8,1]
c = [200,63,42]

#ax + by = c
# y = (c - ax)/b
for (ai,bi,ci) in zip(a,b,c):
    y = (1.0*ci - ai*x)/bi #multiply by 1.0 to get floats. 

    plt.plot(x,y, label="{a}x + {b}y = {c}".format(a=ai, b=bi, c=ci))

plt.legend()
plt.show()

version using subplots:

import numpy as np
import matplotlib.pyplot as plt
from math import ceil, sqrt

x = np.linspace(0,10,100)

a = [10,11,1]
b = [7,-8,1]
c = [200,63,42]

nPlots = len(a)
gridSize = int(ceil(sqrt(nPlots)))

fig, ax = plt.subplots(gridSize, gridSize)

#ax + by = c
# y = (c - ax)/b
for i, (ai,bi,ci) in enumerate(zip(a,b,c)):
    y = (1.0*ci - ai*x)/bi #multiply by 1.0 to get floats. 

    ax.flat[i].plot(x,y, label="{a}x + {b}y = {c}".format(a=ai, b=bi, c=ci))
    ax.flat[i].legend(loc=0)

#clear empty plots if not enough to fill the whole grid.
for j in ax.flat[i+1:]:
    j.axis('off')

plt.show()

Upvotes: 2

rabs
rabs

Reputation: 333

One of the great things about matplotlib is its function integration - you can directly apply your formula to a numpy array in the plot function.

import numpy as np
from matplotlib import pyplot

def funcfunc(a,b,c):
    x = np.linspace(-10, 10, 100)
    for pos, val in enumerate(a):
        cox, coy, coz = val, b[pos], c[pos]
        pyplot.plot(x, (coz-cox*x)/coy)
    pyplot.show()

This function will generate a plot, between -10 and 10 of your lines (on the x-axis).

Upvotes: 0

Related Questions