Jump to content

Format y axis on a seaborn plot and draw rectangles at certain points (Python)

So I'm trying to make a graph program that plots weight over a given time period with using BMI to display which points are underweight, healthyweight and overweight. Here's my current code:

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import seaborn as sns
import pandas as pd
from datetime import datetime
import csv

weights = []
dates = []

newWeight = float(input("Enter new weight: "))
newDate = datetime.today().date()

with open("re.txt", "a") as file:
    lineCount = sum(1 for line in open('re.txt'))
    newRecord = f"{lineCount},{newWeight},{newDate}\n"
    file.write(newRecord)

with open("re.txt", "r") as file:
    csv_reader = csv.reader(file, delimiter=",")
    for count, record in enumerate(csv_reader):
        if count == 0:
            continue
        else:
            weights.append(record[1])
            date = datetime(year=int(record[2][0:4]), month=int(record[2][5:7]), day=int(record[2][8:10])).date()
            dates.append(date)

df = pd.DataFrame({"Weight": weights, "Dates": dates}, columns=["Weight", "Dates"])

print(df)

df["Dates"] = pd.to_datetime(df["Dates"])

df.sort_values(by="Dates", inplace=True, ascending=False)

sns.set_theme(style="dark")

dates = [datetime.date(x) for x in df["Dates"]]

graph = sns.scatterplot(data=df, x=dates, y="Weight", color="black")

currentAxis = plt.gca()
#currentAxis.add_patch(Rectangle((0,0), 1000000, 51.2559, facecolor="yellow"))
#currentAxis.add_patch(Rectangle((0,51.2559), 1000000, 62.5957, facecolor="green"))
#currentAxis.add_patch(Rectangle((0,62.5957), 1000000, 200, facecolor="red"))

plt.show()

#df.to_csv("re.txt")

However, when I run the code (without the rectangles), this is the result:

https://ibb.co/BPX77mW

And with the rectangles:

https://ibb.co/BPX77mW

 

If anyone can find where I've gone wrong, that'll be great. Thanks!

Specs:

Motherboard: Gigabyte Z97X Gaming 3

CPU: Intel Core I7 4790K

GPU: Gigabyte G1 Gaming GTX 970

RAM: HyperX Fury 16GB

HDD: Seagate ST3000DM001 3TB

SSD: KINGSTON SV300S37A480G 450GB

Cooler: Corsair H100i GTX

Case: NZXT H440 Red

Monitor: DELL U2412M

Keyboard: Gigabyte Force K7

Mouse: Corsair Sabre RGB

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×