Python Forum
Plotting Matplotlib Grid Lines
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Plotting Matplotlib Grid Lines
#1
The below script works but I would like to add x minor grid lines.
I am new to Matplotlib so I also fill like I am not sure I am doing everything correctly.

Would someone kindly show me how to get minor x-axis grid lines and if there is a better way to do the plot?

The data is from my glucose meter and contains date as YYYY/MM/DD HH:MM:SS. For three days there are around 500 lines.

I would like to display just the time as HH:MM on the y-axis but can't figure that out either.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# --------------------------------- read from csv ---------------------------
df = pd.read_csv('summary.csv', delimiter='\t', skiprows=3, header=None, names=[
    'id', 'rdate', 'rtype', 'hist', 'scan'], usecols=[0, 1, 2, 3, 4])

# ------------------------ Get last 3 days --------------------------------------
df.tail(600)

# ---------- Fill in 0 ---------------------------
df['hist'] = df['hist'].fillna(0)
df['scan'] = df['scan'].fillna(0)

# -------------- Change dtypes -------------------
df['rdate'] = df['rdate'].astype('datetime64')
df['hist'] = df['hist'].astype('int64')
df['scan'] = df['scan'].astype('int64')

# ------------------------------ Replace nil hist with scan ---------------------------------
df.loc[df['hist'] == 0, 'hist'] = df['scan']

# ------------------------------------- Select Record Type = 0 -----------------------------
sd = (df[df.rtype == 0])

# --------------------------------- Get 3 Days of Data -----------------------
sr = (sd[sd.rdate >= pd.Timestamp.today() - pd.to_timedelta("3day")])


# ------------------------------------- Plot Chart -------------------------
def displayplot(time, glucose):
    fig, ax = plt.subplots()

    plt.plot(time, glucose, color='k', marker='.', label='Glucose', linewidth=1,
             markevery=1, markerfacecolor='blue')

    ax.grid()
    ax.tick_params(axis='y')

    fig.autofmt_xdate()

    plt.axhspan(70, 120, facecolor='lightgreen', alpha=0.5)
    plt.axhspan(120, 180, facecolor='lightblue', alpha=0.5)
    plt.axhspan(180, 310, facecolor='#EF9494', alpha=0.5)
    plt.axhspan(0, 70, facecolor='#EF9494', alpha=0.5)
    plt.title('Daily Glucose Chart')
    plt.ylabel('Glucose')
    plt.xlabel('Time')
    plt.legend(loc=2)
    plt.yticks(np.arange(0, 310, 10.0))
    plt.tight_layout()

    plt.show()


# ---------------------------------------------------------------------------------------------------------

time = []
glucose = []
for i, row in sr.iterrows():
    time.append(row.rdate)
    glucose.append(row['hist'])

displayplot(time, glucose)
Thanks
Gary
Reply


Messages In This Thread
Plotting Matplotlib Grid Lines - by gehrenfeld - Feb-20-2019, 03:04 PM
RE: Plotting Matplotlib Grid Lines - by Larz60+ - Feb-20-2019, 10:39 PM
RE: Plotting Matplotlib Grid Lines - by gehrenfeld - Feb-21-2019, 12:11 AM
RE: Plotting Matplotlib Grid Lines - by Larz60+ - Feb-21-2019, 02:11 AM
RE: Plotting Matplotlib Grid Lines - by gehrenfeld - Feb-21-2019, 12:38 PM
RE: Plotting Matplotlib Grid Lines - by Larz60+ - Feb-21-2019, 02:09 PM
RE: Plotting Matplotlib Grid Lines - by gehrenfeld - Feb-21-2019, 02:17 PM
RE: Plotting Matplotlib Grid Lines - by Larz60+ - Feb-21-2019, 02:26 PM
RE: Plotting Matplotlib Grid Lines - by gehrenfeld - Feb-21-2019, 06:12 PM
RE: Plotting Matplotlib Grid Lines - by Larz60+ - Feb-22-2019, 06:57 PM
RE: Plotting Matplotlib Grid Lines - by gehrenfeld - Feb-22-2019, 07:11 PM
RE: Plotting Matplotlib Grid Lines - by Larz60+ - Feb-22-2019, 11:37 PM
RE: Plotting Matplotlib Grid Lines - by gehrenfeld - Feb-23-2019, 12:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] cutomtkinter matplotlib no x,y - xaxis and x,y - labels-> only graph and grid visible dduric 0 344 Feb-20-2024, 07:09 PM
Last Post: dduric

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020