nschloe / tikzplotlib

:bar_chart: Save matplotlib figures as TikZ/PGFplots for smooth integration into LaTeX.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

imshow plot with uint8 data is exported as blank image

Aikhjarto opened this issue · comments

Tikzplotlib 0.9.2, matplotlib 3.2.2

If an imshow image is generated with unit8 data instead of float data, the tikzplotlib export contains a blank png.

import matplotlib.pyplot as plt
import tikzplotlib as tikz
import numpy as np

# not working (black png as result)
img = np.zeros((400, 400, 3)).astype(np.uint8)
img[100:200, 100:200, 0] = 255  # red
img[150:250, 150:250, 1] = 255  # green
img[100:200, 200:300, 2] = 255  # blue

# working
# img = np.zeros((400, 400, 3))
# img[100:200, 100:200, 0] = 1  # red
# img[150:250, 150:250, 1] = 1  # green
# img[100:200, 200:300, 2] = 1  # blue

fig = plt.figure()
plt.imshow(img)

tikz.save("test.tex", figure=fig, standalone=True, encoding='UTF-8')
fig.savefig('test.png')

Tikzplotlib output

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\DeclareUnicodeCharacter{2212}{−}
\usepgfplotslibrary{groupplots,dateplot}
\usetikzlibrary{patterns,shapes.arrows}
\pgfplotsset{compat=newest}
\begin{document}
% This file was created by tikzplotlib v0.9.2.
\begin{tikzpicture}

\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-0.5, xmax=399.5,
xtick style={color=black},
y dir=reverse,
y grid style={white!69.0196078431373!black},
ymin=-0.5, ymax=399.5,
ytick style={color=black}
]
\addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=399.5, ymin=399.5, ymax=-0.5] {test-000.png};
\end{axis}

\end{tikzpicture}

\end{document}

Tikzplotlib result:
test_latex

Matplotlib export:
test