如何使用 Seaborn 在 X 轴上将 int 绘制为日期时间?

要在 matplotlib 中使用 Seaborn 在 X 轴上将 int 绘制为日期时间,我们可以采取以下步骤

步骤

  • 设置图形大小并调整子图之间和周围的填充。

  • 创建一个包含三列的二维、大小可变、可能异构的表格数据的数据框df 。

  • 使用int创建一个计数图,即X 轴上的 dob。

  • int设置为X 轴上的日期时间标签。

  • 要显示图形,请使用Show()方法。

示例

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

# Set the figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Data frame with 3 columns
df = pd.DataFrame([
    [23, 'James', 1259969793926],
    [39, 'Jimmy', 1259969793927],
    [56, 'Jack', 1259969793929],
    [60, 'Tom', 1259969793928],
    [80, 'Tim', 1259969793939]
], columns=['marks', 'names', 'dob'])

# Create a Countplot with df
ax = sns.countplot(x="dob", data=df)

ax.set_xticklabels(
    [
        pd.to_datetime(tm, unit='ms').strftime('%Y-%m-%d %H:%M:%S')
        for tm in ax.get_xticks()
    ], rotation=45)

# Display the plot
plt.show()
输出结果

它将产生以下输出 -

猜你喜欢