0

I wanted to add average speed below the x axis labels in this graph with matplotlib. After testing with some hardcoded text I noticed that it gets clipped. I wanted to print "43.2 km/h" below the times. Example code below

plt.hist(data,         # data
         30,           # bins
         None,         # data range
         False,        # normalized
         None,         # weights
         False,        # cumulative
         None,         # bottom shift
         'bar',       # graph style
         'mid',        # bar positions
         'vertical',   # vertical/horizontal
         None,         # bar width
         False,        # log
         None,         # color
         None,         # label
         False         # stacked
)
ax = plt.gca()
ax.set_title(group + " Run")
ax.get_xaxis().get_major_formatter().set_useOffset(False)
plt.draw()
labels = []
for label in ax.get_xticklabels():
    try:
        t = timedelta(seconds=int(float(label.get_text())))
    except ValueError:
        t = label.get_text()
        print ("(EE) x label problem in /tmp/run"+group+".svg")
    label.set_text(str(t) + "\n43.2 km/h")
    label.set_rotation(-45)
    labels.append(label)
ax.set_xticklabels(labels)
plt.savefig("/tmp/run"+group+".svg")
plt.close('all')

the label loop converts seconds into the formatted times you see in the image below.

why clipping?

Chris H
  • 6,433
  • 5
  • 33
  • 51

0 Answers0