通用代码如下:
import math
import matplotlib.pyplot as plt
x = [x for x in range(10)]
y = [math.sin(x) for x in x]
Run Code Online (Sandbox Code Playgroud)
以下两个代码片段产生完全相同的图:
ax = plt.subplot()
ax.scatter(x,y,label='AAA')
ax.legend()
print('ax.get_legend_handles_labels() returns:\n',ax.get_legend_handles_labels())
plt.show()
Run Code Online (Sandbox Code Playgroud)
ax = plt.subplot()
ax.scatter(x,y)
ax.legend(labels=['AAA'])
print('ax.get_legend_handles_labels() returns:\n',ax.get_legend_handles_labels())
plt.show()
Run Code Online (Sandbox Code Playgroud)
第一个片段中的 print 语句返回
ax.get_legend_handles_labels() returns:
([<matplotlib.collections.PathCollection object at 0x7fc32cbdfac0>], ['AAA'])
Run Code Online (Sandbox Code Playgroud)
但第二个片段给出:
ax.get_legend_handles_labels() returns:
([], [])
Run Code Online (Sandbox Code Playgroud)
为什么get_legend_handles_labels()在第二种情况下返回空列表,但结果图是相同的?
| 归档时间: |
|
| 查看次数: |
1170 次 |
| 最近记录: |