Ruthvik Vaila
Ruthvik Vaila

Reputation: 537

Python Plotting using Matplotlib. Plot doesn't show up

I have vectors called v,time,volts. I used matplotlib to do subplotting so that i can have a better view of my results. 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()

Upvotes: 0

Views: 186

Answers (1)

Pouria
Pouria

Reputation: 1091

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). 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. Here is how:

Generators:

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

NumPy:

Use of NumPy allows for vectorisation, which would substantially optimise your programme and make it considerably faster; especially if you are handling large volumes of data.

It also sounds like you're handling some signalling data (maybe EEG or EMG?). Anyhow, NumPy will provide you with variety of extremely useful tools for dealing with, and analysing such data.

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:

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

and thereby you can skip show() as it will no longer be necessary.

Hope this helps.

Upvotes: 2

Related Questions