问题
我担心你在引用的matplotlib git仓库中讨论的bugfix仅对plt.plot()有效,而对plt.scatter()无效
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(4,2))
ax = fig.add_subplot(121)
ax2 = fig.add_subplot(122, sharex=ax, sharey=ax)
ax.plot([1, 2],[0.4,0.4],color='black',marker=',',lw=0, linestyle="")
ax.set_title("ax.plot")
ax2.scatter([1,2],[0.4,0.4],color='black',marker=',',lw=0, s=1)
ax2.set_title("ax.scatter")
ax.set_xlim(0,8)
ax.set_ylim(0,1)
fig.tight_layout()
print fig.dpi #prints 80 in my case
fig.savefig('plot.png', dpi=fig.dpi)
解决方案:设置标记大小
解决方案是使用通常的“o”或“s”标记,但将markersize设置为恰好一个像素.由于标记大小以点给出,因此需要使用图形dpi来计算点中一个像素的大小.这是72./fig.dpi.
>对于aplot`,标记是直接的
ax.plot(..., marker="o", ms=72./fig.dpi)
>对于散点,markersize是通过s参数给出的,s参数是方形点,
ax.scatter(..., marker='o', s=(72./fig.dpi)**2)
完整的例子:
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(4,2))
ax = fig.add_subplot(121)
ax2 = fig.add_subplot(122, sharex=ax, sharey=ax)
ax.plot([1, 2],[0.4,0.4], marker='o',ms=72./fig.dpi, mew=0,
color='black', linestyle="", lw=0)
ax.set_title("ax.plot")
ax2.scatter([1,2],[0.4,0.4],color='black', marker='o', lw=0, s=(72./fig.dpi)**2)
ax2.set_title("ax.scatter")
ax.set_xlim(0,8)
ax.set_ylim(0,1)
fig.tight_layout()
fig.savefig('plot.png', dpi=fig.dpi)