matplotlib图例标记仅打开

2024-10-05 13:21:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我经常在matplotlib绘图上绘制一个点:

x = 10
y = 100
plot(x, y, "k*", label="Global Optimum")
legend()

但是,这会导致传说在传说中放置一颗星星两次,使其看起来像:

* * Global Optimum

当我真的想让它看起来像:

 *  Global Optimum

我该怎么做?


Tags: 绘图plotmatplotlib绘制globallabellegendoptimum
2条回答

这应该有效:

legend(numpoints=1)

顺便说一句,如果你加上

legend.numpoints     : 1      # the number of points in the legend line

对于matplotlibrc文件,这将是新的默认值。

[另请参见散布点,具体取决于情节。]

API:Link to API docs

我喜欢在每个python脚本中动态更改matplotlib rc参数。为了实现这个目标,我只需在python文件的开头使用类似的somthing。

from pylab import *
rcParams['legend.numpoints'] = 1

这将应用于从python文件生成的所有绘图。

编辑:对于那些不喜欢导入pylab的人,答案是

import matplotlib as mpl
mpl.rcParams['legend.numpoints'] = 1

相关问题 更多 >

    热门问题