简体   繁体   English

matplotlib 中的直方图,x 轴上的时间

[英]Histogram in matplotlib, time on x-Axis

I am new to matplotlib (1.3.1-2) and I cannot find a decent place to start.我是 matplotlib (1.3.1-2) 的新手,我找不到合适的起点。 I want to plot the distribution of points over time in a histogram with matplotlib.我想用 matplotlib 在直方图中 plot 点随时间的分布。

Basically I want to plot the cumulative sum of the occurrence of a date.基本上我想 plot 是某个日期发生的累计总和。

date
2011-12-13
2011-12-13
2013-11-01
2013-11-01
2013-06-04
2013-06-04
2014-01-01
...

That would make那会让

2011-12-13 -> 2 times
2013-11-01 -> 3 times
2013-06-04 -> 2 times
2014-01-01 -> once

Since there will be many points over many years, I want to set the start date on my x-Axis and the end date , and then mark n-time steps (ie 1 year steps) and finally decide how many bins there will be.由于多年来会有很多点,我想在我的x-Axis设置start dateend date ,然后标记n-time steps (即 1 年步),最后决定将有多少个bins

How would I achieve that?我将如何实现这一目标?

Matplotlib uses its own format for dates/times, but also provides simple functions to convert which are provided in the dates module. Matplotlib使用自己的日期/时间格式,但也提供了转换dates模块中提供的简单函数。 It also provides various Locators and Formatters that take care of placing the ticks on the axis and formatting the corresponding labels. 它还提供各种LocatorsFormatters ,负责将刻度线放在轴上并格式化相应的标签。 This should get you started: 这应该让你开始:

import random
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

# generate some random data (approximately over 5 years)
data = [float(random.randint(1271517521, 1429197513)) for _ in range(1000)]

# convert the epoch format to matplotlib date format 
mpl_data = mdates.epoch2num(data)

# plot it
fig, ax = plt.subplots(1,1)
ax.hist(mpl_data, bins=50, color='lightblue')
ax.xaxis.set_major_locator(mdates.YearLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m.%y'))
plt.show()

Result: 结果:

在此输入图像描述

To add to hitzg's answer, you can use AutoDateLocator and AutoDateFormatter to have matplotlib do the location and formatting for you: 要添加到hitzg的答案,您可以使用AutoDateLocatorAutoDateFormatter让matplotlib为您执行位置和格式设置:

locator = mdates.AutoDateLocator()
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(mdates.AutoDateFormatter(locator))

在此输入图像描述

Here is a more modern solution for matplotlib version 3.5.3.这是 matplotlib 版本 3.5.3 的更现代的解决方案。

Also, it explicitly specifies the min/max date instead of relying on min/max values derived from the data.此外,它明确指定最小/最大日期,而不是依赖于从数据派生的最小/最大值。

import random
from datetime import datetime, timedelta
import matplotlib.pyplot as plt

days = 365*3
start_date = datetime.now()
random_dates = [
    start_date + timedelta(days=int(random.random()*days))
    for _ in range(100)
]
end_date = start_date + timedelta(days=days)

fig, ax = plt.subplots(figsize=(5,3))
n, bins, patches = ax.hist(random_dates, bins=52, range=(start_date, end_date))
fig.autofmt_xdate()
plt.show()

x 轴上的时间直方图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM