如何通过手动限制设置来协调Matplotlib散点图中的点注释? [英] How to conciliate dots annotation in Matplotlib scatter plot with manual limit setting?

查看:183
本文介绍了如何通过手动限制设置来协调Matplotlib散点图中的点注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过手动限制设置来协调Matplotlib散点图中的点注释,但是我收到错误消息或遇到设计问题.

I'm trying to conciliate dots annotation in a Matplotlib scatter plot with a manual limit setting, but I either got an error message or I get a design problem.

这是我的代码:

fig, ax = plt.subplots(figsize = (20,10)) #manual limit setting
plt.axis([-2,3,-2.5,5])
plt.scatter(x, y)


for i, txt in enumerate(n):   #dot annotation   
    ax.annotate(txt, (x[i], y[i]))

这是输出的屏幕截图(我得到了最终的散点图,它是一个位于大白色矩形左上角的小矩形:

Here is a screen cap of the output (I got the final scatter plot as a small rectangle located in the left corner of a big white rectangle :

我也尝试过此操作:

 fig, ax = plt.subplots(figsize = (20,10))
    ax = plt.axis([-2,3,-2.5,5])
    plt.scatter(x, y)


for i, txt in enumerate(n):
    ax.annotate(txt, (x[i], y[i]))

但是我当然得到了以下错误消息(即使图表正确显示,但每个对应点旁边都没有标签).

But of course I got the following error message (even though the chart correctly displays, but without the labels next to each corresponding dot).

AttributeError: 'list' object has no attribute 'annotate'

出现错误是因为我的循环尝试遍历ax = plt.axis([-2,3,-2.5,5]),这确实没有道理.

The error arises because my loop tries to iterate through ax = plt.axis([-2,3,-2.5,5]), which doesn't make sense indeed.

是否有解决此问题的解决方案?

Any solution to overcome this issue ?

谢谢

推荐答案

之所以会出现此问题,是因为在剪切时使用了特殊的文本框.通常,您可能希望显示轴外的文本.因此,注释和文本具有annotation_clip参数.但是,这会在保存注释时与bbox_inches="tight"选项发生冲突,因为该注释仍被视为布局的一部分,因此该图形仍将坐标外的注释考虑在内.

The problem occurs because of the special casing of texts when it comes to clipping. Usually you might want text outside the axes to be shown. Therefore annotations and text have a annotation_clip argument. However, this interferes with the bbox_inches="tight" option when saving annotations, because the annotations is then still considered part of the layout and hence the figure takes annotations outside the axes still into account.

两种解决方案:

  1. 设置annotation_clip clip_on . IE.您可以明确地告诉注释将其剪切在轴上:

  1. Set annotation_clip and clip_on. I.e. You may explicitely tell the annotation to clip at the axes:

ax.annotate(txt, (x[i], y[i]), annotation_clip=True, clip_on=True)

  • bbox_inches设置为None .当使用IPython内联后端时,您可以告诉它不要通过以下方式扩展图形:

  • Set bbox_inches to None. When using the IPython inline backend you can tell it not to expand the figure via

    %config InlineBackend.print_figure_kwargs = {'bbox_inches':None}
    

    在开始创建内容之前,先在单元格中

    . (可以在此答案中看到)

    in a cell before starting to create your content. (This is seen in this answer)

    这篇关于如何通过手动限制设置来协调Matplotlib散点图中的点注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆