tikzplotlib icon indicating copy to clipboard operation
tikzplotlib copied to clipboard

ticks are not exported correctly

Open IsabellLehmann opened this issue 4 years ago • 4 comments
trafficstars

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.

IsabellLehmann avatar Jul 08 '21 15:07 IsabellLehmann

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

IsabellLehmann avatar Sep 09 '21 13:09 IsabellLehmann

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$])

BMaveau avatar Oct 05 '21 09:10 BMaveau

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

gitfabianmeyer avatar Mar 30 '23 09:03 gitfabianmeyer

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

BMaveau avatar Mar 31 '23 11:03 BMaveau