Home > Software design >  Plot multiple lines in one chart using function
Plot multiple lines in one chart using function

Time:11-07

I am working on a project and I need to plot multiple lines in one chart using function, I tried several times but I still don't know what is missing.enter image description here

I did it without a function and it was successful but using the function is mandatory. enter image description here

CodePudding user response:

What you see is an artifact of using Matplotlib's "inline" backend in a notebook, the same code tested in a command line interpreter works as you intended, three lines and a single legend in a single Axes.

To remedy the problem is, fortunately, very easy: just dedent plt.show, so that it's executed once, when all the three lines have been placed in a single Axes.

def Plot(*cols):
    for c in cols: plt.plot(...)
    plt.legend() ; plt.show()

(note that also plt.legend can/should be outside of the loop).


Post Scriptum

If your function is meant to be executed in a "cell" of a Jupyter notebook, you can just omit completely the plt.show, because Jupyter will do that for you when you execute the cell.


Post Post Scriptum

enter image description here

  • Related