FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Offset between plot and LineCollection by mdboom · Pull Request #2257 · matplotlib/matplotlib · GitHub

Repository navigation

Offset between plot and LineCollection - #2257

Merged
mdboom merged 1 commit into
matplotlib:v1.3.xfrom
mdboom:line_collection_offset
Jul 30, 2013
Merged

mdboom merged 1 commit into
matplotlib:v1.3.xfrom
mdboom:line_collection_offset

Conversation

mdboom commented Jul 30, 2013

Copy link
Copy Markdown
Member

There is a 1 pixel offset between the result of ax.plot and a LineCollection with the same values:

from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection

x1, x2, y1, y2 = -6.87194525904,-6.87194525904,0.0446252911239,0.0478759487178

fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(1,1,1)

lc = LineCollection([([[x1, y1], [x2, y2]])], color='green')
ax.add_collection(lc)        
ax.plot([x1, x2], [y1, y2], color='red')

fig.savefig('offset.png')

gives:

If you zoom in, you'll see:

I think there is a 1 pixel offset in the two lines.

@mdboom, I'm mentioning you here since in my opinion this is bad enough to be potentially considered for a fix that should be included in 1.3.0.

Copy link
Copy Markdown
Contributor Author

This is in the latest master as of 1184f78

mdboom commented Jul 29, 2013

Copy link
Copy Markdown
Member

Hmm... Can't reproduce.

Do you have anything customized in your matplotlibrc? Which backend is this?

mdboom commented Jul 29, 2013

Copy link
Copy Markdown
Member

I suspect you're on the MacOsx backend. First, try Agg instead. Second, try passing "snap=True" or "snap=False" to both of them. I suspect one or the other method is ignoring that flag.

Copy link
Copy Markdown
Contributor Author

I tried using the Agg backend (I guess it was using MacOS X) but that doesn't help. Setting snap=False in plot does help, though I don't quite get what you do. Setting snap in LineCollection has no effect.

Copy link
Copy Markdown
Contributor Author

Here's what I get with snap=False in plot:

Copy link
Copy Markdown
Contributor Author

Also, I made sure to remove my .matplotlib directory first to ensure no custom configuration.

mdboom commented Jul 30, 2013

Copy link
Copy Markdown
Member

Ok -- are you sure this is the Agg backend? What does

import matplotlib
matplotlib.get_backend()

give?

Copy link
Copy Markdown
Contributor Author

The full script is:

import matplotlib
matplotlib.use('Agg')
print(matplotlib.get_backend())

from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection

x1, x2, y1, y2 = -6.87194525904,-6.87194525904,0.0446252911239,0.0478759487178

fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(1,1,1)

lc = LineCollection([([[x1, y1], [x2, y2]])], color='green')
ax.add_collection(lc)        
ax.plot([x1, x2], [y1, y2], color='red')

fig.savefig('offset.png')

and the output is:

$ python bug.py 
agg

mdboom commented Jul 30, 2013

Copy link
Copy Markdown
Member

I was finally able to reproduce this -- I had forgotten I had a custom dpi set for some other testing. I think the attached resolves the (long standing) issue.

Copy link
Copy Markdown
Contributor Author

Works perfectly - thanks!!

mdboom added a commit that referenced this pull request Jul 30, 2013
mdboom merged commit b8fab17 into matplotlib:v1.3.x Jul 30, 2013

DPeterK commented Jan 8, 2014

Copy link
Copy Markdown

It would seem that this merge has had an impact on the behaviour of pcolor that becomes evident when a comparison between pcolor and pcolormesh is carried out. This can be seen in the image below, where 10 random values are plotted onto the same axes, using pcolor for the left column and pcolormesh for the right (and generated using the version of matplotlib as at the point where this commit was merged):

But setting snap=False on the pcolor call solves this:

However this problem does not show up in an earlier version of matplotlib (namely v1.2.1). Here the pcolor call has no snap keyword set yet the problem does not appear:

The code used to generate these three images is as below (but note that snap=False is added to the pcolor call for the second image):

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import numpy.random as rnd

MPL_VERSION = matplotlib.__version__

nx, ny = 1, 10
x0, x1 = [0, 1], [1, 2]
y = np.arange(ny+1)
X0, Y = np.meshgrid(x0, y)
X1, Y = np.meshgrid(x1, y)
rnd.seed(5)
z = rnd.randint(20, size=(ny, 1)).astype(np.float32)

fig = plt.figure(dpi=45)
pcr_ax = plt.subplot(111)
plt.pcolor(X0, Y, z)
plt.pcolormesh(X1, Y, z)
pcr_ax.grid(True)
pcr_ax.set_xlim([0, nx+1])
pcr_ax.set_ylim([0, ny])
plt.title('pcolor - pcolormesh (using MPL v{})'.format(MPL_VERSION))

plt.savefig('comparison_mpl_v{}_snap.png'.format(MPL_VERSION),
            dpi=45)
plt.show()

pelson commented Jan 8, 2014

Copy link
Copy Markdown
Member

@dkillick: Is your proposal that we should be setting snap=False as default to pcolor then?

DPeterK commented Jan 8, 2014

Copy link
Copy Markdown

@pelson the above was designed just to highlight the problem, not to also recommend a solution.

mdboom mentioned this pull request Jan 13, 2014

mdboom commented Jan 13, 2014

Copy link
Copy Markdown
Member

#2726 turns of snap in pcolor, and it does revert your example to the old behavior.

mdboom deleted the line_collection_offset branch August 7, 2014 13:50
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL