Dae-Young Park
Dae-Young Park

Reputation: 11

Format X-axis in pandas and matplotlib

As you know by checking image below, X-axis contains hour, minute, second

I want to remove hour, minute, second in X-axis

How can I solve this problem?

df = pd.read_csv('./new_df2_count_per_date_{0}.csv'.format(str_day))
    df['Total_date'] = pd.to_datetime(df['Total_date'])
    df = df.set_index('Total_date')
    ax = df.plot.bar()
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
    plt.xlabel("Date")
    plt.ylabel("Count")
    plt.show()

enter image description here

Upvotes: 1

Views: 64

Answers (1)

jezrael
jezrael

Reputation: 862641

You can change index values in pandas side by convert datetimes to strigs in format YYYY-MM-DD:

ax = df.plot.bar()
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))

to:

ax = df.set_index(df.index.strftime('%Y-%m-%d')).plot.bar()

Upvotes: 1

Related Questions