[英]Plotting more than 10K data point using Seaborn for x-axis as timestamp
I am trying to plot more than 10k data points, where I want to plot a data properties versus Timestamp.我正在尝试 plot 超过 10k 数据点,我想 plot 数据属性与时间戳。 But on the x-axis the timestamps are overlapping and not visible.但在 x 轴上,时间戳重叠且不可见。
How can I reduce the amount of labels on the x-axis, so that they are legible?如何减少 x 轴上的标签数量,使它们清晰易读?
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:示例 plot 演示了该问题:
Update: TIMESTAMP was in string format by converting into datatime format it resolves the problem.更新: TIMESTAMP是字符串格式,通过转换为数据时间格式可以解决问题。
data['TIMESTAMP'] = pd.to_datetime(data['TIMESTAMP'])
Please make sure that TIMESTAMP
is a datetime object.请确保TIMESTAMP
是日期时间 object。 This should not happen when the x axis is a datetime.当 x 轴是日期时间时,不应发生这种情况。 (You can use pd.to_datetime
to convert int, float, str, and... to datetime.) (您可以使用pd.to_datetime
将 int、float、str 和...转换为 datetime。)
If TIMESTAMP
is a datetime, you can use the autofmt_xdate()
method:如果TIMESTAMP
是日期时间,则可以使用autofmt_xdate()
方法:
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()
Update:TIMESTAMP was in string format by converting into datetime format it resolves the problem.更新:TIMESTAMP 是字符串格式,通过转换为日期时间格式可以解决问题。
data['TIMESTAMP'] = pd.to_datetime(data['TIMESTAMP'])
I didn't encounter such problem with sns.lineplot
我没有遇到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. sns 自动选择 x 刻度和 x 标签。
alternatively, you can use ax.set_xticks
and ax.set_xlabels
to set the x ticks and x labels manually.或者,您可以使用ax.set_xticks
和ax.set_xlabels
手动设置 x 刻度和 x 标签。 Also you may use fig.autofmt_xdate() to rotate the x labels您也可以使用 fig.autofmt_xdate() 来旋转 x 标签
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.