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

Colorbar breaks tex import #111

Closed
max3-2 opened this issue Sep 22, 2016 · 7 comments
Closed

Colorbar breaks tex import #111

max3-2 opened this issue Sep 22, 2016 · 7 comments

Comments

@max3-2
Copy link

max3-2 commented Sep 22, 2016

Having a weird issue with colorbars usage with pyplot.imshow.
Plottng imshow without colorbar exports perfect.
Adding colorbar leads to break due to

  • ! Extra }, or forgotten \endgroup.
    easily fixed by adding endgroup which seems to miss b4 end of tikzfigure
  • ! Package pgf Error: No shape named group c1r-1 is known. (line 283 m marked below)

still looking into it, any ideas yet?

% This file was created by matplotlib2tikz v0.5.11.
\begin{tikzpicture}

\begin{axis}[
title={A first view},
xmin=-0.5, xmax=639.5,
ymin=-0.5, ymax=511.5,
axis on top,
colorbar,
colormap={mymap}{[1pt]
  rgb(0pt)=(0.267004,0.004874,0.329415);
...long color def...
  rgb(255pt)=(0.993248,0.906157,0.143936)
},
point meta min=0,
point meta max=5,
colorbar style={ytick={0,0.625,1.25,1.875,2.5,3.125,3.75,4.375,5},yticklabels={0.0,0.6,1.2,1.9,2.5,3.1,3.8,4.4,5.0}}
]
\addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=639.5, ymin=-0.5, ymax=511.5] {img1.png};
\end{axis}

\nextgroupplot[
ylabel={sc. Ir},
xmin=0, xmax=1,
ymin=0, ymax=1,
axis on top,
axis y line=right,
ytick={0,0.125,0.25,0.375,0.5,0.625,0.75,0.875,1},
yticklabels={0.0,0.6,1.2,1.9,2.5,3.1,3.8,4.4,5.0}
>>>line 283:  ]
\addplot graphics [includegraphics cmd=\pgfimage,xmin=0, xmax=1, ymin=0, ymax=1] {imageTest_img001.png};
\path [draw=black, fill opacity=0] (axis cs:0,0)
--(axis cs:0,0.00390625)
--(axis cs:0,0.99609375)
--(axis cs:0,1)
--(axis cs:1,1)
--(axis cs:1,0.99609375)
--(axis cs:1,0.00390625)
--(axis cs:1,0)
--cycle;

\path [draw=white, fill=white] (axis cs:0,0)
--(axis cs:0,0.00390625)
--(axis cs:0,0.99609375)
--(axis cs:0,1)
--(axis cs:1,1)
--(axis cs:1,0.99609375)
--(axis cs:1,0.00390625)
--(axis cs:1,0)
--cycle;
>>>>inserting \endgroup as a quick fix for error one<<<<<
\end{tikzpicture}
@nschloe
Copy link
Owner

nschloe commented Sep 24, 2016

Could you post a minimal Python example that generates the failing TeX code?

@max3-2
Copy link
Author

max3-2 commented Sep 24, 2016

Will do on Monday. But it's just generating an pyplot.imshow and adding a colorbar.

Found out that replacing axis environment by groupplot around all plots and then initializing each axis by \groupplot soes the trick but changing code to do so then subsequently breaks with 2 or more subplots due to then chaotic groupplot environment setup.

@max3-2
Copy link
Author

max3-2 commented Sep 25, 2016

Ok here's the detailed follow-up. Code below, errors are as above, with same ideas for fixing...

Python goes as already explained as simple as possible

import numpy as np
import matplotlib.pyplot as plt
import matplotlib2tikz

da = np.random.random((512,640))

fig = plt.figure()
ax = plt.gca()

im = ax.imshow(da, cmap='viridis')
cax = plt.colorbar(im, aspect=10, shrink=0.8)

plt.title('A new figure')
matplotlib2tikz.save('aTest.tex')

produces following tex

% This file was created by matplotlib2tikz v0.5.11.
\begin{tikzpicture}

\begin{axis}[
title={A new figure},
xmin=-0.5, xmax=639.5,
ymin=-0.5, ymax=511.5,
axis on top,
colorbar,
colormap={mymap}{[1pt]
  rgb(0pt)=(0.267004,0.004874,0.329415);
%%%...a lot of colors...
  rgb(255pt)=(0.993248,0.906157,0.143936)
},
point meta min=7.83885246313787e-06,
point meta max=0.999999429985691,
colorbar style={ytick={0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9},yticklabels={0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9}}
]
\path [draw=black, fill opacity=0] (axis cs:0,511.5)
--(axis cs:0,-0.5);

\path [draw=black, fill opacity=0] (axis cs:1,511.5)
--(axis cs:1,-0.5);

\path [draw=black, fill opacity=0] (axis cs:-0.5,1)
--(axis cs:639.5,1);

\path [draw=black, fill opacity=0] (axis cs:-0.5,0)
--(axis cs:639.5,0);

\addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=639.5, ymin=511.5, ymax=-0.5] {img1.png};
\end{axis}

\nextgroupplot[
xmin=0, xmax=1,
ymin=0, ymax=1,
axis on top,
axis y line=right,
ytick={0.0999930019753686,0.199993842869117,0.299994683762865,0.399995524656613,0.499996365550361,0.599997206444109,0.699998047337857,0.799998888231605,0.899999729125354},
yticklabels={0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9}
]
\addplot graphics [includegraphics cmd=\pgfimage,xmin=0, xmax=1, ymin=0, ymax=1] {aTest_img001.png};
\path [draw=black, fill opacity=0] (axis cs:0,0)
--(axis cs:0,0.00390625)
--(axis cs:0,0.99609375)
--(axis cs:0,1)
--(axis cs:1,1)
--(axis cs:1,0.99609375)
--(axis cs:1,0.00390625)
--(axis cs:1,0)
--cycle;

\path [draw=white, fill=white] (axis cs:0,0)
--(axis cs:0,0.00390625)
--(axis cs:0,0.99609375)
--(axis cs:0,1)
--(axis cs:1,1)
--(axis cs:1,0.99609375)
--(axis cs:1,0.00390625)
--(axis cs:1,0)
--cycle;

\end{tikzpicture}

Tex has in the preamble:

\usepackage{tikz,pgfplots}
\usetikzlibrary[pgfplots.groupplots]
\pgfplotsset{compat=1.12}

and the line producing the error is just

\include{aTest}

@nschloe
Copy link
Owner

nschloe commented Sep 26, 2016

Fix released in 0.5.12.

The reason for this bug is the fact that it's hard to determine if an axis is a colorbar or not. The heuristic takes the ratio of x- and y-axis and checks for the presence of ticks. You used a custom aspect=10 which broke the heuristic. I've adapted it, but you can still break it now if you use, e.g., aspect=1.0 or so.

@nschloe nschloe closed this as completed Sep 26, 2016
@max3-2
Copy link
Author

max3-2 commented Sep 27, 2016

Sorry to bother you again but this consequently leads to failing with 2 subplots and a horizontal bar. Probably because aspect gets inverted? Open a new issue?

I'll attach examples here..

Simple python

import numpy as np
import matplotlib.pyplot as plt
import matplotlib2tikz

data = np.random.random((512,640))

fig = plt.figure()

ax1 = plt.subplot(121)
ax2 = plt.subplot(122)
ax = [ax1,ax2]

for i,axes in enumerate(ax):
    im = axes.imshow(data)

cax = fig.colorbar(im, ax=ax, orientation='horizontal')

matplotlib2tikz.save('2plots.tex')

Produces tex with error “..nexted axes not allowed…”

% This file was created by matplotlib2tikz v0.5.12.

\begin{tikzpicture}
\begin{groupplot}[group style={group size=2 by 1}]
\nextgroupplot[
xmin=-0.5, xmax=639.5,
ymin=-0.5, ymax=511.5,
axis on top
]
\addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=639.5, ymin=511.5, ymax=-0.5] {img3.png};

\begin{axis}[
xmin=-0.5, xmax=639.5,
ymin=-0.5, ymax=511.5,
axis on top,
colorbar horizontal,
colormap={mymap}{[1pt]
  rgb(0pt)=(0,0,0.5);
  rgb(22pt)=(0,0,1);
  rgb(25pt)=(0,0,1);
  rgb(68pt)=(0,0.86,1);
  rgb(70pt)=(0,0.9,0.967741935483871);
  rgb(75pt)=(0.0806451612903226,1,0.887096774193548);
  rgb(128pt)=(0.935483870967742,1,0.0322580645161291);
  rgb(130pt)=(0.967741935483871,0.962962962962963,0);
  rgb(132pt)=(1,0.925925925925926,0);
  rgb(178pt)=(1,0.0740740740740741,0);
  rgb(182pt)=(0.909090909090909,0,0);
  rgb(200pt)=(0.5,0,0)
},
point meta min=5.50205141325311e-06,
point meta max=0.99999961523477,
colorbar style={xtick={0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9},xticklabels={0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9}}
]
\addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=639.5, ymin=511.5, ymax=-0.5] {img4.png};

\end{axis}

\end{tikzpicture}

Cheers
Max

@nschloe
Copy link
Owner

nschloe commented Sep 27, 2016

Open a new issue?

Yes.

@theRealSuperMario
Copy link
Contributor

Any progress on this one?

I am also struggeling to get the horizontal colorbar to work

# 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