tikzplotlib
tikzplotlib copied to clipboard
Separation between groupplots
Setting the separation between subplots in Python/Matplotlib is not respected by the generated pgfplots code.
Minimal example:
from matplotlib import pyplot as plt
from matplotlib2tikz import save as tikz_save
plt.subplot(2,2,1)
plt.plot([0, 10], [0, 10])
plt.setp(plt.gca().get_xticklabels(), visible=False) # make x tick labels invisible
plt.subplot(2,2,2)
plt.plot([0, 10], [0, 10])
plt.setp(plt.gca().get_xticklabels(), visible=False) # make x tick labels invisible
plt.setp(plt.gca().get_yticklabels(), visible=False) # make y tick labels invisible
plt.subplot(2,2,3)
plt.plot([0, 10], [0, 10])
plt.subplot(2,2,4)
plt.plot([0, 10], [0, 10])
plt.setp(plt.gca().get_yticklabels(), visible=False) # make y tick labels invisible
plt.subplots_adjust(hspace=.0) # remove vertical gap between subplots
plt.subplots_adjust(wspace=.0) # remove horizontal gap between subplots
#plt.savefig("matplotlib2tikz_groupplots_space.png", dpi=300)
tikz_save("matplotlib2tikz_groupplots_space.tikz")
plt.show()
Matplotlib shows the plot as follows:
The generated TiKZ plot looks like this:

The generated pgfplots code is:
% This file was created by matplotlib2tikz v0.6.13.
\begin{tikzpicture}
\definecolor{color0}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}
\begin{groupplot}[group style={group size=2 by 2}]
\nextgroupplot[
xmin=-0.5, xmax=10.5,
ymin=-0.5, ymax=10.5,
xtick={-2.5,0,2.5,5,7.5,10,12.5},
xticklabels={},
tick align=outside,
tick pos=left,
x grid style={lightgray!92.026143790849673!black},
y grid style={lightgray!92.026143790849673!black}
]
\addplot [semithick, color0, forget plot]
table {%
0 0
10 10
};
\nextgroupplot[
xmin=-0.5, xmax=10.5,
ymin=-0.5, ymax=10.5,
xtick={-2.5,0,2.5,5,7.5,10,12.5},
xticklabels={},
ytick={-2,0,2,4,6,8,10,12},
yticklabels={},
tick align=outside,
tick pos=left,
x grid style={lightgray!92.026143790849673!black},
y grid style={lightgray!92.026143790849673!black}
]
\addplot [semithick, color0, forget plot]
table {%
0 0
10 10
};
\nextgroupplot[
xmin=-0.5, xmax=10.5,
ymin=-0.5, ymax=10.5,
tick align=outside,
tick pos=left,
x grid style={lightgray!92.026143790849673!black},
y grid style={lightgray!92.026143790849673!black}
]
\addplot [semithick, color0, forget plot]
table {%
0 0
10 10
};
\nextgroupplot[
xmin=-0.5, xmax=10.5,
ymin=-0.5, ymax=10.5,
ytick={-2,0,2,4,6,8,10,12},
yticklabels={},
tick align=outside,
tick pos=left,
x grid style={lightgray!92.026143790849673!black},
y grid style={lightgray!92.026143790849673!black}
]
\addplot [semithick, color0, forget plot]
table {%
0 0
10 10
};
\end{groupplot}
\end{tikzpicture}
The pgfplots code to respect the vertical separation set via Python plt.subplots_adjust(hspace=.0) is to set the group style vertical sep to zero, and similarly plt.subplots_adjust(wspace=.0) should result in the group style option horizontal sep=0pt. Thus the correct line 6 of the generated code would be:
\begin{groupplot}[group style={group size=2 by 2,vertical sep=0pt,horizontal sep=0pt}].
"Workaround" would be to set e.g.
\pgfplotsset{/pgfplots/group/.cd,
horizontal sep=0.5cm,
vertical sep=0.5cm
}
Inside the fig env
As of #374 it's possible to set the sep options from python.