- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to extract only the month and day from a datetime object in Python?
To extract only the month and day from a datetime object in Python, we can use the DateFormatter() class.
steps
Set the figure size and adjust the padding between and around the subplots.
Make a dataframe, df, of two-dimensional, size-mutable, potentially heterogeneous tabular data.
Create a figure and a set of subplots.
Plot the dataframe using plot() method.
Set the axis formatter, extract month and day.
To display the figure, use show() method.
Example
import numpy as np import pandas as pd from matplotlib import pyplot as plt, dates plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(dict(time=list(pd.date_range("2021-01-01 12:00:00", periods=10)), speed=np.linspace(1, 10, 10))) fig, ax = plt.subplots() ax.plot(df.time, df.speed) ax.xaxis.set_major_formatter(dates.DateFormatter('M:%m\nD:%d')) plt.show()
Output
- Related Articles
- Extract the Day / Month / Year from a Timestamp in PHP MySQL?
- Extract day, hour, minute, etc. from a datetime column in PostgreSQL?
- How to extract from datetime column in MySQL by comparing only date and ignoring whitespace?
- How to get only Date portion from DateTime object in C#?
- MongoDB query to search for Month and Day only?
- How to get a Date from year, month and day in Java?
- How to get last day of a month in Python?
- How to display first day and last day of the month from date records in MySQL?
- How can we extract the Year and Month from a date in MySQL?
- Compare only day and month with date field in MySQL?
- MongoDB query to convert the field value and create datetime day of month during projection?
- MongoDB query to search date records using only Month and Day
- Extract only characters from given string in Python
- MySQL query to extract only the day instead of entire date
- How to extract only the numbers from a text field in MySQL?

Advertisements