Python 在存在间隙和缺少数据的情况下,使用matplotlib使用日期作为索引打印堆叠条形图

Python 在存在间隙和缺少数据的情况下,使用matplotlib使用日期作为索引打印堆叠条形图,python,datetime,matplotlib,pandas,Python,Datetime,Matplotlib,Pandas,我有一个数据框,其中包含了从1971年到2012年的连续(年度)数据。在那之后,我有一些关于2020年、2025年、2030年和2035年的“预测”值。数据帧的索引是整数格式(每个日期),我尝试使用适当的模块将其转换为日期-时间格式,但这仍然不能正确地在x轴上隔开日期(以显示实际的时间间隔)。下面是我一直在试验的代码: fig, ax = plt.subplots() # Set title ttl = "India's fuel mix (1971-2012)" # Set color tr

我有一个数据框,其中包含了从1971年到2012年的连续(年度)数据。在那之后,我有一些关于2020年、2025年、2030年和2035年的“预测”值。数据帧的索引是整数格式(每个日期),我尝试使用适当的模块将其转换为日期-时间格式,但这仍然不能正确地在x轴上隔开日期(以显示实际的时间间隔)。下面是我一直在试验的代码:

fig, ax = plt.subplots()
# Set title
ttl = "India's fuel mix (1971-2012)"

# Set color transparency (0: transparent; 1: solid)
a = 0.7


# Convert the index integer dates into actual date objects
new_fmt.index = [datetime.datetime(year=date, month=1, day=1) for date in new_fmt.index]


new_fmt.ix[:,['Coal', 'Oil', 'Gas', 'Biofuels', 'Nuclear',    'Hydro','Wind']].plot(ax=ax,kind='bar', stacked=True, title = ttl)
ax.grid(False)
xlab = 'Date (Fiscal Year)'
ylab = 'Electricity Generation (GWh)'
ax.set_title(ax.get_title(), fontsize=20, alpha=a)
ax.set_xlabel(xlab, fontsize=16, alpha=a)
ax.set_ylabel(ylab, fontsize=16, alpha=a)

# Tell matplotlib to interpret the x-axis values as dates
ax.xaxis_date()

# Make space for and rotate the x-axis tick labels
fig.autofmt_xdate()

我试图弄明白:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import datetime

# create data frame with random data (3 rows, 2 columns)
df = pd.DataFrame(np.random.randn(3,2))
# time index with missing years
t = [datetime.date(year=1971, month=12, day=31), datetime.date(year=1972, month=12, day=31), datetime.date(year=1980, month=12, day=31)]
df.index = t

# time index with all the years:
tnew = pd.date_range(datetime.date(year=1971, month=1, day=1),datetime.date(year=1981, month=1, day=1),freq="A")

# reindex data frame (missing years will be filled with NaN
df2 = df.reindex(tnew)

# replace NaN with 0
df2_zeros = df2.fillna(0)

# or interpolate
df2_interp = df2.interpolate()

# and plot
df2_interp.columns = ["coal","wind"]
df2_interp.plot(kind='bar', stacked=True)
plt.show()
希望这有帮助