Matplotlib 图例:如何分配多个散点值
我使用 python 中的 matplotlib 库来生成出版质量的 xy 散点图。我遇到了有关图例中标记的问题。我正在绘制 2 个不同的 xy 散点图系列;一个是形成曲线的一组 xy 点,另一个是单个 xy 点。
我希望图例显示 3 个“曲线”标记和 1 个单点标记。我知道如何更改图例标记数量的唯一方法是在声明图例时使用“scatterpoints”参数。但是,此参数设置图例中所有系列的标记数量,并且我不确定如何单独更改每个图例条目。
遗憾的是,我无法作为新用户发布图片,但希望这个描述足够了。 有没有办法使用 matplotlib 为每个图例条目单独设置散点值?
编辑:以下是显示具有不同散点值的图像的链接。
散点 = 3: https://i.sstatic.net/KK4Kn.jpg
散点 = 1: https://i.sstatic.net/DAp2G.jpg
希望这能让问题稍微解决一点更清楚。
I'm using the matplotlib library in python to generate publication-quality xy scatter plots. I ran into a problem regarding the markers in the legend. I'm plotting 2 different xy-scatter series; one is a set of xy points that forms a curve, and the other is a single xy point.
I would like the legend to show 3 markers for the "curve", and 1 marker for the single point. The only way I know how to change the number of legend markers is using the "scatterpoints" argument when declaring the legend. However, this argument sets the number of markers for all series in the legend, and I'm not sure how to change each legend entry individually.
Sadly I can't post pictures as a new user, but hopefully this description is sufficient. Is there a way to set scatterpoints values individually for each legend entry using matplotlib?
EDIT: Here are links showing images with different values for scatterpoints.
scatterpoints = 3: https://i.sstatic.net/KK4Kn.jpg
scatterpoints = 1: https://i.sstatic.net/DAp2G.jpg
Hopefully this makes the issue a bit more clear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以获取图例中的线条,并自行更改:
Legend.legendHandles 是图例中所有线条的列表,线条的 _legmarker 属性是标记。
您可以调用 set_markevery(3) 或 set_xdata() & set_ydata() 更改标记数量。
you can get the line in legend, and change it yourself:
Legend.legendHandles is a list of all the lines in legend, and the _legmarker attribute of the line is the marks.
You can call set_markevery(3) or set_xdata() & set_ydata() to change the number of marks.