如何在matplotlib和pandas中用日期绘制hexbin? [英] How to plot hexbin with dates in matplotlib and pandas?

查看:83
本文介绍了如何在matplotlib和pandas中用日期绘制hexbin?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制日期的十六进制,但是我采用的方法似乎存在问题:

I'm trying to plot a hexbin of dates, but there seem to be problems with the approach I take:

import pandas as pd
import datetime
import random
import matplotlib.pyplot as plt

def getdate():
    return datetime.datetime(2014,1,1)+datetime.timedelta(random.randint(0,100))

df=pd.DataFrame([(getdate(), getdate()) for i in range(100)], columns=list("ab"))
plt.hexbin(df.a, df.b)

有时会出现错误消息

packages\IPython\core\formatters.py:239: FormatterWarning: Exception in image/png  formatter: Python int too large to convert to C long
  FormatterWarning, <matplotlib.figure.Figure at 0x929bba8>

无论如何,该图不会显示在 IPython 中.

In any case, the plot doesn't show up in IPython.

您是否知道发生了什么以及如何获取以日期标记的六边形图?

Do you have an idea what's going on and how to get a hexbin plot labeled by dates?

推荐答案

您可以将日期转换为使用 xaxis_date() yaxis_date()

You can convert the date to numbers an use xaxis_date() and yaxis_date()

import pandas as pd
import datetime
import random
import matplotlib.pyplot as plt
import pylab as pl #**added**

def getdate():
    return datetime.datetime(2014,1,1)+datetime.timedelta(random.randint(0,100))

df=pd.DataFrame([(getdate(), getdate()) for i in range(10000)], columns=list("ab"))
ax = plt.gca()
ax.set_aspect("equal")
ax.hexbin(pl.date2num(df.a), pl.date2num(df.b), gridsize=20)
ax.xaxis_date()
ax.yaxis_date()
ax.xaxis.major.formatter.scaled[1.0] = "%Y-%m-%d"
ax.yaxis.major.formatter.scaled[1.0] = "%Y-%m-%d"
pl.xticks(rotation=45);

这里是输出:

这篇关于如何在matplotlib和pandas中用日期绘制hexbin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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