Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Rectangle patch and colorbar are missing in tikz file #331

Open
albflo opened this issue Sep 11, 2019 · 2 comments
Open

Rectangle patch and colorbar are missing in tikz file #331

albflo opened this issue Sep 11, 2019 · 2 comments

Comments

@albflo
Copy link

albflo commented Sep 11, 2019

Hi,

currently I struggle to export a matplotlib (v3.1.0) plot with tikzplotlib (v0.8.2) to tikz. I made simple example of a plot with two rectangles with different colors and a colorbar. The resulting tikz plot doesn't show both rectangles and additional the colorbar and its ticks are missing.

python code

import matplotlib as mpl
import matplotlib.pyplot as plt
import tikzplotlib as tikz

plt.style.use('ggplot')
plt.rc('text', usetex=True)
plt.rc('font', family='serif')

fig = plt.figure()
axes = fig.add_subplot(111, aspect='equal')
axes.set_xlabel('$x$ axis')
axes.set_ylabel('$y$ axis')

c = [0, 1]
norm = mpl.colors.Normalize(vmin=c[0], vmax=c[-1])
cmap = mpl.cm.ScalarMappable(norm=norm, cmap=mpl.cm.viridis)
cmap.set_array([])

box0 = mpl.patches.Rectangle(xy=(0.1, 0.1), width=0.2, height=0.2,
                             color=cmap.to_rgba(0))
axes.add_patch(box0)

box1 = mpl.patches.Rectangle(xy=(0.6, 0.6), width=0.2, height=0.2,
                             color=cmap.to_rgba(1))
axes.add_patch(box1)

cbar = fig.colorbar(cmap, ticks=c)
cbar.set_label('$z$ axis')

tikz.save('test.tex')

plt.show()

output tex

here it's already visible that the two rectangles and colorbox are missing

% This file was created by tikzplotlib v0.8.2.
\begin{tikzpicture}

\definecolor{color0}{rgb}{0.267004,0.004874,0.329415}
\definecolor{color1}{rgb}{0.993248,0.906157,0.143936}

\begin{axis}[
axis background/.style={fill=white!89.80392156862746!black},
axis line style={white},
tick align=outside,
tick pos=left,
x grid style={white},
xlabel={\(\displaystyle x\) axis},
xmajorgrids,
xmin=0, xmax=1,
xtick style={color=white!33.33333333333333!black},
y grid style={white},
ylabel={\(\displaystyle y\) axis},
ymajorgrids,
ymin=0, ymax=1,
ytick style={color=white!33.33333333333333!black}
]
\end{axis}

\end{tikzpicture}

expected pgfplot

Figure_1

let me know if I can help somehow!

@eric-wieser
Copy link
Contributor

eric-wieser commented Jan 10, 2020

Caused by these lines:

# Objects with labels are plot objects (from bar charts, etc). Even those without
# labels explicitly set have a label of "_nolegend_". Everything else should be
# skipped because they likely correspong to axis/legend objects which are handled by
# PGFPlots
label = obj.get_label()
if label == "":
return data, []

@senselessDev
Copy link
Contributor

Because I also stumbled over missing rectangles, I had a quick look through the code, but basically the comment of @eric-wieser contains everything necessary for a quick hack. So until it is fixed elsewhere, a hacky workaround is possible by giving your rectangle the _nolegend_ label. So you would have to call

box0 = mpl.patches.Rectangle(xy=(0.1, 0.1), width=0.2, height=0.2,
                             color=cmap.to_rgba(0), label='_nolegend_')

It might cause some side effects but at least for me it was working and good enough to get the figures ready for my paper ;)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants