3

I have a plot with two datasets, using the same x-axis data, so I want to plot this using the twinx functionality in python's matplotlib. To typeset the resulting graph in my latex document that the graph is used in, I save the created graph to a tex file using tikzplotlib. I'm working in Spyder 5.0.0, using python 3.8, which shows me the graph as well as it is produced by matplotlib.

The following minimal example reproduces the problem I get:

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

# crete some data
x = np.arange (0, 4, 0.01)
y1 = x**2
y2 = 2**x-5

# create plot
fig, ax1 = plt.subplots()

color = 'tab:red'

ax1.set_xticks([0.5,1.5,2.5,3.5])
ax1.set_xticklabels(['-0.5-', '-1.5-', '-2.5-', '-3.5-'])

ax1.plot(x, y1, color=color)
ax1.tick_params(axis='y', labelcolor=color)

ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis

color = 'tab:blue'
ax2.plot(x, y2, color=color)
ax2.tick_params(axis='y', labelcolor=color)


tikzplotlib.save("Tikz-twinx.tex", encoding='utf-8')

Then in my latex document I input the graph using:

\begin{figure}
    \input{Tikz-twinx.tex}
\end{figure}

The figure is produced correctly in python:

The figure is produced correctly in python

But in the pdf created with the latex code it looks like this:

enter image description here

Somehow both the automatic x-ticks and the manually added x-ticks show up. One way to remove them is by setting the same x-ticks for the second x-axis in the python code.

ax2.set_xticklabels(['-0.5-', '-1.5-', '-2.5-', '-3.5-'])

However, then the bottom axis becomes thicker as if it is drawn twice, which I would like to avoid. Has anyone encountered this problem and if yes: is there an easy workaround (that doesn't require manually adjusting the Tikz-twinx.tex file each time) to solve this issue?

(Also, there is an arrow at the top of the right y-axis that appears in the latex pdf, I haven't tried to remove that, yet, but if anyone happens to know what caused that I'd appreciate that, too.)

Edit 1: as requested the tex code for the plot that is generated by tikzplotlib (I've taken out most of the data to be plotted, because that just takes up space on here):

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

\definecolor{color0}{rgb}{0.83921568627451,0.152941176470588,0.156862745098039}
\definecolor{color1}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}

\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-0.1995, xmax=4.1895,
xtick style={color=black},
xtick={0.5,1.5,2.5,3.5},
xticklabels={-0.5-,-1.5-,-2.5-,-3.5-},
y grid style={white!69.0196078431373!black},
ymin=-0.796005, ymax=16.716105,
ytick style={color=black}
]
\addplot [semithick, color0]
table {%
0 0
%etc.: data for the plot. Taken out for here, so it doesn't take up too much space
3.99 15.9201
};
\end{axis}

\begin{axis}[
axis y line=right,
tick align=outside,
x grid style={white!69.0196078431373!black},
xmin=-0.1995, xmax=4.1895,
xtick pos=left,
xtick style={color=black},
y grid style={white!69.0196078431373!black},
ymin=-4.74447399634963, ymax=11.6339539233422,
ytick pos=right,
ytick style={color=black},
yticklabel style={anchor=west}
]
\addplot [semithick, color1]
table {%
0 -4
%etc.: data for the plot. Taken out for here, so it doesn't take up too much space
3.99 10.8894799269926
};
\end{axis}

\end{tikzpicture}

2
  • Welcome to TeX.SX. Conversion is not a bullet-proof method, there's always some loss in the process that is why it would be better if you can post as well your TikZ code of the plot: that would make easier to recognize what went wrong. Jun 16, 2021 at 11:17
  • Hi Claudio, thank you for your comment. I've added the code. As far as I can tell, there is a completely separate plot created in the tex file for the second line to be plotted, with no x ticks and tick labels specified, so the default x-ticks are added by latex.
    – Friki
    Jun 18, 2021 at 15:13

1 Answer 1

2

I suppose one option might be to add empty ticklabels to the second axis, at least the labels won't be printed twice:

ax2.set_xticklabels([' ']*4) 

For the arrow tip, try

tikzplotlib.save("Tikz-twinx.tex", encoding='utf-8', extra_axis_parameters=['y axis line style={-}']) 
3
  • Thanks for your anwer. The first things unfortunately doesn't work. I think in Python the x-axes are connected, so by adding that line all x-labels get removed and only the automatic or default tick labels show up in the latex pdf. However, the second thing you recommended seems to work thanks a lot for that!
    – Friki
    Jun 18, 2021 at 15:02
  • @Friki That doesn't happen here. After adding ax2.set_xticklabels([' ']*4) I get a .tex file with two axis environments, the first one having xticklabels={-0.5-,-1.5-,-2.5-,-3.5-}, and the second one having xticklabels={ , , , },. Jun 18, 2021 at 15:11
  • Sorry, you're completely right! I had accidentally commented out another line that created the ticks in the first place. Now that those are uncommented your trick solves the double label issue as well. Thank you!
    – Friki
    Jun 18, 2021 at 15:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .