0

New to programming. Trying to plot a dataframe with pandas, but just the date label shows up with no ticks.

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

Picture of the Plot.

enter image description here

2
  • 1
    Try df.index = pd.to_datetime(df.index) before the df.plot(). You are trying to show categorical values the x axis instead of datetime. Commented Sep 19, 2018 at 15:20
  • stackoverflow.com/questions/25416955/… Commented Sep 19, 2018 at 15:37

1 Answer 1

0

It is duplicate Plot pandas dates in matplotlib

But the answer is as follows for your case

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.