Python 熊猫图二元矩阵

Python 熊猫图二元矩阵,python,matrix,pandas,matplotlib,plot,Python,Matrix,Pandas,Matplotlib,Plot,我在pandas中有一个dataframe(数据),它有一个datetimeindex(大约25000天的数据)和527列ID work_id_10 work_id_100 work_id_1007 work_id_1009 concert_date 1917-01-27 0 0 0 0 1917-01-28 0 0

我在pandas中有一个dataframe(数据),它有一个datetimeindex(大约25000天的数据)和527列ID

                  work_id_10  work_id_100  work_id_1007  work_id_1009
concert_date
1917-01-27             0            0             0             0
1917-01-28             0            0             0             0
1917-01-29             0            0             0             0
1917-01-30             0            0             0             0
1917-01-31             0            0             0             0
每个列ID用0(不存在)或1(存在)表示特定ID的存在或不存在。所以,基本上我有一个二进制值的矩阵

现在我想创建一个在x轴上有所有日期的绘图,并且对于每个列ID,存在作为点。我使用的是ipython

%matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set_yticklabels(data.index)
ax.set_xticklabels(data.columns)
plt.imshow/data, cmap='Greys', interpolation='none')
这给了我一个回忆:

Traceback (most recent call last):
  File "C:\Python27\Lib\lib-tk\Tkinter.py", line 1486, in __call__
    return self.func(*args)
  File "C:\Python27\Lib\lib-tk\Tkinter.py", line 533, in callit
    func(*args)
  File "C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", lin
e 365, in idle_draw
    self.draw()
  File "C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", lin
e 349, in draw
    FigureCanvasAgg.draw(self)
  File "C:\Python27\lib\site-packages\matplotlib\backends\backend_agg.py", line
469, in draw
    self.figure.draw(self.renderer)
  File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 59, in draw_wr
apper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\figure.py", line 1079, in draw
    func(*args)
  File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 59, in draw_wr
apper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 2092, in d
raw
    a.draw(renderer)
  File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 59, in draw_wr
apper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\image.py", line 367, in draw
    self._draw_unsampled_image(renderer, gc)
  File "C:\Python27\lib\site-packages\matplotlib\image.py", line 321, in _draw_u
nsampled_image
    self._get_unsampled_image(self._A, extent_in_ic, viewLim_in_ic)
  File "C:\Python27\lib\site-packages\matplotlib\image.py", line 219, in _get_un
sampled_image
    x = (x * 255).astype(np.uint8)
MemoryError
这是正确的方法吗?为什么我会得到一个MemoryError


谢谢大家!

正如我在一篇评论中提到的,您可能希望将数据分解成更直观易懂的块。以下是大小为527 x 2500的随机矩阵示例(1为蓝色,0为白色):

您的数据很可能有更多的结构,但可能仍然难以解释。您描述的矩阵是527 x 25000。您可以按年份(527x365)或十年(527x3650ish)显示,也可以四处玩,看看什么效果最好

以下是我将如何显示您的数据矩阵(这是一个小得多的集合):

这会打印我的假数据:

  concert_date  work_id_10  work_id_100  work_id_1007  work_id_1009  \
0   1917-01-27           1            1             0             0   
1   1917-01-28           0            0             1             0   
2   1917-01-29           0            1             1             0   
3   1917-01-30           1            0             0             0   
4   1917-01-31           0            0             0             0   
5   1917-02-01           0            0             1             1   

   work_id_1011  
0             0  
1             0  
2             1  
3             1  
4             1  
5             0  
然后获取标题和值:

id_labels = data.columns[1:]
# take the transpose since you want to see id on y-axis
id_matrix = np.array(data[id_labels].values, dtype=float).T
concert_dates = pd.to_datetime(data['concert_date'])
concert_dates = [d.date() for d in concert_dates]
现在使用imshow()绘制此图:


您可以四处玩,使它更漂亮,但这是一般的想法。

如果没有看到更多的代码和最少的数据示例,就很难给您提供有效的东西。假设你做了类似于图,ax=plt.subplot()的事情,那么你的
plt.plot(…)
你想像这样设置你的y轴刻度标签
ax.set_-yticklabels(something)
其中有一些是你的列名列表。谢谢@Scott为我指出了正确的方向。我没有任何其他代码,但我已经尝试过了,现在遇到了内存错误。我已相应地更新了我的初始问题。谢谢你再看一遍!把问题再分清楚。例如,注释掉标签行,它是否仍然断开?使用四乘四1和零的虚拟
数据
;还是坏了?等等。将最后一个示例行中的“/”替换为“(”。(我强烈建议使用脚本文件而不是解释器。其他文件不同,但在学习过程中,不要设置剪切和粘贴错误。)您是否正在尝试绘制25000 x 527矩阵?您可能希望将数据分为几年,这样您就有了365 x 527或类似的数据。
id_labels = data.columns[1:]
# take the transpose since you want to see id on y-axis
id_matrix = np.array(data[id_labels].values, dtype=float).T
concert_dates = pd.to_datetime(data['concert_date'])
concert_dates = [d.date() for d in concert_dates]
fig, ax = plt.subplots()
mat = ax.imshow(id_matrix, cmap='GnBu', interpolation='nearest')
plt.yticks(range(id_matrix.shape[0]), id_labels)
plt.xticks(range(id_matrix.shape[1]), concert_dates)
plt.xticks(rotation=30)
plt.xlabel('Concert Dates')

# this places 0 or 1 centered in the individual squares
for x in xrange(id_matrix.shape[0]):
    for y in xrange(id_matrix.shape[1]):
        ax.annotate(str(id_matrix[x, y])[0], xy=(y, x), 
                    horizontalalignment='center', verticalalignment='center')
plt.show()