简体   繁体   English

Matplotlib偏移量errorbar bug解决方法?

[英]Matplotlib offset errorbar bug workaround?

The bug is documented here: 这里记录了错误:

Matplotlib errorbar not centered on marker Matplotlib错误栏不以标记为中心

and here: https://github.com/matplotlib/matplotlib/issues/3400 在这里: https//github.com/matplotlib/matplotlib/issues/3400

Basically, the markers are plotted off by 1 pixel all the time.. You can even see this on Matplotlib's own tutorial page if you look closely at the second plot: http://matplotlib.org/1.2.1/examples/pylab_examples/errorbar_demo.html 基本上,标记总是以1像素为单位绘制。如果仔细观察第二个图,您甚至可以在Matplotlib自己的教程页面上看到这个: http ://matplotlib.org/1.2.1/examples/pylab_examples/ errorbar_demo.html

This is very frustrating as I cannot produce publication-quality plots from matplotlib, and I'm very surprised this has not been fixed. 这非常令人沮丧,因为我无法从matplotlib生成出版品质的图,我很惊讶这还没有修复。

In any case, I have too much time and code invested into matplotlib to switch to a different package. 在任何情况下,我都有太多的时间和代码投入matplotlib来切换到不同的包。 So my question is how would you go about making a workaround? 所以我的问题是你将如何制定解决方法? I suppose one solution is to plot the markers 1 pixel to the left/right from the errorbars. 我想一种解决方案是将标记从错误栏左侧/右侧绘制1个像素。 I don't know how to do this. 我不知道该怎么做。 I figured out how to get the display coordinates of my plot points, but how can I make an interactive plot that preserves the 1-pixel offset? 我想出了如何获得我的绘图点的显示坐标,但是如何制作保留1像素偏移的交互式绘图? I can plot them with 1-pixel offsets, but then you can't zoom or manipulate the plot. 我可以使用1像素偏移绘制它们,但是您无法缩放或操纵绘图。

It seems like the Matplotlib team have fixed the issue when calling savefig() using .svg or .pdf, but for .png I've found that you can circumvent this issue by using an odd number for the error line thickness. 似乎Matplotlib团队在使用.svg或.pdf 调用savefig()时修复了这个问题 ,但对于.png我发现你可以通过使用奇数来表示错误行的厚度来解决这个问题。 Using the first example on the Matplotlib tutorial , if we use 如果我们使用,请使用Matplotlib教程中的第一个示例

plt.errorbar(x, y, yerr=0.4, marker='X', markersize=15)

then the bars are offset like this: 然后条形偏移如下:

在此输入图像描述

However if we use a line width of 3 但是,如果我们使用3的线宽

plt.errorbar(x, y, yerr=0.4, marker='X', markersize=15, elinewidth=3)

then the bars are centred like this: 那么酒吧就像这样居中:

在此输入图像描述

This isn't a perfect solution, but it does the job if you don't mind having slightly thicker lines. 这不是一个完美的解决方案,但如果您不介意稍微粗一点的线条,它就能完成这项工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM