1

I have a dataframe of daily temperature variation with time

time    temp    temp_mean
00:01:51.57 185.94  185.94
00:01:52.54 187.48  186.71
00:01:53.51 197.85  190.4233333
00:01:54.49 195.71  191.745
00:01:55.46 197.22  192.84
00:01:56.43 187.33  191.9216667
00:01:57.41 194.18  192.2442857
00:01:58.38 199.9   193.20125
00:01:59.35 184.23  192.2044444
00:02:00.33 201.34  193.118
00:02:01.30 200.12  193.7545455
00:02:02.27 199.13  194.2025
00:02:03.24 187.47  193.6846154
00:02:04.22 187.65  193.2535714
00:02:05.19 195.59  193.4093333
00:02:06.17 188.7   193.115
00:02:07.14 196.16  193.2941176
00:02:08.11 191.17  193.1761111
00:02:09.08 198.62  193.4626316
00:02:10.06 190.79  193.329
00:02:11.03 193.35  193.33
00:02:12.00 199.36  193.6040909
00:02:12.98 190.76  193.4804348
00:02:13.95 205.16  193.9670833
00:02:14.92 194.89  194.004
00:02:15.90 185.3   193.6692308

like this. (12000+ rows) I want to plot time vs temp as a line plot, with hourly ticks on x-axis(1 hr interval). But somehow I couldn't assign x ticks with proper frequency.

fig, ax = plt.subplots()
ax.plot(data['time'], data['temp'])
ax.plot(data['time'], data['temp_mean'],color='red')
xformatter = mdates.DateFormatter('%H:%M')
xlocator = mdates.HourLocator(interval = 1)

## Set xtick labels to appear every 15 minutes
ax.xaxis.set_major_locator(xlocator)

## Format xtick labels as HH:MM
ax.xaxis.set_major_formatter(xformatter)

fig.autofmt_xdate()
ax.tick_params(axis='x', rotation=45)
plt.show()

Plot I got.

Here xticks seems to be crowded and overlapping, but I need ticks from 0:00 to 23:00 with one hour interval. What should I do ?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
vgb_backup
  • 49
  • 1
  • 6
  • 1
    Probably the issue is `00:01:51.57` are strings, they probably need to be formatted as something like a pandas timedelta. See [Convert string to timedelta in pandas](https://stackoverflow.com/q/53543061/7758804) – Trenton McKinney Nov 02 '21 at 08:07
  • Yeah,But When I convert to datetime the xticks seems to be fine but the entire plot showing a different pattern. This is that plot suppose to be except the xtick part/ – vgb_backup Nov 02 '21 at 08:49
  • There's not sufficient data to reproduce that. Can you provide a link to the full data? – Trenton McKinney Nov 02 '21 at 08:54

1 Answers1

0
import pandas as pd

# sample data
data = {'time': ['00:01:51.57', '00:01:52.54', '00:01:53.51', '00:01:54.49', '00:01:55.46', '00:01:56.43', '00:01:57.41', '00:01:58.38', '00:01:59.35', '00:02:00.33', '00:02:01.30', '00:02:02.27', '00:02:03.24', '00:02:04.22', '00:02:05.19', '00:02:06.17', '00:02:07.14', '00:02:08.11', '00:02:09.08', '00:02:10.06', '00:02:11.03', '00:02:12.00', '00:02:12.98', '00:02:13.95', '00:02:14.92', '00:02:15.90'],
        'temp': [185.94, 187.48, 197.85, 195.71, 197.22, 187.33, 194.18, 199.9, 184.23, 201.34, 200.12, 199.13, 187.47, 187.65, 195.59, 188.7, 196.16, 191.17, 198.62, 190.79, 193.35, 199.36, 190.76, 205.16, 194.89, 185.3],
        'temp_mean': [185.94, 186.71, 190.4233333, 191.745, 192.84, 191.9216667, 192.2442857, 193.20125, 192.2044444, 193.118, 193.7545455, 194.2025, 193.6846154, 193.2535714, 193.4093333, 193.115, 193.2941176, 193.1761111, 193.4626316, 193.329, 193.33, 193.6040909, 193.4804348, 193.9670833, 194.004, 193.6692308]}
df = pd.DataFrame(data)

# convert column to datetime and extract time component
df.time = pd.to_datetime(df.time, format='%H:%M:%S.%f').dt.time

# plot
ax = df.plot(x='time', color=['tab:blue', 'tab:red'])

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158