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

ticks are not exported correctly

IsabellLehmann opened this issue · comments

Hi,

I have created a plot where I want to have the ticks only at specific positions:

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

K = 16200
n = np.arange(0, K)
H = np.zeros_like(n)
H[3000] = 1
H[7000] = 1
fig, ax = plt.subplots(1,1)
ax.axhline(0,color='black', lw=1) # x = 0
ax.axvline(0,color='black', lw=1) # y = 0
ax.plot(n, H)
ax.set_xticks([3000, 7000])
ax.set_xticklabels([3000, 7000])
tikzplotlib.clean_figure()
tikzplotlib.save('1e.tex',figure=fig, axis_width='14cm', axis_height='7cm', standalone=True, encoding='utf-8')

The figure in Python looks the following:
image

However, the tick information is not exported:
image
Instead, there is a scientific notation which I did not want to have, and the ticks are distributed at the complete axis instead of showing only the two defined ticks.

This might be related to issue #355 , from where I took the idea of using ax.set_xticks instead of plt.xticks, but that did not help.

I used MikTeX and TeXStudio on a Windows PC.

I found the option strict but this unfortunately also keeps the Python font...

I had the same problem. The code checks if the value of the tick label is the same as the tick position. If this is true, it ignores the formatting of the label. To circumvent this problem, just put the values of the tick labels between dollar signs, i.e.
ax.set_xticklabels([$3000$, $7000$])

I had the same problem. The code checks if the value of the tick label is the same as the tick position. If this is true, it ignores the formatting of the label. To circumvent this problem, just put the values of the tick labels between dollar signs, i.e. ax.set_xticklabels([$3000$, $7000$])

Do you mean as strings or something? Your code is not working

Indeed, the correct code is
ax.set_xticklabels(['$3000$', '$7000$'])
Tikzplotlib thinks that it is a string while LaTeX considers it a number.