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

Python matplotlib中的图例设置(numpoints和scatterpoints)不起作用,python,matplotlib,Python,Matplotlib,我试着让虚线的图例正确,所以我玩了一点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(ra

我试着让虚线的图例正确,所以我玩了一点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()
结果是:

可以看出,虚线的numpoints是不够的。有人能帮忙吗


谢谢

如果使用标记进行打印,
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')
您可以看到以下图像:

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

如果要更改图例中的行长度,请尝试使用
matplotlib.rcParams['legend.handlelength']
和/或
matplotlib.rcParams['legend.handlelight']
。更多关于rc文件的信息可以找到


正如@tcaswell所建议的,您不必设置rc参数。所有的
legend.*
参数都可以作为
legend
功能的关键字使用。请参见
Line2D
对象和
散点
对象由图例代码进行不同的处理。
numpoints
设置为
Line2D
对象显示的标记数,
散点
为散点图设置相同的标记数。无需使用
rcparams
进行控制,只需将它们作为关键字参数传递给
图例
@tcaswell:感谢您确认
散点
。稍后我将编辑答案,添加您可以使用
legend
关键字的事实