ref: 4db10ad3e1cbace8fd76a189d37a3a1daeaa3b67
parent: c3d286cf66643e0ccf088ccdf2df24061d5f8c0c
author: Paul Brossier <piem@piem.org>
date: Fri Feb 8 23:48:47 EST 2013
demo_spectrogram.py: round ticks
--- a/interfaces/python/demo_spectrogram.py
+++ b/interfaces/python/demo_spectrogram.py
@@ -31,12 +31,26 @@
print ", samplerate: %.2fkHz" % (samplerate / 1000.)
n_xticks = 10
n_yticks = 10
- xticks_pos = [ x / float(n_xticks) * len(specgram) for x in range(n_xticks) ]
- xticks_str = [ "%.2f" % (x * total_time / float(n_xticks)) for x in range(n_xticks) ]
- xticks( xticks_pos , xticks_str )
- yticks_pos = [ y / float(n_yticks) * len(specgram[0]) for y in range(n_yticks) ]
- yticks_str = [ "%.2f" % (y * samplerate / 2000. / float(n_yticks)) for y in range(n_yticks) ]
- yticks( yticks_pos , yticks_str )
+
+ def get_rounded_ticks( top_pos, step, n_ticks ):
+ top_label = top_pos * step
+ # get the first label
+ ticks_first_label = top_pos * step / n_ticks
+ # round to the closest .1
+ ticks_first_label = round ( ticks_first_label * 10. ) / 10.
+ # compute all labels from the first rounded one
+ ticks_labels = [ ticks_first_label * n for n in range(n_ticks) ] + [ top_label ]
+ # get the corresponding positions
+ ticks_positions = [ ticks_labels[n] / step for n in range(n_ticks) ] + [ top_pos ]
+ # convert to string
+ ticks_labels = [ "%.1f" % x for x in ticks_labels ]
+ # return position, label tuple to use with x/yticks
+ print ticks_positions, ticks_labels
+ return ticks_positions, ticks_labels
+
+ # apply to the axis
+ xticks( *get_rounded_ticks ( len(specgram), time_step, n_xticks ) )
+ yticks( *get_rounded_ticks ( len(specgram[0]), (samplerate / 2. / 1000.) / len(specgram[0]), n_yticks ) )
ylabel('Frequency (kHz)')
xlabel('Time (s)')