您可以使用:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
#sample data
start = pd.to_datetime('2016-01-15')
rng = pd.date_range(start, periods=100)
data_df = pd.DataFrame({'date': rng, 'value': range(100)})
data_df.value = data_df.value * 15/data_df.date.dt.day
print (data_df)
date value
0 2016-01-15 0.000000
1 2016-01-16 0.937500
2 2016-01-17 1.764706
3 2016-01-18 2.500000
4 2016-01-19 3.157895
5 2016-01-20 3.750000
6 2016-01-21 4.285714
7 2016-01-22 4.772727
8 2016-01-23 5.217391
9 2016-01-24 5.625000
10 2016-01-25 6.000000
...
...
如果有必要將列date
到to_datetime
然後set_index
從date
柱:
data_df.date = pd.to_datetime(data_df.date)
data_df.set_index('date', inplace=True)
print (data_df)
value
date
2016-01-15 0.000000
2016-01-16 0.937500
2016-01-17 1.764706
2016-01-18 2.500000
2016-01-19 3.157895
2016-01-20 3.750000
2016-01-21 4.285714
2016-01-22 4.772727
2016-01-23 5.217391
2016-01-24 5.625000
2016-01-25 6.000000
...
...
情節Series
data_df['value']
通過plot
然後設置軸的形式x
:
ax = data_df['value'].plot()
ticklabels = data_df.index.strftime('%Y-%m-%d')
ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels))
plt.show()
完美的答案 - 謝謝 – jeangelj