Question

I have four confusion matrices to plot in pyplot. Below is my code and resulting picture:

confmatmap=cm.YlOrBr    
fig = plt.figure()

plt.clf()
ax = fig.add_subplot(221)
ax.set_aspect(1)
res = ax.imshow(np.array(norm_conf_1), cmap=confmatmap, interpolation='nearest')
plt.xticks(range(2), ['T', 'F']); plt.yticks(range(2), ['T', 'F'])

ax = fig.add_subplot(222)
ax.set_aspect(1)
res = ax.imshow(np.array(norm_conf_2), cmap=confmatmap, interpolation='nearest')
plt.xticks(range(2), ['','']);plt.yticks(range(2), ['','']);
ax = fig.add_subplot(223)
ax.set_aspect(1)
res = ax.imshow(np.array(norm_conf_3), cmap=confmatmap, interpolation='nearest')
plt.xticks(range(2), ['','']);plt.yticks(range(2), ['','']);
ax = fig.add_subplot(224)
ax.set_aspect(1)
res = ax.imshow(np.array(norm_conf_4![enter image description here][2]), cmap=confmatmap, interpolation='nearest')
plt.xticks(range(2), ['','']);plt.yticks(range(2), ['','']);


fig.subplots_adjust(left=0, bottom=0, right=1, top=1,wspace=0, hspace=0.1)

enter image description here

But the gap between left subplots and right subplots are too big for me. I want both the vertical and horizontal gaps to be size of roughly a character. I can narrow the gap between upper and lower subplots by setting hspace to very small value, but the wspace does not work. I figure this is might because the subplots have some blank part which is white.

How can I narrow gaps to any appointed value here?

Was it helpful?

Solution

Try to do:

fig = plt.figure(figsize=(4, 4))

at the beginning and your code should work. The issue seems to be the figure size which is imcompatible with its content, making it not possible to respect both wspace and hspace.

OTHER TIPS

You are on the right way with fig.subplots_adjust(left=0, bottom=0, right=1, top=1).

By setting left to nonzero for instance you can move the left two subplots more inwards i.e. towards the two subplots on the right. Thus, decreasing the width between the subplots.

This will create extra white space on the left of your figure as the total size does not change. When saving the image you can remove this by including the option: bbox_inches = "tight"

Another thing you can use is:

fig.tight_layout()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top