Issue
I am trying to create a graph using matplotlib with num of requests (y-axis) vs time stamp (x-axis in HH:MM format). This graph will show the pattern for the all the requests received between 6:00 AM to 6:00 PM. Below is the sample data. Actual data has more than 500 entries.
time_stamp = ['06:02', '06:03', '06:12', '06:16', '06:17', '06:27', '06:28', '06:30', '06:31', '06:34', '06:35', '06:36', '06:37', '06:38', '06:39', '06:40', '06:41', '06:42', '06:43']
requests = [74, 20, 2, 1, 11, 9, 34, 3, 5, 4, 28, 77, 75, 73, 122, 99, 170, 79, 44, 79, 100, 58, 104, 84, 77, 98, 27]
Below is the script which I am using to generate the graph. Problem which I am facing currently is overlapping of all the time stamp on the x-axis.
Script:
import matplotlib.pyplot as plt
TITLE = 'Time (Per Minute) Vs Num of Requests Graph'
X_AXIS_NAME = 'TimeStamps (per minute)'
Y_AXIS_NAME = 'No. of Requests'
time_stamp = ['06:02', '06:03', '06:12', '06:16', '06:17', '06:27', '06:28',
'06:30', '06:31', '06:34', '06:35', '06:36', '06:37', '06:38', '06:39',
'06:40', '06:41', '06:42', '06:43', '06:44', '06:45', '06:46', '06:47',
'06:48', '06:49', '06:50', '06:51', '06:52', '06:53', '06:54', '06:55',
'06:56', '06:57', '06:58', '06:59', '07:00', '07:01']
requests = [74, 20, 2, 1, 11, 9, 34, 3, 5, 4, 28, 77, 75, 73]
fig, ax = plt.subplots()
plt.plot(time_stamp, requests)
fig.autofmt_xdate()
plt.xlabel(X_AXIS_NAME)
plt.ylabel(Y_AXIS_NAME)
plt.title(TITLE)
plt.show()
fig.savefig('graph.png', dpi=fig.dpi)
And this is the graph which I actually want to generate. This graph has been generated using excel.
Expected Graph: Timestamps are not over lapped.
EDIT 1:
dates = []
for ts in time_stamp:
dates.append( datetime.strptime(ts, '%H:%M'))
mp_dates = matplotlib.dates.date2num(dates)
matplotlib.pyplot.plot_date(mp_dates, requests)
EDIT 2:
dates = []
for ts in time_stamp:
local_d = datetime.strptime(ts, '%H:%M')
dates.append( local_d)
fig, ax = plt.subplots()
plt.setp( ax.xaxis.get_majorticklabels(), rotation=90)
plt.plot(dates, requests)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
#fig.autofmt_xdate()
plt.xlabel(X_AXIS_NAME)
plt.ylabel(Y_AXIS_NAME)
plt.title(TITLE)
# function to show the plot
plt.show()
fig.savefig('graph.png', dpi=fig.dpi)
Only missing piece is to reduce the interval between 2 ticks , currently it is 2 hours.
Any help or pointer in this regards is highly appreciated.
Solution
After doing more research finally I am able to plot it.
dates = []
for ts in time_stamp:
local_d = datetime.strptime(ts, '%H:%M')
dates.append( local_d)
fig, ax = plt.subplots()
plt.setp( ax.xaxis.get_majorticklabels(), rotation=90)
plt.plot(dates, requests)
ax.xaxis.set_major_locator(mdates.MinuteLocator(interval=20))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
plt.xlabel(X_AXIS_NAME)
plt.ylabel(Y_AXIS_NAME)
plt.title(TITLE)
plt.show()
fig.savefig('graph.png', dpi=fig.dpi)
Thanks to the community!
Answered By - saurav
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.