0

I am trying to plot a large dataset using a CSV file I had gathered previously. When I plot the data using plt.step every xtick is marked making it unreadable. How do I reduce the number of displayed xticks but keep the same graph?

I have tried using plt.xticks with np.arrange but I keep getting errors as I am using a Dataframe rather than an array. Additionally the x values I'm working with is time in the following format (%H:%M:%S).

import pandas as pd
import numpy as np
import datetime
import matplotlib
matplotlib.use("TkAgg")
from matplotlib import pyplot as plt

df = pd.read_csv(thisFile)
plt.figure(figsize=(16,6))
plt.step(df.Time, df.HR)

print(df.Time)

Output
0 13:40:34
1 13:40:44
2 14:18:29
3 14:19:15
4 14:20:58
5 14:21:17

plt.ylabel('Heart Rate (BPM)')
plt.xlabel('Time')
plt.show()

Displaying the plot is mostly correct except for the xtick frequency. It appears too often and collides with each other as shown in my graph output.

Ideally I would like for the xticks to display from 12AM to 12PM with a xtick frequency of an hour.

Kyle
  • 1
  • 1
  • try: `plt.xticks(your_desired_xticks)` – Quang Hoang Apr 19 '19 at 19:50
  • 1
    You forgot to convert your date strings to actual dates. – ImportanceOfBeingErnest Apr 20 '19 at 00:33
  • To expand on what @ImportanceOfBeingErnest said: you should specify what *kind of data* pandas can expect from each column, see e.g. in [this anwer](https://stackoverflow.com/a/37453925/565489) where a `parse_dates=['col1', 'col2']` argument is given to `read_csv()`. To specify the plot range on the x axis, look around here for `plt.xlim( )` and `plt.xticks()`, there are many answers for this already. – Asmus Apr 20 '19 at 10:15

0 Answers0