Plotting more than 10K data point using Seaborn for x-axis as timestamp

Viewed 613

I am trying to plot more than 10k data points, where I want to plot a data properties versus Timestamp. But on the x-axis the timestamps are overlapping and not visible.

How can I reduce the amount of labels on the x-axis, so that they are legible?

import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
sns.set_style("whitegrid")

data = pd.read_csv('0912Testday4.csv',header=2)

for i in data.columns:
    if i!='TIMESTAMP':
        sns.lineplot(x="TIMESTAMP",y=i,data = data)
        plt.title(f"{i} vs TIMESTAMP")
        plt.show()

Example plot demonstrating the problem:

Example plot demonstrating the problem

Update:TIMESTAMP was in string format by converting into datatime format it resolves the problem.

data['TIMESTAMP'] = pd.to_datetime(data['TIMESTAMP'])
3 Answers

Update:TIMESTAMP was in string format by converting into datetime format it resolves the problem.

data['TIMESTAMP'] = pd.to_datetime(data['TIMESTAMP'])

Please make sure that TIMESTAMP is a datetime object. This should not happen when the x axis is a datetime. (You can use pd.to_datetime to convert int, float, str, and ... to datetime.)

If TIMESTAMP is a datetime, you can use the autofmt_xdate() method:

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

fig, ax = plt.subplots() # Create a figure and a set of subplots.

sns.set_style("whitegrid")

data = pd.read_csv('0912Testday4.csv',header=2)

# Use the following line if the TIMESTAMP is not a datetime.
# (You may need to change the format from "%Y-%m-%d %H:%M:%S+00:00".)
# data['TIMESTAMP'] = pd.to_datetime(data.TIMESTAMP, format="%Y-%m-%d %H:%M:%S+00:00")

for i in data.columns:
    if i!='TIMESTAMP':
        sns.lineplot(x="TIMESTAMP", y=i, data=data, ax=ax)
        fig.autofmt_xdate() # rotate and right align date ticklabels
        plt.title(f"{i} vs TIMESTAMP")
        plt.show()

I didn't encounter such problem with sns.lineplot

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns

sns.set_style("whitegrid")

# example data
time_stamps = pd.date_range('2019-01-01', '2020-01-01', freq='H')
vals =[np.random.randint(0, 1000) for i in time_stamps]
data_df = pd.DataFrame()
data_df['time'] = time_stamps
data_df['value'] = vals
print(data_df.shape)

# plotting
fig, ax = plt.subplots()
sns.lineplot(x='time', y='value', data=data_df)


plt.show()

sns automatically selects the x ticks and x labels.

alternatively, you can use ax.set_xticks and ax.set_xlabels to set the x ticks and x labels manually. Also you may use fig.autofmt_xdate() to rotate the x labels

Related