python - 为什么python情节会延长到明年?如何停止最后的数据?(熊猫,matplotlib)
问题描述
两者都resample
导致groupby
生成的线图延长到明年 1 月。如何使用最后一个数据停止绘图以更清楚地查看我实际拥有的数据?
这是代码和结果图。
# import painkiller data
import pandas as pd
import matplotlib.pyplot as plt
#import plotly.plotly as py
df = pd.read_csv('/Users/user/Documents/health/PainOverTime.csv',delimiter=',',header=0)
# plot bar graph of date and painkiller amount
times = pd.to_datetime(df.loc[:,'Time'])
# raw plot of data
ts = pd.Series(df.loc[:,'acetaminophen'].values, index = times,
name = 'Painkiller over Time')
# combine data by day
#groupby method
ts1 = df.groupby(times.dt.date)['acetaminophen'].sum()
fig1 = ts1.plot()
# resample method
ts2 = ts.resample('D').sum()
plt.figure()
fig2 = ts2.plot()
解决方案
像这样的东西应该可以解决问题:
plt.xlim(None, last_index_you_want)
或者set_xlim
推荐阅读
- java - Apache GraphX 分区策略正在生成错误
- javascript - 为什么我在 Gatsby 构建中收到缺少分号的 CssSyntaxError?
- mule - 使用 dataweave 从三/四个 json 数组列表生成所有可能的组合
- r - 如何在读取 XLSB 文件时跳过空白行以节省读取时间
- c# - Visual Studio C# - 如何关闭自动完成(不是智能感知)?
- c# - 如何在 Zedgraph 中设置其他 yaxis 的字体和大小?
- google-sheets - 如何在 Sheet2 中显示我的函数,其中另一个 Sheet1 中的数据在谷歌电子表格中?
- node.js - 在多个 CPU 内核上部署 NextJs (ReactJS)
- java - 素数计算结果不一致
- javascript - 如果在 chrome 中突出显示,则 HTML 输入范围类型将无法通过拖动操作使用