简体   繁体   English

Seaborn Plot - X 轴上的日期错误

[英]Seaborn Plot - Wrong Dates on X Axis

I have a dataframe that associates each date with multiple values.我有一个 dataframe 将每个日期与多个值相关联。 The date range is from 02-02 to 04-30.日期范围是从 02-02 到 04-30。

I have a dataframe with two columns -- 'Date' and 'Score'.我有一个 dataframe 有两列——“日期”和“分数”。 The 'Date' entries are timestamps. “日期”条目是时间戳。

 dem_data = {Timestamp('2020-02-02 22:27:00+0000', tz='UTC'): [0.5423],
             Timestamp('2020-02-02 18:52:09+0000', tz='UTC'): [-0.1027],
             Timestamp('2020-02-02 21:26:46+0000', tz='UTC'): [0.4939],
             Timestamp('2020-02-03 18:35:43+0000', tz='UTC'): [0.8074],
             Timestamp('2020-02-03 22:45:00+0000', tz='UTC'): [-0.7845],
             Timestamp('2020-02-03 18:39:47+0000', tz='UTC'): [0.9081],
             Timestamp('2020-02-04 05:43:06+0000', tz='UTC'): [0.8402],
             Timestamp('2020-02-04 19:31:46+0000', tz='UTC'): [0.8316],
             ...}

I converted the Timestamp values to shortened string versions and made these values the indices for the dataframe.我将时间戳值转换为缩短的字符串版本,并将这些值作为 dataframe 的索引。
1

Here's the code I wrote to plot the data:这是我写给 plot 数据的代码:

fig_dims = (9, 6)
fig, ax = plt.subplots(figsize=fig_dims)
ax = sns.lineplot(x=dem_data.index, y='Score', data=dem_data, ax = ax)
ax.set_facecolor('white')
freq = int(10)
ax.set_xticklabels(concatenated.iloc[::freq].Date)
xtix = ax.get_xticks()
ax.set_xticks(xtix[::freq])
fig.autofmt_xdate()
plt.tight_layout()
plt.show()

And here's the resulting image.这是生成的图像。
2

A few things are strange about this.这有几件事很奇怪。

  1. The x-axis is labeled as 'fake-date', the name of my index column, which includes shortened string versions of the timestamps in the 'dates' column. x 轴标记为“假日期”,这是我的索引列的名称,其中包括“日期”列中时间戳的缩短字符串版本。 However, it displays the full timestamp, which I did not want.但是,它显示了我不想要的完整时间戳。
  2. The x-axis only displays dates between 02-02 and 02-12. x 轴仅显示 02-02 和 02-12 之间的日期。

How can I get the axis to display all dates (and in a way that is legible)?如何让轴显示所有日期(并且以清晰的方式)?

I recreated a portion of your DataFrame, and just plotted every other row by setting freq = int(2) .我重新创建了 DataFrame 的一部分,并通过设置freq = int(2)每隔一行绘制一次。 I formatted the date to not display time (but you can modify it to display whatever part of the date/time you want to keep), and also adjusted the angle of the x-axis labels to be 45 degrees.我将日期格式化为不显示时间(但您可以对其进行修改以显示您想要保留的日期/时间的任何部分),并将 x 轴标签的角度调整为 45 度。 An angle of 90 degrees can save room but may be harder to read. 90 度的角度可以节省空间,但可能更难阅读。

I can update my answer when I know where the variable concatenated comes from.当我知道 concatenated 变量的来源时,我可以更新我的答案。 For now I'll assume concatenated.iloc[::freq].Date would work similarly to dem_data.iloc[::freq].index , but something is different between the two if concatenated.iloc[::freq].Date only leads to the very beginning dates of your dem_data being plotted现在我假设concatenated.iloc[::freq].Date的工作方式与dem_data.iloc[::freq].index ,但是如果concatenated.iloc[::freq].Date两者之间有些不同导致绘制dem_data的最开始日期

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import seaborn as sns
from datetime import datetime

dem_data = pd.DataFrame({'Score':[0.5423,-0.1027,0.4939,0.8074,-0.7845,0.9081,0.8402,0.8316]})
dem_data.index = [pd.Timestamp('2020-02-02 22:27:00+0000'),
    pd.Timestamp('2020-02-02 18:52:09+0000'),
    pd.Timestamp('2020-02-02 21:26:46+0000'),
    pd.Timestamp('2020-02-03 18:35:43+0000'),
    pd.Timestamp('2020-02-03 22:45:00+0000'),
    pd.Timestamp('2020-02-03 18:39:47+0000'),
    pd.Timestamp('2020-02-04 05:43:06+0000'),
    pd.Timestamp('2020-02-04 19:31:46+0000')]

fig_dims = (9, 6)
fig, ax = plt.subplots(figsize=fig_dims)
ax = sns.lineplot(x=dem_data.index, y='Score', data=dem_data, ax = ax)
ax.set_facecolor('white')
freq = int(2)
ax.set_xticklabels(dem_data.iloc[::freq].index)
xtix = ax.get_xticks()
ax.set_xticks(xtix[::freq])

format_ymd = mdates.DateFormatter('%Y-%m-%d')
ax.xaxis.set_major_formatter(format_ymd)
plt.xticks(rotation=45)

plt.tight_layout()
plt.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM