
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
How to plot a kernel density plot of dates in Pandas using Matplotlib?
To plot a kernel density plot of dates in Pandas using Matplotlib, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Create a Pandas dataframe.
- Format the Pandas date column.
- Plot the Pandas date as kernel density estimate class by name.
- Set xtick labels using set_xticklabels() method.
- To display the figure, use show() method.
Example
import pandas as pd import numpy as np import datetime import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True dates = pd.date_range('2010-01-01', periods=31, freq='D') df = pd.DataFrame(np.random.choice(dates, 100), columns=['dates']) df['ordinal'] = [x.toordinal() for x in df.dates] ax = df['ordinal'].plot(kind='kde') x_ticks = ax.get_xticks() ax.set_xticks(x_ticks[::2]) xlabels = [datetime.datetime.fromordinal(int(x)) .strftime('%Y-%m-%d') for x in x_ticks[::2]] ax.set_xticklabels(xlabels) plt.show()
Output
- Related Articles
- How to plot a density map in Python Matplotlib?
- How Seaborn library used to display a kernel density estimation plot (joinplot) in Python?
- How to plot certain rows of a Pandas dataframe using Matplotlib?
- How to plot histograms from dataframes in Pandas using Matplotlib?
- How to plot a 3D density map in Python with Matplotlib?
- Plot multiple time-series DataFrames into a single plot using Pandas (Matplotlib)
- How to plot a Pandas Dataframe with Matplotlib?
- How to plot CSV data using Matplotlib and Pandas in Python?
- Frequency plot in Python/Pandas DataFrame using Matplotlib
- How to plot arbitrary markers on a Pandas data series using Matplotlib?
- How to plot data against specific dates on the X-axis using Matplotlib?
- How do I plot hatched bars using Pandas and Matplotlib?
- How can I make a scatter plot colored by density in Matplotlib?
- How to change the DPI of a Pandas Dataframe Plot in Matplotlib?
- How to a plot stem plot in Matplotlib Python?

Advertisements