VeVi
VeVi

Reputation: 281

Multiple plots with matplotlib in a nested while loop

I'm making a program to control a LCR meter (the specifics aren't important). Therefore I need two nested while loops (easy example):

while x <= stopFrequency:
    y = startVoltage
    while y <= stopVoltage:
        getCapacity = y * 2
        y += stepValueVoltage 
    x += stepValueFrequency 

Now I need to make a plot for the different frequency's (outer loop) of y and getCapacity. I can get a plot of y and getCapacity for one frequency. But for more, I don't know how to get the graphs on one plot.

Upvotes: 2

Views: 4300

Answers (1)

unutbu
unutbu

Reputation: 880717

To put multiple plots ("graphs") on the same axis ("plot"), simply call plt.plot once for each plot.

import matplotlib.pyplot as plt
import itertools
markers = itertools.cycle([ '+', '*', ',', 'o', '.', '1', 'p', ])
while x <= stopFrequency:
    y = startVoltage
    ys = []
    vals = []
    while y <= stopVoltage:
        ys.append(y)
        vals.append(getCapacity)
        getCapacity = y * 2
        y += stepValueVoltage
    plt.plot(ys, vals, 
             label = 'x: {0}'.format(x),
             marker = next(markers))
    x += stepValueFrequency
plt.legend(loc = 'best')
plt.show()

Upvotes: 3

Related Questions