1

As per the matplotlib documentation, x and/or y may be 2D arrays, and in this case the columns are treated as different datasets. When I follow the example in the matplotlib page it works fine:

>>> x = [1, 2, 3]
>>> y = np.array([[1, 2], [3, 4], [5, 6]])
>>> plot(x, y)

However, when I try with larger, float64 arrays, it plots a weird figure. This is what I got

from scipy.stats import chi2

x = np.linspace(0,5,1000)
chi2_2, chi2_5 = chi2.pdf(x,2), chi2.pdf(x,5)
y = np.array((chi2_2,chi2_5)).reshape(1000,2)
fig, ax = plt.subplots()
ax.plot(x,y)

and produces this plot:

enter image description here

if I plot them separately, it comes out fine:

fig, ax = plt.subplots()
ax.plot(x,chi2_2,'b')
ax.plot(x,chi2_5,'r')

enter image description here

I can't figure out what is the difference between the example and my case other then using 2D arrays with Float64 instead of Int64. Any help is appreciated.

1 Answer 1

2

It looks like reshape isn't doing what you expect it to do. I think the function that you are looking for is transpose rather than reshape.

from scipy.stats import chi2

x = np.linspace(0,5,1000)
chi2_2, chi2_5 = chi2.pdf(x,2), chi2.pdf(x,5)

y = np.array((chi2_2,chi2_5)).T
y2 = np.array((chi2_2,chi2_5)).reshape(1000,2)
print(np.array_equal(y,y2))
fig, ax = plt.subplots()
ax.plot(x,y)
plt.show()

Using transpose returns the plot that you want and np.array_equal(y,y2) being False confirms that the 2 arrays are not the same.

Below is the output:

enter image description here

1
  • 1
    Thank you! That's exactly what the problem was
    – gmolnar
    Commented Nov 19, 2021 at 17:15

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.