How to get matplotlib to place lines accurately?

2024/12/9 20:55:12

By default, matplotlib plot can place lines very inaccurately.

For example, see the placement of the left endpoint in the attached plot. There's at least a whole pixel of air that shouldn't be there. In fact I think the line center is 2 pixels off.

How to get matplotlib to draw accurately? I don't mind if there is some performance hit.

Inaccurately rendered line in matplotlib plot:

inaccurately rendered line in matplotlib plot

Inaccurately rendered line in matplotlib plot - detail magnified:

inaccurately rendered line in matplotlib plot - detail magnified

This was made with the default installations in Ubuntu 16.04 (Python 3), Jupyter notebook (similar result from command line).

Mathematica, for comparison, does subpixel-perfect rendering directly and by default: Same thing rendered by Mathematica Why can't we?

Answer

Consider the following to see what is going on

import matplotlib.pyplot as pltfig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 4], clip_on=False, lw=5, alpha=.5)
ax.set_xlim([1, 3])fig.savefig('so.png', dpi=400)

example

You can also disable pixel snapping by passing snap=False to plot, however once you get down to placing ~ single pixel wide line, you are going to have issues because the underlying rasterization is too coarse.

https://en.xdnf.cn/q/72928.html

Related Q&A

Using Flask as pass through proxy for file upload?

Its for app engines blobstore since its upload interface generates a temporary endpoint every time. Id like to take the comlexity out of frontend, Flask would take the post request and forward it to th…

What does printing an empty line do?

I know this question may well be the silliest question youve heard today, but to me it is a big question at this stage of my programming learning.Why is the second empty line needed in this Python code…

Django - how do I _not_ dispatch a signal?

I wrote some smart generic counters and managers for my models (to avoid select count queries etc.). Therefore I got some heavy logic going on for post_save. I would like to prevent handling the signa…

Python 3 Decoding Strings

I understand that this is likely a repeat question, but Im having trouble finding a solution.In short I have a string Id like to decode:raw = "\x94my quote\x94" string = decode(raw)expected f…

how get context react using django

i need get context in react using django but i cant do itthis is my code in my jsx <h1>{{titulo}}</h1> <h2>{{ejemplo}}</h2>in my template:{% load staticfiles %} <!DOCTYPE ht…

Copy certain files from one folder to another using python

I am trying to copy only certain files from one folder to another. The filenames are in a attribute table of a shapefile. I am successful upto writing the filenames into a .csv file and list the column…

I want to create django popup form in my project [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

SVG to PNG with custom fonts in Python

Im using Cairo/RSVG based solution for rasterizing SVG to PNG. Its already beeb described on StackOverflow in Convert SVG to PNG in Python. However, this solution doesnt seem to work with custom fonts.…

How to solve an equation with variables in a matrix in Python?

im coding in Pyhon, and Im working on stereo-correlation. I want to resolve this equation : m = K.T.Mm,K,M are know.where :M is the homogeneous coordinate of a point in the cartesian coordinate system…

how to create a new method with signature of another

How can I copy the signature of a method from one class, and create a "proxy method" with same signature in another ?.I am writing a RPC library in python. The server supports remote calls t…