我有一个包含多个列的数据框,从1971年到2012年,我有连续(年度)数据。之后我有一些说“2020”,2025年,2030年和2035年的“预测”值。数据索引frame是整数格式(每个日期),我尝试使用适当的模块将其转换为日期时间格式,但这仍然没有正确地将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()
答案 0 :(得分:1)
我试图弄清楚:
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()
希望这有帮助。