0

This is a weird smearing effect I've never seen before in rendered matplotlib figures that looks like a printer error. I'm not clear on what might be causing it. I'm processing a sizeable dataframe (4.5gb) to render two copies of the same figure generated from each row, filenames are derived from the data which is why they're not simply copied and renamed after the process.

Here's a simpler version with np.random instead of the dataframe:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import time as time

data = np.random.random((20,6))
labels = ['a','b','c','d','e','f']

plt.figure(figsize=(25,15))
plt.rcParams.update({"font.size": "15"})
plt.matshow(data.T, fignum=1, aspect='auto')
plt.title('title', pad=15, wrap=True)
plt.yticks(np.arange(len(labels)), labels)

ax = plt.gca() 

# Set the X locator to index every 10 points (30 seconds) with no offset
ax.xaxis.set_major_locator(plt.IndexLocator(base=10, offset=0.))

# Multiply each tick by 3 and add 2 to get correct labels
ax.set_xticklabels([time.strftime('%M:%S', time.gmtime(item * 3 + 2)) for item in ax.get_xticks()]) 

ax.xaxis.set_ticks_position('bottom')
plt.xlabel('Time', labelpad=15)

divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="3%", pad=0.15)

plt.colorbar(cax=cax)

render = './path1/filename1.jpg'
plt.savefig(render)

track = './path2/filename2.jpg'
plt.savefig(render)

However I can't seem to reproduce the effect using the simplified version here. The script is actually running on a VM and using dask over an apply loop, but I don’t think that’s a factor

smeared colorbar ticklabels

lys
  • 949
  • 2
  • 9
  • 33
  • 4
    That looks like there are multiple labels being printed on top of each other. Are you sure you're clearing the figure correctly? – AKX Dec 11 '20 at 12:58
  • That's exactly what it looks like. Might just be as simple as forgetting to `plt.clf()` – lys Dec 11 '20 at 13:01
  • 2
    You could set `plt.matshow(..., vmin=0, vmax=1)` to make sure the colorbar always gets the ticks at the same position. – JohanC Dec 11 '20 at 13:04

1 Answers1

0

Clearing the figure at the end of the apply loop and setting the colorbar ticks with vmin and vmax solves the problem. See also this answer.

plt.matshow(..., vmin=0, vmax=1)

plt.clf()
plt.cla()
plt.close()
lys
  • 949
  • 2
  • 9
  • 33