简体   繁体   中英

Matplotlib is plotting multiple labels

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()

在此输入图像描述

How to make matplotlib display the label name only once?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM