简体   繁体   中英

Get matplotlib to plot x & y axis data

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()

Here is my output:

You would want to convert your strings to datetime, eg 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()

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: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM