Pandas没有标记日期记号

2024-05-16 18:39:07 发布

您现在位置:Python中文网/ 问答频道 /正文

编程新手。试图用pandas绘制一个数据帧,但是只有日期标签没有显示记号。你知道吗

import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight') 


#Read in Data set date as index
cattle_ppi = pd.read_csv('C:/Users/SkyLH/Documents/Cattle Forcast Model/Producer Price Index Corn.csv', index_col = 'DATE')
corn_ppi = pd.read_csv('C:/Users/SkyLH/Documents/Cattle Forcast Model/Producer Price Index Slaughter Cattle.csv', index_col = 'DATE')


#Combine Data
df = cattle_ppi.join(corn_ppi)
df.columns = ('Cattle PPI', 'Corn PPI')

print(df.head())
print(df.tail())

df.plot()
plt.show()
            Cattle PPI  Corn PPI
DATE                            
1971-01-01        63.4      44.8
1971-02-01        63.6      50.5
1971-03-01        62.0      50.4
1971-04-01        60.8      51.6
1971-05-01        60.2      52.0
            Cattle PPI  Corn PPI
DATE                            
2018-04-01       148.4     177.9
2018-05-01       154.2     171.7
2018-06-01       144.8     167.5
2018-07-01       132.1     168.5
2018-08-01       137.6     164.7

情节的图片。你知道吗

enter image description here


Tags: csvimportpandasdfdatadateindexas
1条回答
网友
1楼 · 发布于 2024-05-16 18:39:07

它是重复的Plot pandas dates in matplotlib

但对于你的情况,答案如下

import numpy as np
import pandas as pd
time = pd.date_range(start='1971-01-01', end='2018-08-01', periods=100)
a= np.random.randint(60, high=165, size=100)
b= np.random.randint(20, high=60, size=100)
df = pd.DataFrame({'time': time, 'a': a, 'b': b})
df['time'] = df['time'].dt.date

df.set_index(['time'],inplace=True)
df.plot()

enter image description here

相关问题 更多 >