0

I have matrix A of size 50x4 and another matrix B of size 4*2

import numpy as np
import matplotlib.pyplot as plt

plt.plot(np.dot(A, B), [0]*A.shape[0], "bo", label="Tennis")

plt.legend()
plt.show()

enter image description here

How to make matplotlib display the label name only once?

1
  • I can't recreate this issue. Are you sure you aren't calling plt.plot again on a different data set (or the same data set) with the same label? Commented Apr 5, 2014 at 8:43

1 Answer 1

2

Because you have 2 columns of data.

import numpy as np
import matplotlib.pyplot as plt

A=np.random.rand(50, 4)
B=np.random.rand(4, 2)
C=np.dot(A, B)
D=[0]*A.shape[0]
plt.plot(C, D, "bo", label="Tennis")

plt.legend()
plt.show()

C has now shape (50, 2) and D has shape (50,)

>>> C.shape
(50, 2)
>>> np.shape(D)
(50,)

The Documentation on plt.plot says:

If x and/or y is 2-dimensional, then the corresponding columns will be plotted.

I would recommend you to fix this by reducing the data to one column using C.flatten() and changing the dimension of D accordingly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.