Reputation: 27
For some reason I can't get my data plotted on the 'x'axis, and I can't get column name printed on the 'y' axis. I've tried a number of variations to the 'df.plot()' line over the past week without success.
Here is my code:
data = [['2018/10/11',1000],['2018/10/12',2000],['2018/10/13',1500]]
df = pd.DataFrame(data,columns=['Date','Amount'])
df.plot(x='Date', y='Amount')
plt.show()
Upvotes: 0
Views: 43
Reputation: 3043
You can try this:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data = [['2018/10/11',1000],['2018/10/12',2000],['2018/10/13',1500]]
df = pd.DataFrame(data, columns=['Date','Amount'])
xx = np.arange(len(df.values[:,0]))
# xx = [0 1 2]
yy = df.values[:,1]
# yy = [1000 2000 1500]
plt.scatter(x=xx, y=yy)
plt.xlabel('Dates')
plt.ylabel('Amount')
plt.tight_layout()
plt.show()
You will get the following plot:
Upvotes: 0
Reputation: 339122
You would want to convert your strings to datetime, e.g. via pd.to_datetime
.
import pandas as pd
import matplotlib.pyplot as plt
data = [['2018/10/11',1000],['2018/10/12',2000],['2018/10/13',1500]]
df = pd.DataFrame(data,columns=['Date','Amount'])
df["Date"] = pd.to_datetime(df["Date"], format="%Y/%m/%d")
df.plot(x='Date', y='Amount')
plt.show()
Upvotes: 1