以下代码绘制了两个红色矩形。红色矩形应该彼此相邻,两者之间没有空格。在python图中,这没关系。在导出的pdf中,矩形之间有一个薄而明显的空白区域。有没有办法解决这个问题?
import matplotlib.pyplot as plt
import matplotlib
import matplotlib.patches as patches
fig1 = plt.figure();
ax = fig1.add_subplot(111, aspect='equal');
ax.set_xticks([]);
ax.set_yticks([]);
#add first patch
dx=1.0;
loc=(0.0,0.0);
ax.add_patch(patches.Rectangle((loc[0]-dx/2.0,loc[1]-dx/2.0),dx,dx,facecolor='red',edgecolor='none',linewidth=0));
#add second patch
dx=1.0;
loc=(1.0,0.0);
ax.add_patch(patches.Rectangle((loc[0]-dx/2.0,loc[1]-dx/2.0),dx,dx,facecolor='red',edgecolor='none',linewidth=0));
ax.set_xlim([-1,3]);
ax.set_ylim([-1,2]);
fig1.show();
plt.savefig('spaceTest.pdf');
细线来自不精确的算术。您可以使用整数运算来计算矩形的点,但是您应该强制浮点运算。
例如:而不是
ax.add_patch(patches.Rectangle((float(loc[0]-dx/2),float(loc[1]-dx/2)),float(dx),float(dx),facecolor='red',edgecolor='none',linewidth=0));
使用
ax.add_patch(patches.Rectangle((loc[0]-dx/2.0,loc[1]-dx/2.0),float(dx),float(dx),facecolor='red',edgecolor='none',linewidth=0));
使用2.0
而不是2
给出0.5
而不是0
作为除法结果。
如果这没有帮助,你也应该转Antialiasing off:
ax.add_patch(patches.Rectangle((loc[0]-dx/2.0,loc[1]-dx/2.0),float(dx),float(dx),facecolor='red',edgecolor='none',linewidth=0, antialiased = False));