1

So I'm trying to convert a plot from python to Tikz with hour data in the x axis.

This is the image in python:

enter image description here

But when I use the tikz_save I got this error:

TypeError: a float is required

I'm looking and I suppose the error is for the type of data in x. How can i solve this?

Thanks in advance!

2

It is difficult to know exactly what's happening, but from what I can tell this will solve your problem

import matplotlib.pyplot as plt
import datetime
from matplotlib2tikz import save as tikz_save

# input data
t = ['10:00:00', '10:05:00', '10:07:00', '10:27:00']
P = [10, 20, 30, 40]

plt.style.use('ggplot')

fmt = '%H:%M:%S'
tseconds = [datetime.datetime.strptime(k, fmt) for k in t]
dt = [(k - tseconds[0]).seconds for k in tseconds]
dtmax = float(max(dt))
dt = np.array(dt) / dtmax

fig, ax = plt.subplots()
ax.plot(dt, P)
ax.set_xlabel('time')
ax.set_ylabel('Pressure')
fig.canvas.draw()

labels = plt.xticks()
labels = [datetime.timedelta(seconds = label * dtmax) + tseconds[0] for label in labels[0]]
tlabels = [label.strftime(fmt) for label in labels]
ax.set_xticklabels(tlabels)
plt.xticks(rotation = 45)

# export    
tikz_save('test.tex')

enter image description here

I know the x-label is off, I will leave that part for you to figure out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.