Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with image sizes #1513

Closed
flamingbear opened this issue Nov 16, 2012 · 2 comments
Closed

Problems with image sizes #1513

flamingbear opened this issue Nov 16, 2012 · 2 comments

Comments

@flamingbear
Copy link

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()
@pelson
Copy link
Member

pelson commented Nov 19, 2012

Hi @flamingbear,

The best place to ask these general usage questions is either on the matplotlib-user mailinglist or StackOverflow (http://matplotlib.org/1.2.0/faq/troubleshooting_faq.html#getting-help) - it's easier for other users who have similar issues to find a solution to their problem rather than having to trawl through our (often fairly verbose 😄 ) development tickets. If you want to post to either of these sources I would be happy to copy over the following answer:


The bbox_inches='tight' option to savefig does some analysis on the artists visible on your plot and figures out the minimum bounding box needed to fit all of the artists on the plot. In doing so, it will reduce the size of the outputted image, rather than keeping the size and "zooming" in. A really simple example to show this (not using basemap):

>>> import matplotlib.pyplot as plt
>>> fig = plt.figure(figsize=(3, 4))
>>> ax = plt.axes([0.4, 0.4, 0.2, 0.2])
>>> ax.plot(range(10))
>>> plt.savefig('figsize_no_tight.png')
>>> plt.savefig('figsize_tight.png', bbox_inches='tight')

$> file figsize_*                            
figsize_no_tight.png: PNG image data, 300 x 400, 8-bit/color RGBA, non-interlaced
figsize_tight.png:    PNG image data, 98 x 123, 8-bit/color RGBA, non-interlaced

I hope that helps you. I'll close this issue, but if you do want further help/advice please let us know via the mailing list.

All the best,

Phil

@pelson pelson closed this as completed Nov 19, 2012
@flamingbear
Copy link
Author

Hi Phil,

That actually helps me see where I should post my question, but I don't believe your answer addresses my issue. What's in my code, doesn't use basemap at all, it's simply putting a rectangle and setting some ranges on it and the difference
in the ylim at the 17th decimal point causes the aspect ratio of the image to change drastically

I will repost my question somewhere.

Thanks
Matt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants