2

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.

1 Answer 1

3

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()
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much! It's exactly what I needed. I had almost the same code as you, but I placed ys=[] and vals=[] before the first while and that was a wrong move :-) Tx also for the label format, that I didn't knew!
Maybe I have another small question. Here you use .format. Now every plot has another color, is there also a command to let them have another line genre (like dashed or circles or whatever)?
I've added some code to show how to assign a different marker to each plot. You can change the particular markers used by redefining the markers variable. The available choices are documented here and this example shows how to use LaTeX characters as markers too. Since I don't know how many markers you need, I used itertools.cycle to make the markers cycle ad infinitum.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.