python - Pandas/Matplotlib:如何更改 x 轴的比例
问题描述
我查看了一些关于绘图的教程。如果到目前为止我理解正确,我尝试通过 pandas 中的 matplotlib 模块创建一个绘图。为此,我创建了一个带有计数和时间跨度索引的 df:
我的数据框(我想分析每季度的报价量,因此我为第一季度创建了另一个 df 等等):
count_second_quarter = second_quarter.groupby("request_date").count()
count_second_quarter
request_date quote_id count
2019-04-01 94 94
2019-04-02 123 123
2019-04-03 423 423
2019-04-04 123 123
2019-04-05 312 312
... ... ...
然后我创建了第一个情节:
plt.plot(count_first_quarter['count'])
plt.plot(count_second_quarter['count'])
第一个问题,我想我正在混合 matplotlib 和 pandas 。如何更改 x 轴的比例?理想情况下,我想有一个情节,每个季度都有一条单独的线..
谢谢!
解决方案
我认为你需要的是.reset_index()
在绘图之前。这将使 x 轴从 0 开始到 89,您可以使用更新它plt.xticks()
plt.plot(count_first_quarter.reset_index()['count'])
plt.plot(count_second_quarter.reset_index()['count'])
# update xticks
plt.xticks(range(90), range(1,91)
# update xlabel
plt.xlabel('Days')
# show legend
plt.legend(['Q1', 'Q2'])
推荐阅读
- terraform - 将公共 IP 输出与服务器名称连接起来
- reactjs - 在 jsx 中使用 react 渲染嵌套对象
- waf - Waf 任务应该使用变体目录
- javascript - 为什么我的 React 路由器只加载默认路由?
- tensorflow - Tensorflow GPU 无法识别我的 GPU
- java - Hibernate Select distinct with order by another table
- javascript - 减少与过滤和映射
- intellij-idea - intellij 理念中的新 Kotlin 类
- facebook - 授权码未通过移动应用程序交换为令牌 facebook
- pip - 如何在我的 Mac 上修复与 127.0.0.1 的 pip 连接?