How to conciliate dots annotation in Matplotlib scatter plot with manual limit setting?

前端 未结 2 1643
日久生厌
日久生厌 2020-12-12 03:06

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.

相关标签:
2条回答
  • 2020-12-12 03:09

    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.

    Two solutions:

    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)
      
    2. 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)

    0 讨论(0)
  • 2020-12-12 03:17

    I can't replicate the first issue (tried in versions 2.2.3, 3.1.1, 3.1.2) - I get this (using random data). Try upgrading your version of matplotlib or using

    plt.savefig('/path/to/output/image.png')
    

    To save the figure to the disk instead of showing it directly and see if the problem persists.

    I can however explain the error

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

    This occurs because plt.axis() returns [xmin, xmax, ymin, ymax], not an axes instance (fig, ax = plt.subplots(figsize=(20,10) returns an axes instance to ax).

    0 讨论(0)
提交回复
热议问题