0

Sorry - I admit that I don't know how to ask this question in a really clear way.

Both the documentation and previous stackoverflow questions have various points that seem in some way to touch on this problem but it's not organised or structured in any way that I can make sense of it with respect to what I'm trying to achieve, and the StackOverflow questions seem to be so narrow that it's hard to know if they even apply.

I have a script that generates scatter plots for data being generated by a hardware device. The plot data generation step cycles through different sources of data on the hardware to plot them on the scatter. (The original script was written by another person, no longer with our group). As I understand it, it's dumping the data into an anonymous scatter plot object - which presumably is the same one - because what it does (and what I want it to do) is collect the data from all the sources onto the same scatter plot.

But now I want to change the size of the labels along the axis - and all the on-line information I can find seems to suggest that you have to do this by creating a named instance of the plot and modifying it through the instance. However I don't know how to make sure that all the data from each successive source is placed into the same instance, so that when the final plot is displayed I get the scatter for all sources in one plot. How would I do this (and then modify the associated axis text)?

The actual plot lines are:

id_accumulator=0
for i in range(len(pfc)):
    data = numpy.asarray(pfc[i].getSpikes())
    if len(data) > 0:
       pylab.scatter(data[:,0], data[:,1] + id_accumulator, color='green', s=4) # s=1
    id_accumulator = id_accumulator + pfc[i].size
pylab.show()

Speaking more generally than this specific example, how would I set things up so that I can have, for X data sources, Y separate plots each displaying some subset of points [S[x][p]] where S is the set of data sources, x is a source and p is a data point, where I then set the labels for each axis to any size (and presumably font)?

1 Answers1

1

I have annotated that snippet of code with what each line is doing:

id_accumulator = 0   # set up counter
for i in range(len(pfc)):  # loop over input data array
    data = numpy.asarray(pfc[i].getSpikes())  # get data from hardware?
    if len(data) > 0:  # if we have some data
       # plot it via pylab.scatter -> pyplot.scatter -> pyplot.gca().scatter
       pylab.scatter(data[:,0], data[:,1] + id_accumulator, color='green', s=4) # s=1
    id_accumulator = id_accumulator + pfc[i].size   # increment 
pylab.show()  # after the loop, show the window using pylab.show -> pyplot.show

The original code has some pathological imports/naming schemes. See Which is the recommended way to plot: matplotlib or pylab? for an explaination of matplotlib vs pylab vs pyplot. The short of it is you should not be using pylab as an imported module (at a minimum, there are plans to get rid of it).

How I would re-write this code would be:

import matplotlib.pyplot as plt  # standard import
import numpy as np

fig, ax = plt.subplots(1, 1)     # make axes and figure objects
# do what ever axes level setup you want
ax.set_xlabel('text')

# iterate over input data
id_accumulator = 0
for _pfc in pfc:
    # get the data
    data = np.asarray(_pfc.getSpikes())
    if len(data) > 0:    # there should be a better check for this
        # plot the data
        ax.plot(data[:0], data[:1] + id_accumulator, 
                color='g', marker='o', linestyle='none')
    id_accumulator += _pcf.size   # update the offset count


# show the figure window

plt.show()

Unless you plan to vary either the color or size of your markers within a data set, you are better off using plot with no line.

If you want many axes to play with, just make more axes objects. If you want separate figure windows

fig1, ax1 = plt.subplots(1, 1)
fig2, ax2 = plt.subplots(1, 1)

or if you want many axes per figure:

fig, ax_lst = plt.subplots(2, 2)

which will give you a 2x2 grid. ax_lst is a 2x2 ndarray of the axes objects. From this you can do what ever permutation of data source <-> axes you want.

Community
  • 1
  • 1
tacaswell
  • 84,579
  • 22
  • 210
  • 199