Skip to content

Problems with image sizes #1513

Closed
Closed
@flamingbear

Description

@flamingbear

Hi, I'm filing this here after submitting to basemap.

What I'm seeing is that under some circumstances, when I am trying to create an image with an exact shape/size specifications, I get unexpected behavior. I wouldn't expect that a small change in the limits of my data would cause this type of change.

I'm including code that creates 800x1399 images when I expect 800x1400 images. In the code are comments showing how to reproduce the bug.

Images:
Aspect for 800x1399 = 1.74875
Aspect for 800x1400 = 1.75

Data values:

Wrong size image:
27.999999999999994670 / 16.
aspect => 1.7499999999999996

Right sized image:
27.999999999999994671 / 16.
aspect => 1.7499999999999998

Cheers,
Matt

import matplotlib.pyplot as plt


def create_image():
    fig = plt.figure(1, figsize=(8, 14), frameon=False, dpi=100)
    fig.add_axes([0, 0, 1, 1])
    ax = plt.gca()

    # This isn't necessary to create the issue unless you want to see the
    # transparent pixels at bottom.

    # for spine in ax.spines.values():
    #     spine.set_linewidth(0.0)

    limb = ax.axesPatch
    limb.set_facecolor('#6587ad')

    x1 = 0.0
    y1 = 0.0
    x2 = 16.

    # Use this line and get what I was expecting:
    #    y2 = 27.999999999999994671   # produces 800 x 1400 image

    # Use this line and get the wrong size
    y2 = 27.999999999999994670   # produces (wrong?) 800 x 1399 image

    corners = ((x1, y1), (x2, y2))
    ax.update_datalim(corners)
    ax.set_xlim((x1, x2))
    ax.set_ylim((y1, y2))

    ax.set_aspect('equal', anchor='C')
    ax.set_xticks([])
    ax.set_yticks([])

    plt.savefig('rectangle.png', pad_inches=0.0, bbox_inches='tight')

    # If you use this below, the file size is correct, but there is a single
    # line transparent pixels along the bottom of the image if you set the
    # linewidth to zero...

    #  plt.savefig('rectangle.png', pad_inches=0.0)


if __name__ == '__main__':
    create_image()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions