How do I show mm:ss instead of sample numbers in the following diagram?
I tried:
def formatMS(miliseconds):
return time.strftime('%M:%S', time.gmtime(miliseconds//1000))
fig, ax = plt.subplots()
plt.plot(segtime, segStrength)
labels = [formatMS(t) for t in segtime]
print labels
ax.set_xticklabels(labels)
But now it is showing all 00:00. How do I fix this problem?
To increase the space for X-axis labels in Matplotlib, we can use the spacing variable in subplots_adjust() method's argument.
What Does Matplotlib Mean? Matplotlib is a plotting library available for the Python programming language as a component of NumPy, a big data numerical handling resource. Matplotlib uses an object oriented API to embed plots in Python applications.
As an alternative to setting the labels, you could use a tick formatter as follows:
import matplotlib
import matplotlib.pyplot as plt
import time
segtime = [1000, 2000, 3000, 3500, 7000]
segStrength = [10000, 30000, 15000, 20000, 22000]
fig, ax = plt.subplots()
plt.plot(segtime, segStrength)
formatter = matplotlib.ticker.FuncFormatter(lambda ms, x: time.strftime('%M:%S', time.gmtime(ms // 1000)))
ax.xaxis.set_major_formatter(formatter)
plt.show()
This will then convert a tick into the format you need, giving you:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With