【问题标题】:How to writte a raster plot?如何编写光栅图?
【发布时间】:2021-02-06 04:51:17
【问题描述】:

我在 google colab 上使用 pytorch。 我下面有个张量矩阵,这个是例子,实际矩阵大小大约是50个神经元,30000~50000次。

a= torch.tensor([[0., 0., 0., 0., 0.],
                 [0., 0., 0., 0., 1.],
                 [0., 1., 0., 1., 0.]])

每个值都是,

a= torch.tensor([[Neuron1(t=1), N2(t=1), N3(t=1), N4(t=1), N5(t=1)],
                 [N1(t=2), N2(t=2), N3(t=2), N4(t=2), N5(t=2)],
                 [N1(t=3), N2(t=3), N3(t=3), N4(t=3), N5(t=3)]])

1 表示神经元着火,0 表示不着火。
所以Neuron5(t=2)Neuron2(t=3)Neuron4(t=3) 正在开火。
然后,我想使用此矩阵制作如下所示的栅格图或散点图,
点表示放电神经元。

神经元数
1|
2| *
3|
4| *
5|__ *_____时间
1  2  3

执行此操作的最佳 Python 代码是什么? 我现在不知道。 感谢您的阅读。

【问题讨论】:

  • 我会说将结果转换为纯 numpy 数组,然后使用 pyplot 的 imshow matplotlib.org/api/_as_gen/… 绘制结果
  • 谢谢你,很抱歉迟到了,因为我写了那个代码。我会放在这里。

标签: python pytorch google-colaboratory


【解决方案1】:

您可以通过以下方式轻松完成:

import matplotlib.pyplot as plt
a= torch.tensor([[0., 0., 0., 0., 0.],
                 [0., 0., 0., 0., 1.],
                 [0., 1., 0., 1., 0.]],device='cuda')
plt.scatter(*torch.where(a.cpu()))

【讨论】:

    【解决方案2】:

    高分辨率版本,源自 Gil 先生的代码。

    a= ~~~a huge torch.tensor (50 neurons and 30,000 time)~~~
    
    fig = plt.figure(facecolor="w", figsize=(300,5))
    ax = fig.add_subplot(111)
    ax.scatter(*torch.where(a.cpu()),
                          s=0.05[![enter image description here][1]][1],
                          c="black",
                          linewidths="0")
    plt.savefig("rascatter.png",format="png", dpi=120)
    

    【讨论】:

      【解决方案3】:

      这里有两个答案。非常感谢大家。

      一个是 Gil Pinsky 先生,另一个是 tgrandje 先生。 吉尔先生的代码在上面。谢谢。

      这是我根据 tgrandje 先生的建议编写的代码。

      a=input_list
      b = a.to('cpu').detach().numpy().copy()
      c=b.T
      raster = plt.imshow(c,cmap="Greys",aspect=1)
      # plt.show
      plt.savefig("raster.png", format="png", dpi=900)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-16
        • 2015-09-03
        • 1970-01-01
        • 1970-01-01
        • 2021-06-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多