0

I have a data file. (duration : One day)

I want to plot time vs temp graph. I tried to plot it using two different block of codes. The methods are standard but I am getting two different plots.

In first method I used time in datetime datatype

#method
t=pd.to_datetime(data['time'],format='%H:%M:%S.%f').dt.time
data['time']=t
data.drop('index',axis=1,inplace=True)
ax=data.plot(x='time',color=['tab:blue','tab:red'])
ax.tick_params(axis='x', rotation=45)

enter image description here

In second method I used time in string format

fig, ax = plt.subplots()
ax.plot(data['time'], data['temp'])
ax.plot(data['time'], data['temp_mean'],color='red')
fig.autofmt_xdate()
ax.tick_params(axis='x', rotation=45)
y1,y2=float(data.temp.min()),float(data.temp.max())
ax.yaxis.set_ticks(np.arange(y1,y2),10)
ax.set_xlabel('Time')
ax.set_ylabel('Temp')
plt.show()

The second plot is the expected one. But the one I got have overlapped x ticks. I need to have xticks in hourly format (1 hr frequency)

enter image description here

Why I am getting plots with two different pattern? How to add xticks with 1 hour frequency?

vgb_backup
  • 49
  • 1
  • 6

0 Answers0