gpt4 book ai didi

python - matplotlib 中的图例设置(numpoints 和 scatterpoints)不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 15:18:00 24 4
gpt4 key购买 nike

我试图让虚线的图例正确,所以我稍微玩了一下 rcParams,但由于某些原因它在我的电脑上不起作用。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib

matplotlib.rcParams['legend.numpoints'] = 5
matplotlib.rcParams['legend.scatterpoints'] = 5


fig, axs = plt.subplots()
axs.plot(range(10), '--k', label="line")
axs.plot(range(10), range(10)[::-1], ':k', label="scatter")
axs.legend(loc=9)

plt.show()

结果图是: Problematic legend

并且可以看出,虚线的数字点数是不够的。有人愿意帮忙吗?

谢谢!

最佳答案

如果您绘制带有标记的图,matplotlib.rcParams['legend.numpoints'] 调整在图例线上绘制的点数。

如果你用这些替换你的情节:

axs.plot(range(10), '--k', label="line", marker='d')
axs.plot(range(10), range(10)[::-1], ':k', label="scatter", marker='o')

你得到这张图片:enter image description here

我不知道 matplotlib.rcParams['legend.scatterpoints'] 有什么作用,但我想它调节了散点图图例中的点数。

如果您想更改图例中线条的长度,请尝试使用 matplotlib.rcParams['legend.handlelength'] 和/或 matplotlib.rcParams['legend. handleheight']。可以找到有关 rc 文件的更多信息 here

正如@tcaswell 所建议的,您不必设置 rc 参数。所有 legend.* 参数都可用作 legend 函数的关键字。参见 matplotlib.pyplot.legend documentation

关于python - matplotlib 中的图例设置(numpoints 和 scatterpoints)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19528579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com