为什么stact.legend_elements的大小有限?

发布于 2025-02-05 02:44:55 字数 817 浏览 1 评论 0 原文

我正在尝试使用此代码绘制:

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个第一个元素)。 知道我如何获得完整的传奇?

此致

I'm trying to plot with this code :

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

But the legend is not showing every line (it is limited to the 9th first elements).
Any idea how can I get the full legend ?

Best regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

混吃等死 2025-02-12 02:44:55

来自 num 选项 scation.legend_elements()控制传说中应包含多少元素。默认情况下, num ='auto',它试图找到大量的元素要显示。

要显示所有元素,您可以使用 num =无,这应该为您提供映射中所有唯一元素的标签。

因此,使用以下代码:

plt.legend(handles=scatter.legend_elements(num=None)[0], labels=datasets_legend, title="datasets")

产生此数字:

”在此处“输入图像说明”

num 参数的文档,供参考:

num int,none,“ auto”(默认),类似数组或定位器

目标元素要创建的目标数。如果,请使用映射数组的所有唯一元素。如果是整数,则在规范范围内使用 num 元素的目标。如果“自动” ,请尝试确定哪种选项更好地适合数据的性质。由于定位器用于查找有用的位置,创建元素的数量可能会略微偏离 num 。如果列表或数组,请准确将这些元素用于传奇。最后,可以提供定位器

From the docs, you can see there is a num option to scatter.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:

plt.legend(handles=scatter.legend_elements(num=None)[0], labels=datasets_legend, title="datasets")

Which produces this figure:

enter image description here

Docs for the num parameter, for reference:

num int, None, "auto" (default), array-like, or Locator

Target number of elements to create. If None, use all unique elements of the mappable array. If an integer, target to use num elements in the normed range. If "auto", try to determine which option better suits the nature of the data. The number of created elements may slightly deviate from num due to a Locator being used to find useful locations. If a list or array, use exactly those elements for the legend. Finally, a Locator can be provided.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文