问题
can someone explain why this simple code wont execute the facecolor command while setting the axis off?
fig = plt.figure(1)
ax = fig.add_subplot(211, facecolor=(0,0,0), aspect='equal')
ax.scatter(np.random.random(10000), np.random.random(10000), c="gray", s=0.25)
ax.axes.set_axis_off()
Thanks in advance!
回答1:
The background patch is part of the axes. So if the axes is turned off, so will the background patch.
Some options:
Re-add the background patch
ax = fig.add_subplot(211, facecolor=(0,0,0), aspect='equal')
ax.set_axis_off()
ax.add_artist(ax.patch)
ax.patch.set_zorder(-1)
Create new patch
ax = fig.add_subplot(211, facecolor=(0,0,0), aspect='equal')
ax.set_axis_off()
ax.add_patch(plt.Rectangle((0,0), 1, 1, facecolor=(0,0,0),
transform=ax.transAxes, zorder=-1))
Turn axis spines and ticks invisible
...but keep the axis on.
ax = fig.add_subplot(211, facecolor=(0,0,0), aspect='equal')
for spine in ax.spines.values():
spine.set_visible(False)
ax.tick_params(bottom=False, labelbottom=False,
left=False, labelleft=False)
来源:https://stackoverflow.com/questions/60805253/matplotlib-turning-axes-off-and-setting-facecolor-at-the-same-time-not-possible