0

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:

graph_added

The annotation is not completely displayed within the plot window and x-axis values tend to overlap on one another.

Any thoughts?

Guto
  • 541
  • 5
  • 14
  • 1
    I'd try out: changing figure size, changing font size, avoiding the `tight_layout()` call – norok2 Sep 25 '17 at 08:24

2 Answers2

1

To avoid having x-axis entries for every point you plot, you could use a locator to just mark for example every minute on your graph.

Secondly, avoid using tight_layout() and instead make use of subplots_adjust() to add additional spacing where you need it. For example:

import numpy as np
import matplotlib
import matplotlib.dates as mdates
import matplotlib.pyplot as plt

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

plot_fs1 = plt.gca()                
fig = plt.gcf()
p = plt.plot(dates, levels)

plot_fs1.set_xticks(dates) # Tickmark + label at every plotted point

plot_fs1.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
plot_fs1.xaxis.set_major_locator(matplotlib.dates.MinuteLocator())

plot_fs1.set_ylabel('Fluid (mL)')
plot_fs1.grid(True)
fig.autofmt_xdate(rotation= 45)
plot_fs1.plot_date(dates, levels, color='orange', ls='-', marker='o')

lastxValue = len(dates)-1
lastyValue = len(levels)-1

lastValue = levels[lastyValue]
lastDate = dates[lastxValue]

plot_fs1.annotate("{} mL".format(lastValue), (lastDate, lastValue), xytext=(15, 15), textcoords='offset points')
fig.subplots_adjust(bottom=0.15, right=0.85)     # Add space at bottom and right
plt.show()

This would give you a graph looking:

matplotlib graph

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
0

One option is use a similar approach as this question and have independent and linear spaced xticks and use dates just as the name of the ticks.

That question uses bars, but you can use your difference in seconds and see how much total time you have passed, make your tick spacing in order to cover the whole time, but with the same step. Your time difference (that has difference spacing) you just need in the plot of your points. The xticks get nicer with a proper spacing. It also help to add an extra space that you need for your text.

Guto
  • 541
  • 5
  • 14