Tom Kurushingal
Tom Kurushingal

Reputation: 6496

Why are there some unnecessary lines in matplotlib colorbars?

I am not sure if this is a local problem or happens to every one. But when I use colorbars in matplotlib and save them in PDF format I get certain unwanted lines in the colorbar as shown below:

enter image description here

A closer view:

enter image description here

enter image description here

The code I used is:

fig = plt.figure(figsize=(5.15, 5.15))
fig.clf()
plot = plt.subplot(111)
plt.specgram(data, NFFT = 256, Fs = sampling_rate)
plot.grid(False, which="major")
plot.set_xlabel(r'\textit{Time (s)}', labelpad=6)
plot.set_ylabel(r'\textit{{Frequency (Hz)}}', labelpad=6)
plt.colorbar()
fig.savefig('specgram.pdf', bbox_inches='tight')
plt.close()

Is there some way of removing this unnecessary lines?

Upvotes: 1

Views: 268

Answers (1)

McMath
McMath

Reputation: 7188

This is a known issue with some vector graphics viewers (i.e. not with matplotlib per se). Some viewers will render gaps between the segments of the gradient. The plt.colorbar documentation contains a simple workaround: set the edge colour for each segment to its face colour, as follows.

cbar = plt.colorbar()
cbar.solids.set_edgecolor("face")

Upvotes: 4

Related Questions