为什么stact.legend_elements的大小有限?
我正在尝试使用此代码绘制:
import pandas as pd
import random as rd
import matplotlib.pyplot as plt
datasets = ['a', 'a', 'b', 'b', 'b', 'c', 'd', 'd', 'e', 'f', 'g','g','g','h', 'i', 'j', 'k', 'k','l','m','n','o','p','q','r','s','t','u','v']
d = {'x': [rd.random()*len(i) for i in datasets]
, 'y': [rd.random()*len(i) for i in datasets]
, 'source': datasets}
df = pd.DataFrame(data=d)
fig = plt.figure()
categ = df.source.astype('category')
datasets_legend = dict(enumerate(categ.cat.categories)).values()
ax = fig.add_subplot(111)
scatter = ax.scatter(df.x, df.y, c=categ.cat.codes, cmap='Set3')
plt.title("test", fontsize=18)
plt.legend(handles=scatter.legend_elements()[0], labels=datasets_legend, title="datasets")
plt.show()
但是传说并未显示每一行(仅限于第9个第一个元素)。 知道我如何获得完整的传奇?
此致
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 num 选项
scation.legend_elements()
控制传说中应包含多少元素。默认情况下,num ='auto'
,它试图找到大量的元素要显示。要显示所有元素,您可以使用
num =无
,这应该为您提供映射中所有唯一元素的标签。因此,使用以下代码:
产生此数字:
num
参数的文档,供参考:From the docs, you can see there is a
num
option toscatter.legend_elements()
which controls how many elements should be included in the legend. By default,num='auto'
, which tries to find a nice number of elements to display.To show all elements, you can use
num=None
, which should give you labels for all the unique elements in the mappable.So, use this line of code:
Which produces this figure:
Docs for the
num
parameter, for reference: