获取matplotlib以绘制x和y轴数据

时间:2018-11-09 16:42:49

标签: python matplotlib

由于某种原因,我无法在“ x”轴上绘制数据,也无法在“ y”轴上打印列名称。在过去的一周中,我尝试对'df.plot()'行进行了多种变体,但均未成功。

这是我的代码:

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:

2 个答案:

答案 0 :(得分:1)

您希望将字符串转换为日期时间,例如通过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()

答案 1 :(得分:0)

您可以尝试以下方法:

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

您将获得以下图解:enter image description here