【问题标题】:Plot Binary Matrix in Pandas在 Pandas 中绘制二进制矩阵
【发布时间】:2015-05-17 20:54:12
【问题描述】:

我在 pandas 中有一个数据框(数据),它有一个日期时间索引(大约 25.000 天的数据)和 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')

这给了我一个 MemoryError:

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?

谢谢!

【问题讨论】:

  • 如果没有看到更多代码和数据的最小示例,很难为您提供有效的东西。假设你做了类似fig, ax = plt.subplots() 然后你的plt.plot(...) 你想设置你的y 轴刻度标签像这样ax.set_yticklabels(something) 其中一些是你的列名列表。
  • 感谢@Scott 为我指明了正确的方向。我没有任何其他代码,但我已经尝试过了,现在遇到了 MemoryError。我已经相应地更新了我最初的问题。感谢您再次查看!
  • 进一步划分问题。例如,注释掉 ticklabel 行,它还会中断吗?使用 4×4 的 1 和 0 的虚拟 data 进行操作;还是坏了?等等。将最后一个示例行中的 '/' 替换为 '('。(我强烈建议使用脚本文件而不是解释器。其他情况有所不同,但是在您学习时,不要设置自己为 cut - 并粘贴错误。)
  • 您是否要绘制 25000 x 527 矩阵?您可能希望将数据分解为年份,以便拥有 365 x 527 或类似的尺寸。

标签: python matrix pandas matplotlib plot


【解决方案1】:

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

您的数据很可能具有更多结构,但仍可能难以解释。您所描述的矩阵是 527 x 25000。您可以按年份 (527 x 365) 或按十年 (527 x 3650ish) 显示,或者尝试看看哪种效果最好。

这是我将如何显示您的数据矩阵(这是针对更小的集合):

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime

data = pd.read_csv('concertdata.csv')
print data

这会打印出我的虚假数据:

  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() 进行绘制:

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()

你可以尝试让它更漂亮,但这是一般的想法。

【讨论】:

    猜你喜欢
    • 2020-10-22
    • 2022-10-15
    • 2019-01-15
    • 2015-06-08
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多