Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Seaborn을 사용하여 X축에서 int를 datetime으로 플롯하는 방법은 무엇입니까?

<시간/>

matplotlib에서 Seaborn을 사용하여 X축에서 int를 datetime으로 플롯하려면 다음 단계를 수행할 수 있습니다.

단계

  • Figure 크기를 설정하고 서브플롯 사이와 주변의 패딩을 조정합니다.

  • 데이터 프레임, df 만들기 , 3개의 열이 있는 2차원, 크기 변경 가능, 잠재적으로 이질적인 표 형식 데이터.

  • countplot 만들기 int, 즉, dob 사용 X축에.

  • 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()

출력

다음 출력을 생성합니다 -

Seaborn을 사용하여 X축에서 int를 datetime으로 플롯하는 방법은 무엇입니까?