I discovered some peculiarities in the libraries that export to pgf/tikz.
First, in matplotlib
, the quadmesh (i.e. the heatmap) is exported correctly to pgf, but the colorbar is exported as png (what the op encountered).
I tried the same thing with the matplotlib2tikz
library. in this case, the quadmesh is exported as a png, but the colormap can be exported correctly. However, If you suppress the heatmap, for example by plotting the colormap to a separate Axes
and clearing the heatmap axes, the colorbar is exported as png.
So if you combine the output of two exports, you can have them both.
The heatmap:
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
df = pd.DataFrame.from_csv("test.csv", sep=',')
sns.set('paper', 'white',
rc={'font.size': 10, 'axes.labelsize': 10,
'legend.fontsize': 8, 'axes.titlesize': 10,
'xtick.labelsize': 8,'ytick.labelsize': 8,
"pgf.rcfonts": False,
})
plt.rc('font', **{'family': 'serif', 'serif': ['Times']})
plt.rc('text', usetex=True)
fig, ax = plt.subplots(figsize=(6,6))
ax = sns.heatmap(df, linewidths=.5, ax=ax, cbar=False)
fig.savefig('heatmap_nocbar.pgf')
Note that I suppress the plotting of the colorbar with cbar=False
Then get the colorbar:
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from matplotlib2tikz import save as tikz_save
df = pd.DataFrame.from_csv("test.csv", sep=',')
sns.set('paper', 'white',
rc={'font.size': 10, 'axes.labelsize': 10,
'legend.fontsize': 8, 'axes.titlesize': 10,
'xtick.labelsize': 8,'ytick.labelsize': 8,
"pgf.rcfonts": False,
})
plt.rc('font', **{'family': 'serif', 'serif': ['Times']})
plt.rc('text', usetex=True)
fig, ax = plt.subplots()
ax = sns.heatmap(df, linewidths=.5, ax=ax, )
tikz_save('colormap.tex', figure=fig, strict=True)
The solution now requires some dirty work. I am aware that this is really not good practice, but this is the only way I could find.
In the colormap.tex
file, you need to delete all the lines except the ones needed for the colormap.
The beginnings of colormap.tex
:
\begin{tikzpicture}
\begin{axis}[
... % definition of axis: delete these lines
colorbar,
colormap={mymap}{[1pt]
...
So that you get this:
\begin{tikzpicture}
\begin{axis}[
hide axis,
colorbar,
colormap={mymap}{[1pt]
...
At the end of the file, delete everything between the closing of the square brackets and \end{axis}
:
]
... %delete these lines
\end{axis}
This change in the file can be automatised, I included an example in the code on github
In your main tex file, you can add these two figures
\input{heatmap_nocbar.pgf}
\input{colorbar.tex}
If you want a full working example and full outputs, I have put everything from this answer on github.
This is probably far from a perfect answer for you, but I lack the python skills to dive deeper in the backends of matplotlib
and matplotlib2tikz
to give a nicer example.