Pandas / matplotlib chart with date axis shows the correct day / month but the wrong working day / year

I load CSV data using pandas, where one of the columns takes the form of a date in the format "% a% d.% M.% Y" (for example, "Mon 06.02.2017"), and then try to make some graphs where the X axis marked according to date.

Something goes wrong during plotting because the date stamps are incorrect; for example, what was "Mon 06.02.2017" in the CSV / DataFrame is displayed on the chart as "Thu 06.02.0048".

Here is the MWE. This is the file 'data.csv':

Mon 06.02.2017  ;  1  ;  2  ;  3
Tue 07.02.2017  ;  4  ;  5  ;  6
Wed 08.02.2017  ;  7  ;  8  ;  9
Thu 09.02.2017  ; 10  ; 11  ; 12
Fri 10.02.2017  ; 13  ; 14  ; 15
Sat 11.02.2017  ; 16  ; 17  ; 18
Sun 12.02.2017  ; 19  ; 20  ; 21
Mon 13.02.2017  ; 22  ; 23  ; 24
Tue 14.02.2017  ; 25  ; 26  ; 27
Wed 15.02.2017  ; 28  ; 29  ; 30
Thu 16.02.2017  ; 31  ; 32  ; 33

And this is the 'plot.py' parsing / plot code:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates


df = pd.read_csv(
        'data.csv',
        sep='\s*;\s*',
        header=None,
        names=['date', 'x', 'y', 'z'],
        parse_dates=['date'],
        date_parser=lambda x: pd.datetime.strptime(x, '%a %d.%m.%Y'),
        # infer_datetime_format=True,
        # dayfirst=True,
        engine='python',
)

# DataFrame 'date' Series looks fine
print df.date

ax1 = df.plot(x='date', y='x', legend=True)
ax2 = df.plot(x='date', y='y', ax=ax1, legend=True)
ax3 = df.plot(x='date', y='z', ax=ax1, legend=True)

ax1.xaxis.set_minor_locator(mdates.DayLocator(interval=1))
ax1.xaxis.set_minor_formatter(mdates.DateFormatter('%a %d.%m.%Y'))
ax1.xaxis.grid(True, which='minor')

plt.setp(ax1.xaxis.get_minorticklabels(), rotation=45)
plt.setp(ax1.xaxis.get_majorticklabels(), visible=False)
plt.tight_layout()

plt.show()

Please note that the DataFrame.date series contains the correct dates, so this is most likely a problem with matplotlib, and not a pandas / parsing error.

, ( ), - LC_TIME = en_US.UTF-8.

, https://www.timeanddate.com/date/weekday.html, 06.02.0048 , - 0048 .

, , .

+6
2

, , , - pandas matplotlib , , mdates.DateFormatter...

, :

# ax1.xaxis.set_minor_locator(mdates.DayLocator(interval=1))
# ax1.xaxis.set_minor_formatter(mdates.DateFormatter('%a %d.%m.%Y'))
# ax1.xaxis.grid(True, which='minor')
# 
# plt.setp(ax1.xaxis.get_minorticklabels(), rotation=45)
# plt.setp(ax1.xaxis.get_majorticklabels(), visible=False)

enter image description here

Pandas , matplotlib . #plt.setp(ax1.xaxis.get_majorticklabels(), visible=False), pandas Matplotlib xaxis, 0048 : enter image description here

, .

, parse_dates=['date'] index_col=0, matplotlib mdates.DateFormatter ticker.FixedFormatter:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.ticker as ticker

df = pd.read_csv(
    'data.csv',
    sep='\s*;\s*',
    header=None,
    names=['date', 'x', 'y', 'z'],
    index_col=0,
    date_parser=lambda x: pd.to_datetime(x, format='%a %d.%m.%Y'),
    engine='python'
)

ax = plt.figure().add_subplot(111)
ax.plot(df)

ticklabels = [item.strftime('%d-%m-%y') for item in df.index]
ax.xaxis.set_major_locator(mdates.DayLocator(interval=1))
ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels))

plt.xticks(rotation='90')
ax.xaxis.grid(True, which='major')

plt.tight_layout()

plt.show()

enter image description here

+2

, .

matForlotlib DateFormatter , . , pandas, postgres, . , ( 0046 2018).

, , .

SELECT start_time::timestamp at time zone '{{timezone}}' as "Start Time" ...

, , , postgres.

0

Source: https://habr.com/ru/post/1673487/


All Articles