Basically, I'm plotting a graph based on a list of times(HH:MM:SS, x-axis) and float values (y-axis) stored in a txt file like this:
15 52 27 0.00
15 52 37 0.2
15 52 50 0.00
15 53 12 2.55
15 54 21 10.00
15 55 15 13.55
I want to plot the last float values (as an annotation text label) in correspondence of the last time available. Using the txt above, I want to plot "13.55 mL" in correspondence of the point [15 55 15, 13.55]
.
Here's the code to plot my graph:
datefunc = lambda x: mdates.date2num(datetime.strptime(x.decode("utf-8"), '%H %M %S'))
dates, levels = np.genfromtxt('sensor1Text.txt', # Data to be read
delimiter=8, # First column is 8 characters wide
converters={0: datefunc}, # Formatting of column 0
dtype=float, # All values are floats
unpack=True) # Unpack to several variables
# Configure x-ticks
plot_fs1.set_xticks(dates) # Tickmark + label at every plotted point
plot_fs1.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
plot_fs1.set_ylabel('Fluid (mL)')
plot_fs1.grid(True)
# Format the x-axis for dates (label formatting, rotation)
fs1.autofmt_xdate(rotation= 45)
plot_fs1.plot_date(dates, levels, color='orange', ls='-', marker='o')
Here's my attempt to plot the annotation label on my last plotted value:
lastxValue= len(dates)-1
lastyValue= len(levels)-1
lastValue = levels[lastyValue]
lastDate = dates[lastxValue]
plot_fs1.annotate(lastValue, (lastDate,
lastValue),xytext=(15, 15),textcoords='offset points')
fs1.tight_layout()
This is what I get:
The annotation is not completely displayed within the plot window and x-axis values tend to overlap on one another.
Any thoughts?