3

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'])
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • 2
    10k labels wouldn't be visible even if the labels are rotated by 90 degrees, maybe plot after aggregating over a year or a month? – Ch3steR Jul 28 '20 at 04:58
  • i just wanted to show few labels. – Abhishek Gupta Jul 28 '20 at 04:59
  • Does this answer your question? [tick frequency when using seaborn/matplotlib boxplot](https://stackoverflow.com/questions/44521648/tick-frequency-when-using-seaborn-matplotlib-boxplot) – SiHa Jul 28 '20 at 05:31

3 Answers3

1

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

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

0

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()
hoomant
  • 455
  • 2
  • 12
-2

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

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
James Li
  • 1
  • 2