0

I am trying to use matplot to graph the number of events in a specific minute generated by different inputs.

broadly similar to the screen clip below, but showing a simple 24 hour period only.

Part of my problem is that I don't know how to work with Y axis data that is not a complete set. eg, if all I have is 2 events, one at 04:04 (y=5) and one at 13:22 (y=1) how do I make that show on a graph for the full 24 hour period please?

I have been experimenting with stacked histtype=step, but I am not making any progress and can't find any examples in http://matplotlib.org/gallery.html

If there is a better plotter for this sort of output I would be happy to try that too.

Thanks

Edit: Adding example that is not quite what I want. need it to: (1). use time on the x axis (2). use time for the events (3). bars to start at 0 (4). not have that line in the middle (5). later on add different events in a different colour

import matplotlib
# Force matplotlib to not use any Xwindows backend.
matplotlib.use('Agg')
import matplotlib.pyplot as plt

a = [1,2,5,6,9,11,15,17,18]
plt.hlines(1,0,24)  # Draw a horizontal line
plt.eventplot(a, orientation='horizontal', colors='b')

plt.savefig('foo.png', bbox_inches='tight')

not quite right

3
  • 1
    I'm not sure what you mean exactly, but, perhaps, set_xlim() (to set the 24-hour window) will help? Some simplified runnable example would be helpful. Commented Dec 16, 2015 at 1:39
  • 1
    Are you looking for event plots? matplotlib.org/examples/pylab_examples/eventplot_demo.html Commented Dec 16, 2015 at 5:28
  • Thanks very much for your comments. I am a complete noob to Python and matplot so I am still struggling. The eventplot_demo top left pane looks as if it might be very helpful but I can't work out how to code a more simple example so I can start to learn. I will keep trying & post a solution if I manage it. Commented Dec 16, 2015 at 13:03

1 Answer 1

1

I think you want to set a 24 hour period in minutes using plt.xlim which creates an index from 0-1440 along the xaxis. Then you can append bars with height equal to your y value, and located based on the index.

plt.figure(figsize=(29, 9))
hours = 24
mins = 60
xlabels = ['%02d:%02d' % (divmod(i, 60)) for i in range(0, mins * hours, 10)]
plt.xlim(0, hours * mins)
plt.ylim(0,6)
xdata_org = ['04:04', '13:22']
ydata = [5, 1]
def get_min(time):
    l = time.split(':')
    return int(l[0]) * 60 + int(l[1])
xdata = [get_min(i) for i in xdata_org]
plt.bar(xdata, ydata, width=1)
plt.xticks(range(0, hours * mins, 10), xlabels, rotation='vertical', fontsize=9)
plt.subplots_adjust(left=0.05, right=0.95, bottom=0.3)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Brian Huey, this is exactly what I was attempting. It is surprisingly complex. I was expecting matplot to have more native time handing. That is why I couldn't find it in the documentation I think. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.