如何使用matplotlib控制条形图中的记录顺序? [英] How do i control the sequence of records in a bar plot with matplotlib?

查看:85
本文介绍了如何使用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

我目前正在按照我设置示例的方式将这些图绘制在彼此之间. 如何将python脚本中这些数据记录的顺序更改为指定顺序?我已经尝试过将其工作到一个数组中.所以我会做

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)    

但是,这很麻烦,而且肯定不是实现此目的的最佳方法.我该如何使用Numpy或Matplotlib库做到这一点?

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

推荐答案

假定transformdatandarray,就像data一样,您可以使用索引技巧来获取数组视图,而不必复制它.例如:

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)]

此处的键是x[[1,3,0,2,4]],您可以在其中按所需顺序按照所需索引的列表进行索引.

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.

这篇关于如何使用matplotlib控制条形图中的记录顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆