Search code examples
numpymatplotlibrecord

How do i control the sequence of records in a bar plot with matplotlib?


I have a data file that consists of several data points, that i would like to plot in a particular order. Here is the example data file:

cop   94.0528    313     441
cpr   225.172    726     747
ent   444.786    926     939
fsh   256.038    743     754
fsp   43.7764    340     356
pbp   343.708    825     834
rho   426.497    858     886
sca   342.431    427     872

I am currently plotting those just below each other in the way i set the example. How can i change the order of those data records within my python script to a specified order? I already tried to work this into an array. So i would do

data=Numpy.genfromtxt(txt)
transformdata.append(2) # cpr to the first slot
transformdata.append(1) # cop to the second slot
outputarray.append(data[transformdata(1)]
outputarray.append(data[transformdata(2)]
pos = range(1,size(outputarray))
scatter(outputarray, pos)    

However this is messy and certainly not the best way to accomplish this. How would i do this using Numpy or Matplotlib libraries?


Solution

  • Assuming that transformdata is a ndarray, like data, you could use a trick of indexing to get a view of your array without having to copy it. For example:

    >>> x=np.array(zip("abcdef",range(5),np.random.rand(5)),dtype=[('',"|S1"),('',int),('',float)])
    >>> print x
    [('a', 0, 0.9818028495949546) ('b', 1, 0.08931155317859962)
     ('c', 2, 0.018796963462246796) ('d', 3, 0.275603618214155)
     ('e', 4, 0.6328806434997251)]
    >>> y = x[[1,3,0,2,4]]
    >>> print y
    [('b', 1, 0.08931155317859962) ('d', 3, 0.275603618214155)
     ('a', 0, 0.9818028495949546) ('c', 2, 0.018796963462246796)
     ('e', 4, 0.6328806434997251)]
    

    The key here is x[[1,3,0,2,4]], where you're indexing with a list of the indices you want in the order you need.