Sage: Ticket #7808: Plots seem to be shifted up and to the left by one pixel or so
https://trac.sagemath.org/ticket/7808
<p>
There was a thread on sage-devel where it seemed like a number of people felt plots (or axes!) were shifted by about one pixel or so. Here's one manifestation:
</p>
<pre class="wiki">region_plot([x^2+y^2<=1, x<=y], (x,-2,2), (y,-2,2),plot_points=400).show(aspect_ratio=1)
</pre><p>
There are a lot of other examples in a thread on one of the sage mailing lists, but I can't find the thread.
</p>
en-usSagehttps://trac.sagemath.org/chrome/site/logo_sagemath_trac.png
https://trac.sagemath.org/ticket/7808
Trac 1.1.6jasonFri, 11 Jun 2010 04:48:34 GMT
https://trac.sagemath.org/ticket/7808#comment:1
https://trac.sagemath.org/ticket/7808#comment:1
<p>
Here is another example which clearly shows something is wrong:
</p>
<pre class="wiki">bl = disk((0.0,0.0), 1, (pi, 3*pi/2), color='yellow')
tr = disk((0.0,0.0), 1, (0, pi/2), color='yellow')
tl = disk((0.0,0.0), 1, (pi/2, pi), color='black')
br = disk((0.0,0.0), 1, (3*pi/2, 2*pi), color='black')
P = tl+tr+bl+br
P.show(aspect_ratio=1,xmin=-2,xmax=2,ymin=-2,ymax=2)
</pre><p>
(from the disk doctests)
</p>
TicketjasonFri, 11 Jun 2010 04:55:13 GMT
https://trac.sagemath.org/ticket/7808#comment:2
https://trac.sagemath.org/ticket/7808#comment:2
<p>
Here's an even better illustration. Try changing the dpi:
</p>
<pre class="wiki">@interact
def show_shift(dpi=(50,150,1)):
# 50 dpi = shifted left
# 70 dpi = shifted up
# 80 dpi = seems perfect
# 100 dpi = shifted up and left
# 105 dpi = shifted up
# 110 dpi = seems perfect
bl = disk((0.0,0.0), 1, (pi, 3*pi/2), color='yellow')
tr = disk((0.0,0.0), 1, (0, pi/2), color='yellow')
tl = disk((0.0,0.0), 1, (pi/2, pi), color='black')
br = disk((0.0,0.0), 1, (3*pi/2, 2*pi), color='black')
P = tl+tr+bl+br
P.show(figsize=[10,10],dpi=dpi,aspect_ratio=1,xmin=-2,xmax=2,ymin=-2,ymax=2)
</pre>
TicketjasonFri, 11 Jun 2010 04:57:33 GMT
https://trac.sagemath.org/ticket/7808#comment:3
https://trac.sagemath.org/ticket/7808#comment:3
<p>
One more iteration, this time with the option to see the frame:
</p>
<pre class="wiki">@interact
def show_shift(dpi=(50,150,1),frame=False):
# 50 dpi = shifted left
# 70 dpi = shifted up
# 80 dpi = seems perfect
# 100 dpi = shifted up and left
# 105 dpi = shifted up
# 110 dpi = seems perfect
# different results if frame is checked
bl = disk((0.0,0.0), 1, (pi, 3*pi/2), color='yellow')
tr = disk((0.0,0.0), 1, (0, pi/2), color='yellow')
tl = disk((0.0,0.0), 1, (pi/2, pi), color='black')
br = disk((0.0,0.0), 1, (3*pi/2, 2*pi), color='black')
P = tl+tr+bl+br
P.show(figsize=[10,10],dpi=dpi,frame=frame,aspect_ratio=1,xmin=-2,xmax=2,ymin=-2,ymax=2)
</pre>
TicketjasonFri, 11 Jun 2010 05:45:51 GMT
https://trac.sagemath.org/ticket/7808#comment:4
https://trac.sagemath.org/ticket/7808#comment:4
<p>
Actually, the above examples might just be due to the line width (the line around the disk). Here is an example that again shows something really is wrong:
</p>
<pre class="wiki">p=line([[-1,0],[1,0]])+line([[0,-1],[0,1]],color='red')+line([[-1,-1],[1,1]],color='yellow')
p.show(figsize=20,dpi=300)
</pre><p>
Zooming in very closely shows that the red and blue lines are perfect, but the yellow line is shifted up or to the left or both (note the yellow pixels in quadrant 2, but not in quadrant 4).
</p>
TicketjasonFri, 11 Jun 2010 06:05:09 GMT
https://trac.sagemath.org/ticket/7808#comment:5
https://trac.sagemath.org/ticket/7808#comment:5
<p>
I posted a message to matplotlib-devel with what I think is a good example illustrating this in straight matplotlib: <a class="ext-link" href="http://permalink.gmane.org/gmane.comp.python.matplotlib.devel/8664"><span class="icon"></span>http://permalink.gmane.org/gmane.comp.python.matplotlib.devel/8664</a>
</p>
TicketjasonFri, 11 Jun 2010 18:51:57 GMTattachment set
https://trac.sagemath.org/ticket/7808
https://trac.sagemath.org/ticket/7808
<ul>
<li><strong>attachment</strong>
set to <em>trac-7808-axes-through-origin.patch</em>
</li>
</ul>
TicketjasonFri, 11 Jun 2010 18:54:37 GMTstatus changed
https://trac.sagemath.org/ticket/7808#comment:6
https://trac.sagemath.org/ticket/7808#comment:6
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>needs_info</em>
</li>
</ul>
<p>
I've attached a patch which is a workaround solving the problem. However, it causes other side effects (fuzzy lines, since now lines are not snapped to whole-pixels). You can uncomment the antialiased line in the patch to not get fuzzy lines, but then sometimes one axis will be one pixel wider than the other, which looks bad. I haven't tested how these options affect vector graphics output (e.g., pdf).
</p>
<p>
So we have three options:
</p>
<ol><li>Leave things the way they are
</li><li>Turn off snapping, but deal with possible fuzzy lines
</li><li>Turn off snapping and turn off antialiasing, and deal with axes that sometimes don't match in width.
</li></ol><p>
Pick your poison.
</p>
TicketjasonFri, 11 Jun 2010 18:54:50 GMTauthor set
https://trac.sagemath.org/ticket/7808#comment:7
https://trac.sagemath.org/ticket/7808#comment:7
<ul>
<li><strong>author</strong>
set to <em>Jason Grout</em>
</li>
</ul>
TicketjasonFri, 11 Jun 2010 21:05:33 GMT
https://trac.sagemath.org/ticket/7808#comment:8
https://trac.sagemath.org/ticket/7808#comment:8
<p>
Instead of applying the patch above, you can also do:
</p>
<ol><li>Install <a class="closed ticket" href="https://trac.sagemath.org/ticket/9208" title="defect: Add PKG_CONFIG_PATH to sage-env so programs like matplotlib link properly. (closed: fixed)">#9208</a> (to the scripts repository)
</li><li>If you've moved Sage from the build directory, then move it back to the original build location, delete SAGE_ROOT/local/lib/sage-current-location.txt, install <a class="closed ticket" href="https://trac.sagemath.org/ticket/9210" title="defect: pkg-config's prefix statements in SAGE_LOCAL/lib/pkgconfig not changed ... (closed: fixed)">#9210</a>, start up and close down Sage, (and then you can move the sage directory back to wherever you had it in step 1).
</li><li>Install the new matplotlib spkg at <a class="closed ticket" href="https://trac.sagemath.org/ticket/9202" title="enhancement: update matplotlib (closed: fixed)">#9202</a>
</li></ol><p>
Now just type this in a notebook cell:
</p>
<pre class="wiki">import matplotlib
matplotlib.rcParams['path.snap'] = False
</pre><p>
Then the plots will go right through the origin. This is equivalent (better, actually), than the patch above, as it makes *all* horizontal/vertical lines not snap to the nearest pixel.
</p>
<p>
So now the debate is if we should make path.snap by default False (it is True by default in matplotlib).
</p>
TicketjasonFri, 11 Jun 2010 21:42:41 GMT
https://trac.sagemath.org/ticket/7808#comment:9
https://trac.sagemath.org/ticket/7808#comment:9
<p>
The new matplotlib spkg with the path.snap parameter has been moved to <a class="closed ticket" href="https://trac.sagemath.org/ticket/9221" title="enhancement: update matplotlib to 1.0.0 and clean out the patches (closed: fixed)">#9221</a>
</p>
TicketkcrismanSat, 12 Jun 2010 02:25:37 GMT
https://trac.sagemath.org/ticket/7808#comment:10
https://trac.sagemath.org/ticket/7808#comment:10
<p>
I'm uncomfortable with the following comment in the patch
</p>
<pre class="wiki">(but drawing another
# horizontal/vertical line through the origin may not!)
</pre><p>
But would that still be the case for the mpl upgrade piece?
</p>
<p>
Otherwise it seems that 2 is the best option. Axes that don't match is a similarly annoying thing to the moved-over pixel.
</p>
TicketjasonSat, 12 Jun 2010 02:46:19 GMT
https://trac.sagemath.org/ticket/7808#comment:11
https://trac.sagemath.org/ticket/7808#comment:11
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/7808#comment:10" title="Comment 10">kcrisman</a>:
</p>
<blockquote class="citation">
<p>
I'm uncomfortable with the following comment in the patch
</p>
<pre class="wiki">(but drawing another
# horizontal/vertical line through the origin may not!)
</pre><p>
But would that still be the case for the mpl upgrade piece?
</p>
</blockquote>
<p>
Setting path.snap to False makes all horizontal/vertical lines do the .set_snap(False) (not just the axes). So using the rcParam as above would make all horizontal and vertical lines consistent.
</p>
<blockquote class="citation">
<p>
Otherwise it seems that 2 is the best option. Axes that don't match is a similarly annoying thing to the moved-over pixel.
</p>
</blockquote>
<p>
In that case, all we have to do is set the matplotlib path.snap config parameter somewhere (either in our code, or provide a default matplotlibrc, or modify the default matplotlibrc in the spkg.
</p>
<p>
I played a little with Mathematica, and it seems like they have both crisp horizontal lines *and* perfect axes. I'm not quite sure what they are doing, though.
</p>
TicketkcrismanSat, 12 Jun 2010 02:51:51 GMT
https://trac.sagemath.org/ticket/7808#comment:12
https://trac.sagemath.org/ticket/7808#comment:12
<p>
Well, anyway one would want to have pictures to vote on first :)
</p>
TicketkcrismanWed, 07 Jul 2010 14:14:58 GMT
https://trac.sagemath.org/ticket/7808#comment:13
https://trac.sagemath.org/ticket/7808#comment:13
<p>
Just another note - with respect to <a class="closed ticket" href="https://trac.sagemath.org/ticket/9221" title="enhancement: update matplotlib to 1.0.0 and clean out the patches (closed: fixed)">#9221</a> - mpl itself actually even shows this thing, if you look carefully at <a class="ext-link" href="http://matplotlib.sourceforge.net/plot_directive/mpl_examples/pylab_examples/spine_placement_demo_01.hires.png"><span class="icon"></span>this</a> picture from one of the latest updates.
</p>
TicketkcrismanTue, 09 Nov 2010 01:32:12 GMT
https://trac.sagemath.org/ticket/7808#comment:14
https://trac.sagemath.org/ticket/7808#comment:14
<p>
Is this now ready for review, with <a class="closed ticket" href="https://trac.sagemath.org/ticket/9221" title="enhancement: update matplotlib to 1.0.0 and clean out the patches (closed: fixed)">#9221</a> merged? Or do we still have to decide on one of 1-3?
</p>
TicketjasonTue, 09 Nov 2010 01:39:31 GMT
https://trac.sagemath.org/ticket/7808#comment:15
https://trac.sagemath.org/ticket/7808#comment:15
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/7808#comment:14" title="Comment 14">kcrisman</a>:
</p>
<blockquote class="citation">
<p>
Is this now ready for review, with <a class="closed ticket" href="https://trac.sagemath.org/ticket/9221" title="enhancement: update matplotlib to 1.0.0 and clean out the patches (closed: fixed)">#9221</a> merged? Or do we still have to decide on one of 1-3?
</p>
</blockquote>
<p>
I don't think the patch should be merged. Instead, a new patch that does this:
</p>
<pre class="wiki">import matplotlib
matplotlib.rcParams['path.snap'] = False
</pre><p>
somewhere should be posted. I think a vote should also be taken on sage-devel with before and after pics.
</p>
TicketkcrismanTue, 09 Nov 2010 01:41:02 GMT
https://trac.sagemath.org/ticket/7808#comment:16
https://trac.sagemath.org/ticket/7808#comment:16
<blockquote class="citation">
<p>
I don't think the patch should be merged. Instead, a new patch that does this:
</p>
<pre class="wiki">import matplotlib
matplotlib.rcParams['path.snap'] = False
</pre><p>
somewhere should be posted. I think a vote should also be taken on sage-devel with before and after pics.
</p>
</blockquote>
<p>
Thanks for volunteering :) Thankfully, I've not even been taking home a computer that can compile Sage in less than one day home lately.
</p>
<p>
The reason I thought of this is a recent ask.sagemath.org post.
</p>
TicketjdemeyerTue, 13 Aug 2013 15:35:53 GMTmilestone changed
https://trac.sagemath.org/ticket/7808#comment:17
https://trac.sagemath.org/ticket/7808#comment:17
<ul>
<li><strong>milestone</strong>
changed from <em>sage-5.11</em> to <em>sage-5.12</em>
</li>
</ul>
Ticketvbraun_spamThu, 30 Jan 2014 21:20:52 GMTmilestone changed
https://trac.sagemath.org/ticket/7808#comment:18
https://trac.sagemath.org/ticket/7808#comment:18
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.1</em> to <em>sage-6.2</em>
</li>
</ul>
Ticketvbraun_spamTue, 06 May 2014 15:20:58 GMTmilestone changed
https://trac.sagemath.org/ticket/7808#comment:19
https://trac.sagemath.org/ticket/7808#comment:19
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.2</em> to <em>sage-6.3</em>
</li>
</ul>
Ticketvbraun_spamSun, 10 Aug 2014 16:51:03 GMTmilestone changed
https://trac.sagemath.org/ticket/7808#comment:20
https://trac.sagemath.org/ticket/7808#comment:20
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.3</em> to <em>sage-6.4</em>
</li>
</ul>
Ticket