这里提出的第一个问题。我希望我做得对。请让我知道我是否应该提供更多信息,或者如果我的问题不清楚。
我正在尝试使用python(pandas、seaborn、matplotlib(来生成一个折线图。经过多次反复播放后,脚本运行,但输出看起来不对劲。原始数据比我的脚本输出中显示的要平滑得多。我将它与excel中生成的图形进行了比较,后者看起来更逼真。我真的不明白我的剧本出了什么问题。
该数据集是";RBD棕榈油FOB马来西亚";。第1列为日期,第2列为价格。数据保存为CSV。我唯一能想到的问题是市场休市的日子不多了。这会是问题所在吗?
代码如下:
import pandas as pd
df = pd.read_csv("/Users/michaelkingston/Desktop/RBDP2015_20.csv")
print(df)
df.info()
df['ds']= pd.to_datetime(df['ds'])
df.info()
import seaborn as sns
sns.lineplot(x="ds",
y="Y",
data=df)
import matplotlib.pyplot as plt
plt.show()
这是excel输出的图像在此处输入图像描述
这是python的输出在此处输入图像描述
print(df.head)
<bound method NDFrame.head of ds Y
0 2015-02-01 673
1 2015-05-01 663
2 2015-06-01 663
3 2015-07-01 668
4 2015-08-01 680
... ... ...
1419 2020-10-23 775
1420 2020-10-26 805
1421 2020-10-27 810
1422 2020-10-28 828
1423 2020-10-29 825
根据数据,我觉得你的输出是正确的,它与你在excel中得到的不同,因为python的绘图被压缩到默认的绘图大小,你可以调整绘图大小以拉伸它,就像在excel中一样。另一点是:如果你需要excel中的x轴标签,那么最好不要将日期列转换为日期时间对象,因为在绘制日期时间对象时,matplotlib只显示几个日期以确保可读性。
以下是您可以尝试的:
import matplotlib.pyplot as plt
#converting the column ds to string type
df['ds'] = df['ds'].astype(str)
#fixing a big figure size for the plot
plt.figure(figsize=(25,10))
sns.lineplot(x="ds", y="Y", data=df)
#for making the x-axis labels rotate by 90 degree to avoid cluttering of tick labels
plt.xticks(rotation=90)
plt.show()