简体   繁体   English

使用Matplotlib进行Python绘图。 剧情没有出现

[英]Python Plotting using Matplotlib. Plot doesn't show up

I have vectors called v,time,volts. 我有向量,称为v,time,volts。 I used matplotlib to do subplotting so that i can have a better view of my results. 我使用matplotlib进行了子绘图,以便可以更好地查看结果。 But, there is no plot coming up I am not sure why? 但是,我不确定为什么没有? Does the size of vectors matter? 向量的大小重要吗? I have really big vectors. 我有很大的载体。

import matplotlib.pyplot as plt
plt.subplot(2, 1, 1)
spike_id = [i[0] for i in v]
spike_time = [i[1] for i in v]
plt.plot(spike_time, spike_id, ".")
plt.xlabel("Time(ms)")
plt.ylabel("NeuronID")


plt.subplot(2, 1, 2)
plt.plot(time,volts , "-")
plt.xlabel("Time (ms)")
plt.ylabel("Volts (mv)")


plt.show()

That's probably because you're not defining a figure to contain it. 那可能是因为您没有定义包含它的图形。

Here is a modification of your code: 这是您的代码的修改:

from matplotlib.pyplot import figure, plot, show, xlabel, ylabel

spike_id = [i[0] for i in v]
spike_time = [i[1] for i in v]

fig = figure(figsize=[16,9])

# Plot number 1:
fig.add_subplot(211)
plot(spike_time, spike_id, ".")
xlabel("Time(ms)")
ylabel("NeuronID")

# plot number 2:
fig.add_subplot(212)
plot(time,volts , "-")
xlabel("Time (ms)")
ylabel("Volts (mv)")

show()

You also asked if the size of the vector matters. 您还询问矢量的大小是否重要。

No. If it can be calculated in Python, it can be shown in MatPlotLib (which is mostly implemented in C). 否。如果可以用Python计算,则可以在MatPlotLib中显示(大多数情况下用C语言实现)。 It just might take some time if there are a few million processes. 如果有数百万个过程,可能只需要一些时间。 Also, consider calculating your spike_id and spike_time as generators, or NumPy array, to avoid unnecessary iterations. 另外,请考虑将您的spike_idspike_time计算为生成器或NumPy数组,以避免不必要的迭代。 Here is how: 方法如下:

Generators: 发电机:

spike_id = (i[0] for i in v)
spike_time = (i[1] for i in v)

NumPy: NumPy:

Use of NumPy allows for vectorisation, which would substantially optimise your programme and make it considerably faster; 使用NumPy可以进行矢量化,这将大大优化您的程序并使它更快地运行。 especially if you are handling large volumes of data. 特别是在处理大量数据时。

It also sounds like you're handling some signalling data (maybe EEG or EMG?). 听起来您正在处理一些信令数据(也许是EEG或EMG?)。 Anyhow, NumPy will provide you with variety of extremely useful tools for dealing with, and analysing such data. 无论如何,NumPy将为您提供各种非常有用的工具来处理和分析此类数据。

from numpy import array

v_array = array(v)

spike_id = v_array[:, 0]
spike_time = v_array[:, 1]

If you use an IPython / Jupyter notebook, you can embed the figure inside your notebook like so: 如果使用IPython / Jupyter笔记本,则可以将图形嵌入到笔记本中,如下所示:

from matplotlib.pyplot import figure, plot, xlabel, ylabel
%matplotlib inline

and thereby you can skip show() as it will no longer be necessary. 因此您可以跳过show()因为它不再需要。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM