Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Incorrect timestamps are shown on the x-axis

I have the following bar plot being generated with the following code:

import matplotlib as mplt
from matplotlib import dates, pyplot
from matplotlib.transforms import ScaledTranslation
import numpy as np
import pandas as pd

ts = pd.date_range('2023/01/01', '2023/01/06', freq='3H', tz='utc')
xs = np.arange(len(ts))
df = pd.DataFrame({'date':ts,'value':np.ones(shape=len(ts)), 'intensity':np.random.uniform(0, 10, len(ts))})

colors = []
for i in df.intensity:
    if 0 <= i < 6:
        colors.append('#75FF71')
    elif 6 <= i < 8:
        colors.append('#FFC53D')
    else:
        colors.append('#FF5C5C')

# pyplot.box
fig, ax = pyplot.subplots(figsize = (24,1), constrained_layout=False)
ax.yaxis.set_ticklabels(labels=[])
ax.yaxis.set_visible(False)
ax.grid(False)
ax.set_frame_on(False)

hour_locs = dates.HourLocator(byhour=[6, 12, 18])
hour_locs_fmt = dates.DateFormatter('%H:%M')
ax.xaxis.set_minor_locator(hour_locs)
ax.xaxis.set_minor_formatter(hour_locs_fmt)

day_locs = dates.DayLocator(interval=1)
day_locs_fmt = dates.DateFormatter('%B %-d')
ax.xaxis.set_major_locator(day_locs)
ax.xaxis.set_major_formatter(day_locs_fmt)
ax.xaxis.set_tick_params(which='major', pad=-10, length=40)
ax.bar(df.date, df.value, color=colors)

offset = ScaledTranslation(1.6, 0, fig.dpi_scale_trans)
for label in ax.xaxis.get_majorticklabels():
    label.set_transform(label.get_transform() + offset)

The output:
Output

The timestamps start from 2023/01/01 00:00:00+000 (UTC), however the plot shows that the data is starting at ~15:00 the day before. I’m assuming that matplotlib is ignoring the timezone in the data.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I did try specifying TZ in the locators and formatter in vain.

How do I get matplotlib to plot in UTC?

>Solution :

As you are plotting bars, it adds a little extra space on either ends. This is the reason you are seeing the extra space. At the end, add this line (ax.set_xlim(...)), forcing the plot to start and end at the times you want.

ax.set_xlim(df.date.min(), df.date.max())

You will get the below plot… hope this is what you are looking for…

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading