Matplotlib Pandas 日期时间频率 [英] Matplotlib Pandas DateTime Frequency

查看:64
本文介绍了Matplotlib Pandas 日期时间频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 matplotlib 绘制一些数据,并希望减少显示的 DateTime x 轴刻度数.我能够使用 plt.locator 将 bin 数量减少一半,但日期时间与条形不对齐.有什么办法可以补救吗?我只想显示10个刻度中的5个.例如,仅以下日期时间显示在各自的绘制条下(2017-09-29 02:00、2017-09-29 04:00、2017-09-29 06:00、2017-09-29 08:00, 2017-09-29 10:00).

I am attempting to plot some data using matplotlib and would like to reduce the number of DateTime x-axis ticks displayed. I was able to use plt.locator to reduce the number of bins by half but the datetime does not align with the bars. Is there a way I can remedy this? I would like to have only 5 of the 10 ticks displayed. Where for example only the following datetimes are displayed under their respective plotted bars (2017-09-29 02:00, 2017-09-29 04:00, 2017-09-29 06:00, 2017-09-29 08:00, 2017-09-29 10:00).

下面是我的可复制代码和绘制的输出.

Below is my reproducible code and the plotted output.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame([{'DATETIME': '2017-09-29 01:00,', 'Population': 1000},
                   {'DATETIME': '2017-09-29 02:00,', 'Population': 3000},
                   {'DATETIME': '2017-09-29 03:00,', 'Population': 4000},
                   {'DATETIME': '2017-09-29 04:00,', 'Population': 5000},
                   {'DATETIME': '2017-09-29 05:00,', 'Population': 7000},
                   {'DATETIME': '2017-09-29 06:00,', 'Population': 6000},
                   {'DATETIME': '2017-09-29 07:00,', 'Population': 5000},
                   {'DATETIME': '2017-09-29 08:00,', 'Population': 4000},
                   {'DATETIME': '2017-09-29 09:00,', 'Population': 4000},
                   {'DATETIME': '2017-09-29 10:00,', 'Population': 4000}])

df.index = df['DATETIME']
df.index = (pd.to_datetime(df.index)).strftime("%m/%d %H:00")
df.Population.plot.bar()
plt.tick_params(axis='both', which='both', labelsize=7)
plt.locator_params(nbins=5)
plt.tight_layout()
plt.show()

推荐答案

首先让我解释一下为什么您选择的在定位器参数上设置 nbins 的方法失败了,尽管原则上可以预期该方法会完全按照自己的意愿做.
图中的条位于整数数字位置 0,1,2,...N-1.它们的标签被设置为固定的文本列表.因此,如果您选择只有5个刻度,则这些刻度已正确定位,但这些刻度被视为固定标签列表中的前5个元素.因此,第三个条具有第二个的标签,第五个条具有第三个的标签,依此类推.

Let me first explain why the method you chose setting nbins on the locator params fails, although in principle this method might be expected to do exactly what you want.
The bars in the plot are positionned at integer numeric locations 0,1,2,...N-1. Their labels are set as a fixed list of texts. Hence if you chose to only have 5 ticks, those ticks are correctly positionned, but the labels to those are taken to be the first 5 elements from the fixed label list. Therefore, the third bar has the label of the second, the fifth bar has the label of the third and so on.

知道了这一点,我们可以通过对当前标签进行切片来玩每隔一个柱线设置新刻度和刻度标签的技巧.您可以通过 plt.xticks() 获取当前刻度位置和标签,它返回 tickslabels 的元组.通过 :: 运算符
可以很容易地对列表进行切片例如 [0,1,2,3,4,5] [1 :: 2] 返回 [1,3,5] .因此:

Knowing this, we can play the trick of setting new ticks and ticklabels just at every second bar by slicing the current labels. You get the current tick positions and labels via plt.xticks(), which returns a tuple of ticks and labels. Slicing a list is easy via the :: operator,
e.g. [0,1,2,3,4,5][1::2] returns [1,3,5]. Hence:

plt.xticks(plt.xticks()[0][1::2], 
           plt.xticks()[1][1::2])

完整示例:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame([{'DATETIME': '2017-09-29 01:00,', 'Population': 1000},
                   {'DATETIME': '2017-09-29 02:00,', 'Population': 3000},
                   {'DATETIME': '2017-09-29 03:00,', 'Population': 4000},
                   {'DATETIME': '2017-09-29 04:00,', 'Population': 5000},
                   {'DATETIME': '2017-09-29 05:00,', 'Population': 7000},
                   {'DATETIME': '2017-09-29 06:00,', 'Population': 6000},
                   {'DATETIME': '2017-09-29 07:00,', 'Population': 5000},
                   {'DATETIME': '2017-09-29 08:00,', 'Population': 4000},
                   {'DATETIME': '2017-09-29 09:00,', 'Population': 4000},
                   {'DATETIME': '2017-09-29 10:00,', 'Population': 4000}])

df.index = df['DATETIME']
df.index = (pd.to_datetime(df.index)).strftime("%m/%d %H:00")
df.Population.plot.bar()
plt.tick_params(axis='both', which='both', labelsize=7)

plt.xticks(plt.xticks()[0][1::2], 
           plt.xticks()[1][1::2])

plt.tight_layout()
plt.show()

这篇关于Matplotlib Pandas 日期时间频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆