Skip to content

Markers are not centered properly #11836

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

Closed
jason-s opened this issue Aug 9, 2018 · 12 comments
Closed

Markers are not centered properly #11836

jason-s opened this issue Aug 9, 2018 · 12 comments

Comments

@jason-s
Copy link

jason-s commented Aug 9, 2018

I am using IPython Notebook to view matplotlib graphs. I have noticed many times that the markers are drawn slightly off-center. Here is an example:

image

Closeup:

image

Code to reproduce:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

t = np.linspace(-1,1,40)
plt.plot(t,t**3 - 0.75*t,'.-',linewidth=0.3)
plt.grid(True)

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version: 2.2.2'
  • Matplotlib backend (print(matplotlib.get_backend())): module://ipykernel.pylab.backend_inline
  • Python version: 2.7.15 from Anaconda Python
  • Jupyter version (if applicable): 5.5.0
  • Other libraries: n/a
@jason-s
Copy link
Author

jason-s commented Aug 9, 2018

Either that, or the markers are fine but the lines are slightly offset from the intended points.

@afvincent
Copy link
Contributor

Possibly related to #7233?

@jason-s
Copy link
Author

jason-s commented Aug 9, 2018

oops, yeah I just found that and #8533

@jason-s
Copy link
Author

jason-s commented Aug 9, 2018

mark as duplicate?

@afvincent
Copy link
Contributor

afvincent commented Aug 9, 2018

No problem. Thank you for opening the ticket ;). I did not follow the latest evolution of those news, so I do not know if there were some improvements on that topic :/ (and pinging @anntzer who tried to tackle those questions IIRC ^^).

Marking as duplicate and closing as the issue seems similar to the ones reported in #7233, #7262, #7624, and #8533. Anyone please remove the flag and reopen the thread (or ask for it) if you disagree.

@ImportanceOfBeingErnest
Copy link
Member

I just want to note, while there certainly is the annoying issue of marker stamping present here, most of the mismatch from the example comes from the wrong linspace in use. If you want points spaced by 0.05, you need to use 41 points between -1 and 1, not 40.
So with t = np.linspace(-1,1,41) we get
image

which is indeed not optimal, but not as bad as it seems at first sight.

@jason-s
Copy link
Author

jason-s commented Aug 10, 2018

thanks but that doesn't concern me, it's only the fact that the markers are off-center compared to the lines joining them.

@anntzer
Copy link
Contributor

anntzer commented Aug 10, 2018

There is no one working on fixing this for the default Agg-based backends (AFAIK), although the issue is well understood. You may want to try the cairo based backends, which don't have this specific issue (not sure you can activate them with jupyter notebook? Perhaps @jklymak knows something here as I know he uses the notebook); alternatively, I wrote https://github.com/anntzer/mplcairo (another cairo based backend) in particular to fix the issue.

@ImportanceOfBeingErnest
Copy link
Member

You may use the Cairo backends in jupyter just as other backends, matplotlib.use("TkCairo"). The output will of course be a figure window invoked by plt.show(). There is no "inline"-cairo backend, but one may of course quickly tinker one together:

import matplotlib
matplotlib.use("TkCairo")

import numpy as np
import matplotlib.pyplot as plt

t = np.linspace(-1,1,41)
fig = plt.figure()
plt.plot(t,t**3 - 0.75*t,'.-',linewidth=0.3, markersize=3)
plt.grid(True)

def inline_show(fig=None):
    import io
    from IPython.display import Image, display
    fig = fig or plt.gcf()
    buff = io.BytesIO()
    fig.savefig(buff)
    buff.seek(0)
    display(Image(data=buff.getvalue()))
    plt.close(fig)
    
    
inline_show(fig)  

image

The same approach can also be used with the usual AGG backends. Because the stamping issue is more pronounced the smaller the dpi, one may save a high-dpi version of the figure and display it with lower width in the notebook.

%matplotlib inline  # "inline is Agg based
import numpy as np
import matplotlib.pyplot as plt

t = np.linspace(-1,1,41)
fig = plt.figure()
plt.plot(t,t**3 - 0.75*t,'.-',linewidth=0.3, markersize=3)
plt.grid(True)

def inline_show(fig=None):
    import io
    from IPython.display import Image, display
    fig = fig or plt.gcf()
    buff = io.BytesIO()
    fig.savefig(buff, dpi=400)
    buff.seek(0)
    display(Image(data=buff.getvalue(), width=480))
    plt.close(fig)
    
inline_show(fig)

image

@endolith
Copy link
Contributor

endolith commented Apr 9, 2024

Was this fixed? I'm not sure if I'm still seeing it or something else:

image

The dots seem to be below the line more often than not:

image

@tacaswell
Copy link
Member

Unfortunately we have not increased the number of marker stamps yet. The linked issues are still open.

@tacaswell
Copy link
Member

The issue can be ameliorated by increasing the DPI.

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

No branches or pull requests

6 participants