Some grey colors can be present in the picture due to antialiasing.
One has to distinguish between the antialiasing that is performed by matplotlib and the one that may be present in the image viewer (e.g. the browser).
Concerning matplotlib, there is the option to turn antialiasing off by setting antialiased = False
. Most artists and collections do have this option. So in this case
PatchCollection(...,antialiased=False)
would do the trick.
To observe the difference consider the following script. Setting antialiased
to True
(which is the default) outputs 121 different grey shades in the picture, while setting it to False
leaves us with only 2 (black and white).
antialiased=True
:

antialiased=False
:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from matplotlib.collections import PatchCollection
antialiased=False
N = 3
x = [1,2];y=[1,1]
radii = [0.5,.4]
patches = []
for x1, y1, r in zip(x, y, radii):
patches.append(Circle((x1, y1), r))
coll_intra = PatchCollection(patches, facecolors='black', edgecolors='black',antialiased=antialiased)
fig, ax = plt.subplots(figsize=(2,1))
ax.set_aspect("equal")
ax.axis("off")
ax.set_xlim([0,3])
ax.set_ylim([0,2])
ax.add_collection(coll_intra)
#count the number of different colors
#https://stackoverflow.com/questions/7821518/matplotlib-save-plot-to-numpy-array
#https://stackoverflow.com/questions/40433211/how-can-i-get-the-pixel-colors-in-matplotlib
fig.canvas.draw()
data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
data = data.reshape((int(len(data)/3), 3))
data = np.vstack({tuple(row) for row in data})
print len(data) # prints 121 for antialiased=True
# 2 for antialiased=False
plt.show()
(The method for counting the colors is adapted from this and this question.)