Issue
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.
I did it without a function and it was successful but using the function is mandatory.
Solution
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
The OP laments continuing problems, but I cannot reproduce their issues. To this aim, I have defined two functions, one with show
outside the loop and another with show
inside the loop, and at the bottom you can see what I get using the Jupyter notebook.
I whish good luck to the OP, I'm not going to help them further.
Answered By - gboffi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.