python - Matplotlib 线重叠/分辨率

标签 python matplotlib linestyle

我正在 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')

enter image description here 我希望蓝线和绿线完全不重叠,因为 10000000 < 10000001。

我把蓝线做得稍微粗一些,所以如果你放大,你可以看到蓝线和绿线是如何重叠的。我把dpi调的很高,所以不是分辨率问题。我将不得不处理大量数据,因为我正在处理基因组数据——大量数据是一个问题吗?当我使用 (0 to 10000)(10001, 20000) 的 x 值进行绘图时,问题仍然存在。

非常感谢您的帮助。

最佳答案

Line2D 对象的 cap 样式有关,默认样式为 'projecting',这会导致重叠,请查看放大的 PDF: enter image description here

我们想把它改成'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')

enter image description here

当然,差距非常小,因为它是 1/10000000。

或者,如果您愿意,一个快速的肮脏解决方案是在 (10000000.5, 3) 处绘制一个 size=1 的白色小圆圈标记。


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()

enter image description here

关于python - Matplotlib 线重叠/分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24085489/

相关文章:

python - 无法使用 matplotlib 设置脊线样式

actionscript-3 - AS3 更改现有行的 lineStyle

python - 无法在 Django Model 中调用 save() ?获取: AttributeError: 'unicode' object has no attribute 'save'

python - Plotly:如何使用 px.line 更改线条样式?

python - pandas - 在 groupby DataFrame 之后只保留 True 值

python - 无法向饼图添加标题(matplotlib)

matplotlib - Julia : type PyObject has no field set_yscale 中的 PyPlot 错误

matplotlib - 如何在 Matplotlib 中设置轴从角开始

python - Tensorflow - 准确度从 1.0 开始,随着损失而降低

python - 通过 Tensorflow 消费大数据