0

I am trying to create a graph of blood pressures from a blood pressure recording device. I have extracted all of the data from the device and stored the timestamp of each reading as a DateTime:

import pandas as pd
import matplotlib.pyplot as plt

dataframe["DateTime"] = pd.to_datetime(df[["Year", "Month", "Day", "Hour", "Minute"]]).dt.strftime('%d/%m/%y %H:%M')
x = dataframe.DateTime
a1 = pd.Series(dataframe["BP1 Sys"])
a2 = pd.Series(dataframe["BP2 Dia"])
a3 = pd.Series(dataframe["MAP"])
plt.title(title)
plt.plot(x, a1, marker="^")
plt.plot(x, a2, marker="v")
plt.axhline(y=130)
plt.axhline(y=60)
plt.plot(x, a3, linestyle="dotted", color='green')
plt.bar(x=x, height=a1 - a2, bottom=a2, width=0.05, alpha=.2)
plt.fill_between(x, a1, a2, color='blue', alpha=.1)
plt.gcf().autofmt_xdate()
plt.show()

However, when I create this graph it seems to treat each timestamp as a unique entry (like a bar chart with string labels) rather than creating a continuous axis. This means that every reading looks like it was taken an equal amount of time apart (when come may have a few minutes between and others a few hours). It also means that it creates an X axis mark for each point, rather than putting an appropriate amount of markers in:

matplotlib output

Any Idea what I am doing wrong?

obidose
  • 3
  • 2
  • Do not do `.dt.strftime('%d/%m/%y %H:%M')` you must specify the format of the xticklabels after plotting, otherwise you're plotting strings – Trenton McKinney Nov 21 '21 at 22:00
  • If you want a different format for the xticklabels on the xaxis, then use [Editing the date formatting of x-axis tick labels in matplotlib](https://stackoverflow.com/q/14946371/7758804) as `mdates.DateFormatter('%d/%m/%y %H:%M')` – Trenton McKinney Nov 21 '21 at 22:02
  • 1
    Ahh, that's perfect. I didn't realise the .dt.strftime() function returned a string. Although in retrospect - the hint is in the name. Thank you – obidose Nov 21 '21 at 22:39

0 Answers0