Reputation: 446
I'm having some issues in using imshow()
from matplotlib, in specific with creating a pdf from it.
I'm dealing with a 500x500 matrix which, for the sake of this question, will be just random values:
np.random.seed(1)
arr = np.array(np.random.random((500, 500)))
The rows and columns are all labelled with different names, but for the sake of this question, let's just make them simple:
labels = ["Big_Label" if x % 2 == 0 else "Bigger_Big_Label" for x in range(500)]
So, I have the following code to plot that matrix:
plt.rc('figure', figsize=(5,5), dpi=500)
fig = plt.figure()
ax = fig.add_subplot(111)
im = ax.imshow(arr)
# defining the same labels for rows and columns
ax.set_xticklabels([''] + labels)
ax.set_yticklabels([''] + labels)
# showing the labels for all the ticks
ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
# personalising the ticks. In particular, labels on top
ax.tick_params(axis='both', which='both', labelsize=0.5, length=0)
ax.tick_params(axis='x',which='both', labelbottom='off', labeltop='on')
ax.tick_params(axis='both', pad=1)
# vertical labels
for label in im.axes.xaxis.get_ticklabels():
label.set_rotation(90)
plt.colorbar(im)
plt.title("Just a Big Title With Words")
# Removing outer lines because they hide part of the lines/columns
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
plt.savefig("fig.pdf")
plt.show()
The first issue is with the title because is on top of the xlabels:
The second issue is, when zooming to see the ylabels and xlabels, they are not aligned. With regards the left labels, they have varying spaces between them and the plot, when I specifically coded ax.tick_params(axis='both', pad=1)
; if I execute this python code in an IDE or in terminal, this issue doesn't happen (they are all close to the plot). So I guess something is going on when putting this image into a pdf?. With regards with both labels, you can see they are not aligned with the actual rows and columns; for example, the second label on the top is in middle of the blue and orange square, when it should be aligned with the middle of the orange square:
Finally, I tried to call fig.autofmt_xdate()
just before plt.savefig()
, but the result is even worse, as the top labels are just completely not aligned:
Can you help me solving this issues? I know you have to do a big zoom to see labels, but for the real matrices that I have that is necessary. I also inform that I'm using matplotlib 1.5; I can't use 2.x because of a compatibility issue with another tool
Upvotes: 2
Views: 3234
Reputation: 5913
There is a PR for automatically moving the title. Should be included in v2.2 (Feb timeframe) https://github.com/matplotlib/matplotlib/pull/9498, and another for the title padding https://github.com/matplotlib/matplotlib/pull/9816. So we are working on it.
As for your label misalignment in PDF, it looks like there is a bug in label alignment if labelsize
is less than 1.0, so don't do that: https://github.com/matplotlib/matplotlib/issues/9963 Its a bug, but I would imagine a low-priority one.
Upvotes: 1