Skip to content

Non-accurate placing of images from plt.imshow in PDF output #2935

Closed

Description

@mojca

I have some problems with non-accurate placing of images created by imshow. Output to PNG works fine, but the squares are rectangular and misaligned with the grid when plotting small squares to PDF (order of magnitude around 1-2 inches for the whole plot). I didn't inspect the contents of PDF, but I suspect that coordinates might be written with too low precision.

Here's a small example (maybe not the minimal possible, but hopefully short enough):

import numpy as np
import matplotlib.pyplot as plt

plt.gcf().set_size_inches([1,1])
a=np.random.rand(20,20)
im = plt.imshow(a, interpolation='nearest', origin='lower', extent=[-2,2,-2,2])
plt.gca().set_xticks(np.arange(-2,2,0.2))
plt.gca().set_yticks(np.arange(-2,2,0.2))
plt.gca().xaxis.set_ticklabels([])
plt.gca().yaxis.set_ticklabels([])
plt.grid(ls=':',lw=0.1,color='white')
plt.savefig('test.pdf')

x

I'm using python 3.3 and matplotlib 1.3.1.

Activity

  1. tacaswell commented on Mar 28, 2014

    @tacaswell
    Member

    I strongly suspect that this is the same underlying issue as #2831 and a duplicate of #1441.

  2. WeatherGod commented on Mar 28, 2014

    @WeatherGod
    Member

    May I say that I think this is a fantastic test, and when we nail down the
    fix, that this code gets added as a test.

  3. added this to the v1.4.x milestone on Mar 28, 2014
  4. mojca commented on Mar 28, 2014

    @mojca
    Author

    I don't know enough to be able to judge if the issues cited above are the same, but they certainly seem related or equal.
    I'm glad if you find the test useful, but if taken to the "test suite", I would suggest polishing it a bit (remove border and ticks, avoid hardcoding numbers at multiple places, use a well defined 2D function instead of a random number generator, ...)

  5. modified the milestones: v1.4.x, on Feb 7, 2015
  6. modified the milestones: , on Jul 17, 2015
  7. jklymak commented on Sep 25, 2017

    @jklymak
    Member

    I think this is fixed in master by #9204 (or maybe other fixes since then). There is still a 1-pixel offset, due to the rasterizing, but I think this is pretty accurate. I'll take the liberty of closing, but feel free to re-open.

    banners_and_alerts_and_testimage_pdf__1_page_

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions