nick_eu
nick_eu

Reputation: 3723

matplotlib: remove horizontal gap between axes?

I can't seem to get the horizontal gap between subplots to disappear. Any suggestions?

Code:

plt.clf()
fig = plt.figure()
for i in range(6):
    ax = fig.add_subplot(3,2,i)

    frame_range = [[]]

    ax.set_xlim(-100000, 1300000)
    ax.set_ylim(8000000, 9100000) 
    ax.set_aspect(1)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.set_frame_on(False)

    ax.add_patch(dt.PolygonPatch(provs[0],fc = 'None', ec = 'black'))


fig.tight_layout(pad=0, w_pad=0, h_pad=0)
plt.subplots_adjust( wspace=0, hspace=0)

plt.savefig(wd + 'example.png')

Examples posted both for this code, and with ticks and frame left in.

Example

Example 2

Upvotes: 1

Views: 1519

Answers (1)

mrcl
mrcl

Reputation: 2180

You are setting two concurrent rules for your graphs.

One is axes aspect

ax.set_aspect(i)

This will force the plot to always respect the 1:1 proportions.

The other is setting h_space and w_space to be zero. In this case matplotlib will try to change the size of the axes to reduce the spaces to zero. As you set the aspect to be 1 whenever one of the edges touch each other the size of the axes will not change anymore. This produces the gap that keeps the graphs horizontally apart.

There two way to force them to be close to each other.

  • You can change the figure width to bring them closer to each other.
  • You can set a the spacing of the left and right edge to bring them closer to each other.

Using the example you gave, I modified few lines to illustrate what can be done with the left and right spacing.

fig = plt.figure()
for i in range(6):
    ax = fig.add_subplot(3,2,i)
    ax.plot(linspace(-1,1),sin(2*pi*linspace(-1,1)))
    draw()
    frame_range = [[]]

    ax.set_aspect(1)
    ax.set_xticks([])
    ax.set_yticks([])
#    ax.set_frame_on(False)
#    ax.add_patch(dt.PolygonPatch(provs[0],fc = 'None', ec = 'black'))


fig.tight_layout(pad=0,w_pad=0, h_pad=0)
subplots_adjust(left=0.25,right=0.75,wspace=0, hspace=0)

The result should be something like the figure bellow. It is important to keep in mind that if you resize the window the plots will be put apart again, depending if you make it taller/shorter of wider/narrower.

Hope it helps Grid distribution

Upvotes: 2

Related Questions