gpt4 book ai didi

python - Seaborn:线图看起来像一个步骤图

转载 作者:太空宇宙 更新时间:2023-11-04 02:19:13 27 4
gpt4 key购买 nike

我正在尝试使用以下代码在 Jupyter Notebook 中使用 Seaborn 绘制线图:

import pandas as pd
import seaborn as sns

# load csv
df=pd.read_csv('C:/Users/1/Desktop/graphs/seaborn/Data.csv')
# make sure date is in datetime
df['Date'] = pd.to_datetime(df['Date'], format='%m-%d-%y')
# generate graph
g = sns.lineplot(x="Date", y="Data", data=df)

plt.show()

然而,结果是这样的: enter image description here

而所需的输出如下(内置于 Excel 中): enter image description here

为了使情节更流畅,我缺少什么论据?

.csv 看起来像这样:

Date,Data
01-09-97,
01-10-97,
01-11-97,
01-12-97,
01-01-98,
01-02-98,
01-03-98,
01-04-98,
01-05-98,309.5
01-06-98,
...
01-07-14,44726.5
01-08-14,45735.1
01-09-14,47430
01-10-14,49887.7
01-11-14,51799.5
01-12-14,54258.1
01-01-15,52079.1
01-02-15,51110.6
01-03-15,49614.8
01-04-15,49989.2

最佳答案

简单的错误,你的日期格式不是%m-%d-%y,而是%d-%m-%y:

import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt

# load csv
df=pd.read_csv("test.csv")
# make sure date is in datetime
df['Date'] = pd.to_datetime(df['Date'], format='%d-%m-%y')
# generate graph
g = sns.lineplot(x="Date", y="Data", data=df)

plt.show()

输出:

enter image description here

关于python - Seaborn:线图看起来像一个步骤图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52004050/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com