Akashraj
Akashraj

Reputation: 31

What does the third parameter of plt.plot() in matplotlib do?

plt.plot() usually accepts x,y and format. But if I pass third argument as data another line is plotted. I can't understand the relationship.

x=np.linspace(0,10,5)
plt.plot(x,x,x,label='linear')
plt.grid()

Created plot: Find the output of above code here

Upvotes: 3

Views: 1274

Answers (1)

William Miller
William Miller

Reputation: 10320

This page of the docs outlines the call signatures for matplotlib.pyplot.plt(). Specifically relevant here is the signature

plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)

And the parameter description

x, y: array-like or scalar

The horizontal and vertical components of the data points. x values are optional and default to range(len(y)).

When you do something like

plt.plot(x,x,x)

You are actually specifying a first set of x values followed by the corresponding y values and then a second set of y values which are paired with the default x values of range(len(y)) and plotted, resulting in two lines.

Upvotes: 1

Related Questions