我正在 Matplotlib 中绘制一个图,当不同的线具有不同的 x 值时,它们必须不重叠。出于某种原因,当我画线时,如果它们靠得足够近(但不重叠),就会出现重叠。例如,
fig = plt.figure(num=None, facecolor='w', edgecolor='k')
ax = fig.add_subplot(2, 1, 1)
ax.plot((0, 10000000), (3, 3), linewidth = 2, markersize = 0, clip_on = True, aa = True)
ax.plot((10000001, 200000001), (3, 3), linewidth = 1, markersize = 0, clip_on = True, aa = True)
plt.savefig('test.png', format='png')
我希望蓝线和绿线完全不重叠,因为 10000000 < 10000001。
我把蓝线做得稍微粗一些,所以如果你放大,你可以看到蓝线和绿线是如何重叠的。我把dpi调的很高,所以不是分辨率问题。我将不得不处理大量数据,因为我正在处理基因组数据——大量数据是一个问题吗?当我使用 (0 to 10000)
和 (10001, 20000)
的 x 值进行绘图时,问题仍然存在。
非常感谢您的帮助。
最佳答案
与 Line2D
对象的 cap 样式有关,默认样式为 'projecting',这会导致重叠,请查看放大的 PDF:
我们想把它改成'butt'风格:
L1=ax.plot((0, 10000000), (3, 3), linewidth = 2, markersize = 0, clip_on = True, aa = True)
L2=ax.plot((10000001, 200000001), (3, 3), linewidth = 1, markersize = 0, clip_on = True, aa = True)
for item in L1+L2:
item.set_solid_capstyle('butt')
当然,差距非常小,因为它是 1/10000000。
或者,如果您愿意,一个快速的肮脏解决方案是在 (10000000.5, 3)
处绘制一个 size=1 的白色小圆圈标记。
-
matplotlib.pyplot.plot
可以使用matplotlib.lines.Line2D
中的kwargs
,其中包括参数solid_capstyle
- 这是在绘图调用中设置
solid_capstyle
的附加示例
fig, ax = plt.subplots(1, 1, figsize=(8, 4))
L1 = ax.plot((0, 1), (1, 1), linewidth=10, label='No capstyle')
L2 = ax.plot((1, 2), (1, 1), linewidth=7, label='No capstyle')
L3 = ax.plot((0, 1), (0.9, 0.9), linewidth=10, solid_capstyle='butt', label='With butt capstyle')
L4 = ax.plot((1, 2), (0.9, 0.9), linewidth=7, solid_capstyle='butt', label='With butt capstyle')
ax.set_ylim(0, 2)
ax.grid()
ax.legend()
关于python - Matplotlib 线重叠/分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24085489/