Home > Enterprise >  Plotting time series from a dataframe
Plotting time series from a dataframe

Time:07-21

Trying to make a plot using Pandas with the below data frame.

data = {
    'date': ['2022-07-21 03:28:59.126007', '2022-07-21 03:29:01.127014', '2022-07-21 03:29:02.137008',
             '2022-07-21 03:29:05.136001', '2022-07-21 03:29:06.139007', '2022-07-21 03:53:30.094009',
             '2022-07-21 03:53:31.097000', '2022-07-21 03:53:40.096008', '2022-07-21 03:53:41.094009',
             '2022-07-21 03:53:42.094009', '2022-07-21 04:26:25.108001', '2022-07-21 04:26:26.107009',
             '2022-07-21 04:26:28.109008', '2022-07-21 04:26:29.114013', '2022-07-21 04:26:30.117004'],
    'RFID_NO': ['RFID 003', 'RFID 003', 'RFID 003', 'RFID 003',
                'RFID 003', 'RFID 002', 'RFID 002', 'RFID 002',
                'RFID 002', 'RFID 002', 'RFID 014', 'RFID 014',
                'RFID 014', 'RFID 014', 'RFID 014'],
    'RFID Location': [51, 50, 51, 50, 51, 73, 72, 73, 74, 73, 1, 2, 3, 4, 12],
    'Sh Location': [131, 132, 132, 132, 132, 124, 124, 124, 124, 124, 6, 6, 6, 6, 6]}

df = pd.DataFrame(
    data, columns=[
        'date', 'RFID_NO', 'RFID Location', 'Sh Location'])

                          date   RFID_NO  RFID Location  Sh Location
0   2022-07-21 03:28:59.126007  RFID 003             51               131
1   2022-07-21 03:29:01.127014  RFID 003             50               132
2   2022-07-21 03:29:02.137008  RFID 003             51               132
3   2022-07-21 03:29:05.136001  RFID 003             50               132
4   2022-07-21 03:29:06.139007  RFID 003             51               132
5   2022-07-21 03:53:30.094009  RFID 002             73               124
6   2022-07-21 03:53:31.097000  RFID 002             72               124
7   2022-07-21 03:53:40.096008  RFID 002             73               124
8   2022-07-21 03:53:41.094009  RFID 002             74               124
9   2022-07-21 03:53:42.094009  RFID 002             73               124
10  2022-07-21 04:26:25.108001  RFID 014              1                 6
11  2022-07-21 04:26:26.107009  RFID 014              2                 6
12  2022-07-21 04:26:28.109008  RFID 014              3                 6
13  2022-07-21 04:26:29.114013  RFID 014              4                 6
14  2022-07-21 04:26:30.117004  RFID 014             12                 6


def plot_values(df_plot):
    df_plot["date"] = df_plot["date"].astype("datetime64")
    v_RFID = df_plot['RFID Location']
    v_SLC = df_plot['Sh Location']

    df_plot = pd.DataFrame(list(zip(v_RFID, v_SLC)),
                           index=df_plot["date"],
                           columns=['RFID_no', 'Sh Location'])
    df_plot.plot()
    plt.show()

plot_values(df)

enter image description here

As seen in the frame, there are 3 RFID's, (003, 002, 014). How do I plot each RFID number, its location v time separately and display in a legend? The RFID location (blue) has the three combined. Do I 'somehow' need to split the RFID_no column out into separate columns and related values?

Tks

CodePudding user response:

You use date as the x-coordinate, but they don't have a uniform date value after the RFID_NO distinction, so obviously they can't be placed in one diagram,you could try using subdiagrams to achieve this.

If you need to add other attributes you can change them yourself

df = pd.DataFrame(data, columns=['date', 'RFID_NO', 'RFID Location', 'Sh Location'])


for i, (RFID, item) in enumerate(df.groupby("RFID_NO")):
    plt.subplot(3, 1, i 1)
    item["date"] = pd.to_datetime(item["date"])
    plt.plot(item["date"], item["Sh Location"], label=RFID)
    plt.legend()

plt.show()

CodePudding user response:

Here's one way to do it.

# Prepare "date" column as index
df["date"] = pd.to_datetime(df["date"])
df = df.set_index("date")

fig, axes = plt.subplots(3, 1)
for ax, (name, data) in zip(axes, df.groupby("RFID_NO")):
    data.plot(ax=ax)
    ax.set_title(name)

plt.tight_layout()
plt.show()

enter image description here

  • Related