Alice jinx
Alice jinx

Reputation: 625

Do not show time but only date in my Matplotlib

Example of wrong figure

Need help~! I have plotted a figure for my data. But when I check the figure, the x-axis shows time, but I do not want to show the specific time but only the month (for example, I only wish to show like: 2018-01, 2018-02, but not like 2018-01-01 00:00:00). I do not know what is wrong about my code and it does not show in my terminal run result. So confused, please if someone provides some suggestion. Many thanks. Code is below

data = pd.read_csv('micro_null_data.csv')

clean1_data = pd.value_counts(data['SAMPLE_NAME'])


Zone2 = data.loc[data['SAMPLE_NAME'] =='Zone 2', ['SAMPLED_DATE', 'FORMATTED_ENTRY']]
value_map = {
    'Not Detected': 1,
    'Detected': 0,
}

Zone2_table = pd.crosstab(pd.to_datetime(Zone2['SAMPLED_DATE']), Zone2['FORMATTED_ENTRY'])
Zone2_table.drop(Zone2_table.index[474], inplace=True)
print(Zone2_table)

Zone2_by_mongth = Zone2_table.resample('M').sum()
print(Zone2_by_mongth)

fig = plt.figure()
plt.style.use('seaborn')
Zone2_by_mongth.plot.bar()

plt.title('Zone 2')
plt.xticks(rotation=45)
plt.show()

Upvotes: 1

Views: 62

Answers (1)

moys
moys

Reputation: 8033

Try changing your Zone2_table line code to as below. Just this line.

Zone2_table = pd.crosstab(pd.to_datetime(Zone2['SAMPLED_DATE']).dt.strftime('%Y-%m'), Zone2['FORMATTED_ENTRY'])

Upvotes: 1

Related Questions