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

查看:109
本文介绍了如何使用带有文本的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脚本中这些数据记录的顺序更改为指定顺序?我已经尝试将其工作到一个数组中.在此问题中:如何使用matplotlib控制条形图中的记录顺序?

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. in this question here: How do i control the sequence of records in a bar plot with matplotlib?

所以我从现在开始的下一个问题是,我可以做完全一样的事情,但是使用基于文本的排序吗?

So the next question i have from here on out is, can i do the exact same thing, but using a text-based sorting aaray?

所以我要输入的内容如下:

So what i would put in would be something like this:

posarray = (crp, 2), (cop,1)

edit:posarray将用输出数组中posarray [0]中的文本表示记录的位置. crp最终会出现在第二个位置,cop会出现在第一个位置,依此类推.

edit: The posarray would denote the position of the record with the text in posarray[0] in the outputarray. The crp would end up at the second position, cop at the first and so on.

它会带给我

cop   94.0528    313     441
cpr   225.172    726     747

最好的办法是使用某种插入,这样,如果我忘记在排序数组中提及某个项目,它将陷入最底端.

the best thing would be using some kind of insert, so that if i forget to mention an item in the sorting array it would sink to the bottom.

推荐答案

如您以前的,您可以使用一些索引技巧来实现所需的目标.

As described in the answer to your previous question, you can achieve what you want with some indexing trick.

让我们假设您具有数组的numpy表示形式,例如:

Let's assume that you have a numpy representation of your array as such:

a = array([('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)], 
          dtype=[('f0', '|S3'), ('f1', '<f8'), ('f2', '<i8'), ('f3', '<i8')])

您可以轻松获得第一列的列表:

You can easily get a list of your first column:

>>> fields = list(a['f0'])

现在,定义所需字段的列表,例如:

Now, define a list of the fields ordered as you want, for example:

>>> new_order = ['cpr', 'cop', 'ent', 'fsh', 'fsp', 'rho', 'pbp', 'sca']

通过使用index方法和一些列表理解,您可以获取fields列表中每个new_order项的位置:

You can get the position of each item of new_order in your fields list by using the index method and some list comprehension:

>>> indices = [fields.index(i) for i in new_order]
[1, 0, 2, 3, 4, 6, 5, 7]

(您可以理解为什么我们需要fields作为列表,因为数组没有index方法)

(You can understand why we needed fields to be a list, as an array doesn't have a index method)

就是这样.现在,您可以使用indices列表重新排列阵列:

That's it. You can now reorder your array with the indices list:

>>> a[indices]
array([('cpr', 225.172, 726, 747), ('cop', 94.0528, 313, 441),
       ('ent', 444.786, 926, 939), ('fsh', 256.038, 743, 754),
       ('fsp', 43.7764, 340, 356), ('rho', 426.497, 858, 886),
       ('pbp', 343.708, 825, 834), ('sca', 342.431, 427, 872)], 
      dtype=[('f0', '|S3'), ('f1', '<f8'), ('f2', '<i8'), ('f3', '<i8')])

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

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