Visualise Principal Components Code No Longer Works for Version 2.0

Skip to first unread message

Angela K

Jun 8, 2022, 8:56:03 AMJun 8
to MDnalysis discussion

I recently updated my MDAnalysis version from 1.0.0 to 2.0.0 but suddenly found that there is a numpy error when I run the code for visualising my principal components (I never had this issue with version 1.0.0). Here is the code I'm trying to run:

# generate the projected coordinates of the first principal component
# The mean positions are stored at pc.mean
pc1= pc.p_components[:, 0]
trans1 = transformed[:, 0]
projected = np.outer(trans1, pc1) + pc.mean
coordinates = projected.reshape(len(trans1), -1, 3)

And here is the error:

"ValueError Traceback (most recent call last) <ipython-input-9-bd9fa4c6219f> in <module>
3 pc1= pc.p_components[:, 0] 4
4 trans1 = transformed[:, 0]
----> 5 projected = np.outer(trans1, pc1) + pc.mean 6
6 coordinates = projected.reshape(len(trans1), -1, 3)
ValueError: operands could not be broadcast together with shapes (44,4290) (1430,3)"

Not sure why suddenly numpy is complaining with the new version? Thanks for your help.

Best wishes,


Hugo Macdermott-Opeskin

Jul 22, 2022, 11:21:57 PMJul 22
to MDnalysis discussion
Hi Angela, 

Would you be able to post your full code including the MDAnalysis bits so that I can see what might be happening?
There were some changes to the way components and means work between 1.0 and 2.0 (see here for the PCA versioning entry) so perhaps that will explain it.
I can work through it better with a full example



Kabir Biswas

Sep 13, 2022, 12:01:24 PM (11 days ago) Sep 13
to MDnalysis discussion
Dear Hugo,

We too faced the issue with the shapes of the arrays.

I would appreciate it if you could help identify the issue and suggest a solution.


Sep 13, 2022, 2:59:35 PM (11 days ago) Sep 13
to MDnalysis discussion

Dear Kabir,

The userguide link you use above is unfortunately not the correct one*, please instead use:

I assume the issue you saw was related to `projected = np.outer(trans1, pc1) + pc.mean`? If so, you will see in the updated userguide that this has now changed to `projected = np.outer(trans1, pc1) + pc.mean.flatten()`.

Best regards,


Kabir Biswas

Sep 15, 2022, 7:34:28 AM (9 days ago) Sep 15
to MDnalysis discussion
Dear Irfan,

Thanks for the pointer. It is working now.

Reply all
Reply to author
0 new messages