对于数据可视化还是很新的尝试,并试图制作一个简单的图表来监测我的减肥尝试。 Matplotlib似乎在绘制日期时做得很奇怪,并且不确定如何指定图形使用我的日期,而不是尝试始终从月初开始
import pandas as pd
import numpy as np
dfOrg = pd.DataFrame({"Date": ["2019-1-7", "2019-1-14", "2019-1-21","2019-1-28","2019-2-4","2019-2-11",
"2019-2-18", "2019-2-25", "2019-3-4"],
"Weight:": [204.3,201,202.5,200.7,201.5,202.5,199.5,200.8,201.5]})
dfOrg["Date"] = pd.to_datetime(dfOrg['Date'])
maxDT = dfOrg["Date"].max()
missing = {}
missing["Date"] = pd.date_range(start=maxDT, end='2019-10-07', freq='7D',closed="right")
missing["Weight"] = []
for i in missing["Date"]:
missing["Weight"].append(np.NaN)
missingDF = pd.DataFrame(missing)
df = pd.concat([dfOrg, missingDF], ignore_index=True)
df.drop(["Weight"], axis=1, inplace=True)
df.rename(columns={"Weight:": "Weight"}, inplace=True)
import matplotlib.pyplot as plt
import matplotlib.dates as dates
import datetime
formatter = dates.DateFormatter('%b %d')
plt.plot(df["Date"], df["Weight"], color="g")
plt.xticks(rotation=70)
plt.ylabel("LBS")
plt.title("Weight Loss Progress")
plt.axhline(linewidth=4, color='gold', y=170, label="Goal")
plt.ylim(160, 210)
plt.legend(bbox_to_anchor=[1, 0.5], loc='center left')
plt.gcf().axes[0].xaxis.set_major_formatter(formatter)
plt.show()
因此,理想情况下,图表应显示我的体重,日期从2019-1-7到2019-10-07,这是我的目标和目标线。相反,当我看到该图时显示