Our booth in the laboratory gives this log:

1533288841;34441818;03-08-2018;09:34:01;.tmp9;36.748970; 1533288842;34442968;03-08-2018;09:34:02;.tmp9;36.748970; 1533288844;34444019;03-08-2018;09:34:04;.tmp9;36.748970; 1533288845;34445029;03-08-2018;09:34:05;.tmp9;36.748970; 1533288846;34446025;03-08-2018;09:34:06;.tmp9;36.748970; 1533288847;34447064;03-08-2018;09:34:07;.tmp9;36.748970; 1533288848;34448072;03-08-2018;09:34:08;.tmp9;36.748970; 1533288849;34449120;03-08-2018;09:34:09;.tmp9;36.748970; 1533288850;34450143;03-08-2018;09:34:10;.tmp9;36.773521; 

We are trying to plot a temperature over time.

 import pandas as pd import matplotlib.pyplot as plt plt.style.use('ggplot') plt.rcParams['figure.figsize'] = (10, 5) fn = r'C:\\NET\\Log\\tmp92.trd' df = pd.read_csv(fn, sep=';') df.columns = ['atime', 'ctime', 'date', 'time', 'tag' , 'tmp' , 'NaN'] df['date'] = df.date.astype('datetime64[ns]') df['time'] = df.time.astype('datetime64[ns]') df.dropna(axis = 1, thresh=3) df.drop(df.columns[[0, 1, 4]], axis=1, inplace=True) plt.xlabel('Время') plt.ylabel('Температура (С)') plt.title('Температура на выходе активатора') plt.grid(True) df.plot(x='time', y='tmp') plt.show() 

It seems to have happened, but we can not defeat the lower axis. I wanted to see the time there, and not 08-06-10, it is clear that the joint in the format date + time. - how to make only time ??

    2 answers 2

    Normally, Pandas handles this task well if the column has the correct data type - datetime .

    Example:

     In [73]: df['Время'] = pd.to_datetime(df['date'] + ' ' + df['time']) In [74]: df Out[74]: atime ctime date time tag tmp NaN Время 0 1533288841 34441818 03-08-2018 09:34:01 .tmp9 36.748970 NaN 2018-03-08 09:34:01 1 1533288842 34442968 03-08-2018 09:34:02 .tmp9 36.748970 NaN 2018-03-08 09:34:02 2 1533288844 34444019 03-08-2018 09:34:04 .tmp9 36.748970 NaN 2018-03-08 09:34:04 3 1533288845 34445029 03-08-2018 09:34:05 .tmp9 36.748970 NaN 2018-03-08 09:34:05 4 1533288846 34446025 03-08-2018 09:34:06 .tmp9 36.748970 NaN 2018-03-08 09:34:06 5 1533288847 34447064 03-08-2018 09:34:07 .tmp9 36.748970 NaN 2018-03-08 09:34:07 6 1533288848 34448072 03-08-2018 09:34:08 .tmp9 36.748970 NaN 2018-03-08 09:34:08 7 1533288849 34449120 03-08-2018 09:34:09 .tmp9 36.748970 NaN 2018-03-08 09:34:09 8 1533288850 34450143 03-08-2018 09:34:10 .tmp9 36.773521 NaN 2018-03-08 09:34:10 In [75]: df.dtypes Out[75]: atime int64 ctime int64 date object time object tag object tmp float64 NaN float64 Время datetime64[ns] dtype: object In [76]: df.plot(x='Время', y='tmp') Out[76]: <matplotlib.axes._subplots.AxesSubplot at 0x1b914930470> 

    UPDATE: matplotlib allows you to control the format of signatures:

     import matplotlib.dates as mdates df = pd.read_csv(r'C:\download\tmp92.trd', sep=';', header=None, names=['date','time','tmp'], usecols=[2,3,5]) df['Время'] = pd.to_datetime(df.pop('date') + ' ' + df.pop('time')) ax = df.plot(x='Время', y='tmp') ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M\n%d.%m.%Y')) plt.tight_layout() 

    enter image description here

    • one
      @Ivan, can you upload all your data to any file sharing service so that you can reproduce the graph? - MaxU
    • one
      In the comment above, I threw the link. dropmefiles.com/PwmxF - Ivan
    • one
      @Ivan, and in what format do you want to get a signature on the X axis? And if you have values ​​belonging to several days - how to be then? - MaxU
    • one
      @Ivan, added option with formatting signatures. It looks like the Russian-speaking SO broke down the service of adding pictures - to view the result click on the link at the end of the answer ... - MaxU
    • one
      @Ivan, it is better to ask a separate question and give an example of data in it ... - MaxU

    If you only need time, you can use the pd.to_datetime method.

    I modified your script a bit to get the desired schedule:

     import pandas as pd import matplotlib.pyplot as plt plt.style.use('ggplot') plt.rcParams['figure.figsize'] = (10, 5) fn = 'pydata.txt' df = pd.read_csv(fn, sep=';') df.columns = ['atime', 'ctime', 'date', 'time', 'tag' , 'tmp' , 'NaN'] df.dropna(axis = 1, thresh=3) df.drop(df.columns[[0, 1, 4]], axis=1, inplace=True) plt.xlabel('Время') plt.ylabel('Температура (С)') plt.title('Температура на выходе активатора') plt.grid(True) # Здесь в качестве x передаем преобразованный в тип datetime столбец plt.plot(pd.to_datetime(df['time']), df['tmp']) plt.show() 

    This is the result: enter image description here

    • Thank you kind man. All day, the brain with the datetime was broken :) we did not have time to get it out - Ivan
    • one
      @Ivan If the answer solved your problem, please mark it as correct. By the way, for the designation of axis signatures, you can use the latex syntax - for example, degrees Celsius can be specified like this: plt.ylabel('Температура $^\circ С$') - san-smith
    • Damn, we can not repeat the code :) we still have the X axis. But about Celsius format swears on / - Ivan
    • ibb.co/e7DnGK is the picture. Celsius won. - Ivan
    • but the date and time = object, not the date and in the second column the time df.info () <class 'pandas.core.frame.DataFrame'> RangeIndex: 17369 entries, 0 to 17368 Data columns (total 4 columns): date 17369 non-null object time 17369 non-null object tmp 17369 non-null float64 NaN 0 non-null float64 dtypes: float64 (2), object (2) memory usage: 542.9+ KB - Ivan