Matplotlib.pyplot.plot is able to plot multiple vector pairs but only when the first dimension of y matches x's

Question:

This was very hard to write a title for.
By introducing a bug I discovered an interesting feature of matplotlib and I would like to understand how it works:

import numpy as np       
import matplotlib.pyplot as plt
from scipy.stats import multivariate_normal     
from scipy.spatial import distance

n = 100
X = np.linspace(0, 10, 100).reshape(-1, 1)
D = distance.pdist(X, 'euclidean')
D = distance.squareform(D)
S = np.exp(-D)
mvn = multivariate_normal(np.ones(100), S)
y = [mvn.rvs(1) for i in range(100)]
plt.plot(X, y)
plt.show()

Now this seems to plot one plot X, y[:, i] for i in 100.

Or does it not?

Because it doesn’t work with:

y = [mvn.rvs(1) for i in range(100)]

However this works again as expected:

y = np.array([mvn.rvs(1) for i in range(5)])
plt.plot(X, y.T)
plt.show()

What does matplotlib plot here?
is it X, y[i, :] instead?

So the correct syntax would be Sample Space, [samples]?

Thank you very much for your insight, I only wanted to plot one of these and made myself to this cool discovery by forgetting that rvs(1) is enough.

Asked By: Olli

||

Answers:

From the docs
for plt.plot(x, y)

If x and/or y are 2D arrays a separate data set will be drawn for every column. If both x and y are 2D, they must have the same shape. If only one of them is 2D with shape (N, m) the other must have length N and will be used for every data set m.

Its basically a convenient behaviour instead looping manually over your data.

Answered By: scleronomic
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.