使用 pandas python 的线图 x 轴
Line plot x axis using pandas python
结果
year Month Min_days Avg_days Median_days Count
2015 1 9 12.56666666 10 4
2015 2 10 13.67678788 9 3
........................................................
2016 12 12 15.7889990 19 2
and so on...
我想创建一个线图绘制min_days、avg_days、median_days,按年月算。我该怎么做
到目前为止尝试过的代码
axes = result.plot.line(subplots=True)
type(axes)
这很好用,但我也得到了年份和月份的子图。我希望年份和月份在 x 轴上
已尝试的代码 2:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
result.groupby('Year').plot(x='Year', y='Median_days', ax=ax, legend=False)
这段代码的问题是它只有一年。我也需要月份(2015 年 1 月、2015 年 2 月等)。另一个问题是这给了我一个中位数的子图。如何为均值、最小值等添加子图
编辑:
P.S。此外,如果有人可以回答,以防月份 int 被月份名称替换(Jan、Feb 等,那就太好了)
一个解决方案可能是首先创建一个年月列:
result["year-month"] = pd.to_datetime(
result.year.astype(str) + "-" + result.Month.astype(str)
)
fig, ax = plt.subplots()
for col in ["Min_days", "Avg_days", "Median_days", "Count"]:
ax.plot(result["year-month"], result[col], label=col)
ax.legend(loc="best")
ax.tick_params(axis="x", rotation=30)
根据您提供给我们的有限数据,您将获得:
结果
year Month Min_days Avg_days Median_days Count
2015 1 9 12.56666666 10 4
2015 2 10 13.67678788 9 3
........................................................
2016 12 12 15.7889990 19 2
and so on...
我想创建一个线图绘制min_days、avg_days、median_days,按年月算。我该怎么做
到目前为止尝试过的代码
axes = result.plot.line(subplots=True)
type(axes)
这很好用,但我也得到了年份和月份的子图。我希望年份和月份在 x 轴上
已尝试的代码 2:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
result.groupby('Year').plot(x='Year', y='Median_days', ax=ax, legend=False)
这段代码的问题是它只有一年。我也需要月份(2015 年 1 月、2015 年 2 月等)。另一个问题是这给了我一个中位数的子图。如何为均值、最小值等添加子图
编辑: P.S。此外,如果有人可以回答,以防月份 int 被月份名称替换(Jan、Feb 等,那就太好了)
一个解决方案可能是首先创建一个年月列:
result["year-month"] = pd.to_datetime(
result.year.astype(str) + "-" + result.Month.astype(str)
)
fig, ax = plt.subplots()
for col in ["Min_days", "Avg_days", "Median_days", "Count"]:
ax.plot(result["year-month"], result[col], label=col)
ax.legend(loc="best")
ax.tick_params(axis="x", rotation=30)
根据您提供给我们的有限数据,您将获得: