python 绘图添加图例显示UserWarning: Legend does not support []

import matplotlib.pyplot as plt
import numpy as np


np.random.seed(42)
x = np.random.randn(30)
y = np.random.randn(30)
plt.title("Example")
plt.xlabel("X")
plt.ylabel("Y")

X = plt.plot(x, "r--o")
Y, = plt.plot(y, "b-*")
plt.legend([X, Y], ["X", "Y"])

python 绘图添加图例显示UserWarning: Legend does not support []_第1张图片

没有了X轴但是有Y轴的的图例,原因如下:

 

解释是这样的: 
The comma is Python syntax that denotes either a single-element tuple. E.g.,

>>> tuple([1])
(1,)

In this case, it is used for argument unpacking: plot returns a single-element list, which is unpacked into line:


>>> x, y = [1, 2]
>>> x
1
>>> y
2
>>> z, = [3]
>>> z
3

An alternative, perhaps more readable way of doing this is to use list-like syntax:


>>> [z] = [4]
>>> z
4

though the z, 
= is more common in Python code.

逗号是用于解码。
意思好像是这个逗号用于参数解包。。。。大概就是这样。。。

你可能感兴趣的:(python)