MdC
MdC

Reputation: 41

Graphs all appear on same plot in Jupyter

I want to display a a series of graphs using a loop on one plot in a Jupyter cell, and then in another cell to display another plot. I plot this curves on the first plot using a loop:

def plotTrajectory(launchAngle):
    [yRange, xRange] = setTrajectory(launchAngle)
    plt.plot(yRange, xRange, label=launchAngle)
    plt.ylabel('Vertical distance (m)')
    plt.xlabel('Horiontal distance (m)')
    plt.legend(bbox_to_anchor=(0.79, 1), title='Launch angle', loc='upper left', borderaxespad=0)

for i in launchAngles:
    plotTrajectory(i)

and then try to plot another curve in a separate cell:

plt.plot(angleRange90, heights, label='Height')
plt.show()

and both appear on the same plot. Do I need plt.close() or plt.clf()? I'm sure this is simple and is a misunderstanding I have about how matplotlib works, but I can find nothing online regarding this specific case.

enter image description here

Upvotes: 1

Views: 70

Answers (1)

Mykola Semenov
Mykola Semenov

Reputation: 802

Use plt.figure:

def plotTrajectory(launchAngle):
    [yRange, xRange] = setTrajectory(launchAngle)
    plt.plot(yRange, xRange, label=launchAngle)
    plt.ylabel('Vertical distance (m)')
    plt.xlabel('Horiontal distance (m)')
    plt.legend(bbox_to_anchor=(0.79, 1), title='Launch angle', loc='upper left', borderaxespad=0)


for i in launchAngles:
    plotTrajectory(i)

plt.figure()
plt.plot(angleRange90, heights, label='Height')
plt.show()

Upvotes: 2

Related Questions