我正在尝试绘制日期的六边形,但我采取的方法似乎有问题:
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中。
您是否知道发生了什么以及如何获得按日期标记的hexbin图?
答案 0 :(得分:2)
您可以将日期转换为使用xaxis_date()
和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);
这是输出: