使用python将matplotlib直方图中的x轴值从最低到最高排序

Smatthewenglish

目前,我有一个脚本可以呈现以下直方图:

在此处输入图片说明

根据此数据:

{"first":"A","second":"1","third":"2"} 
{"first":"B","second":"1","third":"2"} 
{"first":"C","second":"2","third":"2"} 
{"first":"D","second":"3","third":"2"} 
{"first":"E","second":"3","third":"2"} 
{"first":"F","second":"3","third":"2"} 
{"first":"G","second":"3","third":"2"} 
{"first":"H","second":"4","third":"2"} 
{"first":"I","second":"4","third":"2"} 
{"first":"J","second":"0","third":"2"} 
{"first":"K","second":"0","third":"2"} 
{"first":"L","second":"0","third":"2"} 
{"first":"M","second":"0","third":"2"} 
{"first":"N","second":"0","third":"2"} 

这是呈现直方图数据的代码:

with open('toy_two.json', 'rb') as inpt:

    dict_hash_gas = list()
    for line in inpt:
        resource = json.loads(line)
        dict_hash_gas.append({resource['first']:resource['second']})

# Count up the values
counts = collections.Counter(v for d in dict_hash_gas for v in d.values())

counts = counts.most_common()

# Apply a threshold
threshold = 4275
counts = [list(group) for val, group in itertools.groupby(counts, lambda x: x[1] > threshold) if val]

print(counts)

它是这样绘制的:

# Transpose the data to get the x and y values
labels, values = zip(*counts[0])

indexes = np.arange(len(labels))
width = 1

plt.bar(indexes, values, width)
plt.xticks(indexes + width * 0.5, labels)
plt.show()

问题是,如何重组x轴,使它们从最低到最高排序,即

0, 1, 3, 4
CT Zhu

我认为,既然您已经在使用matplotlib,那么整理数据也将更有意义pandas

In [101]: JSON = '''[{"first":"A","second":"1","third":"2"}, 
   .....: {"first":"B","second":"1","third":"2"}, 
   .....: {"first":"C","second":"2","third":"2"}, 
   .....: {"first":"D","second":"3","third":"2"}, 
   .....: {"first":"E","second":"3","third":"2"}, 
   .....: {"first":"F","second":"3","third":"2"}, 
   .....: {"first":"G","second":"3","third":"2"}, 
   .....: {"first":"H","second":"4","third":"2"}, 
   .....: {"first":"I","second":"4","third":"2"}, 
   .....: {"first":"J","second":"0","third":"2"}, 
   .....: {"first":"K","second":"0","third":"2"}, 
   .....: {"first":"L","second":"0","third":"2"}, 
   .....: {"first":"M","second":"0","third":"2"}, 
   .....: {"first":"N","second":"0","third":"2"}]
   .....: '''

In [102]: df = pd.read_json(JSON)

In [103]: df
Out[103]: 
   first  second  third
0      A       1      2
1      B       1      2
2      C       2      2
3      D       3      2
4      E       3      2
5      F       3      2
6      G       3      2
7      H       4      2
8      I       4      2
9      J       0      2
10     K       0      2
11     L       0      2
12     M       0      2
13     N       0      2

In [104]: df.groupby('second').size().plot(kind='bar')
Out[104]: <matplotlib.axes._subplots.AxesSubplot at 0x1104eac10>

在此处输入图片说明

条形图将您的类别按正确的顺序排列。

但是,如果您只需要一种通用的方法来整理条形图,则可以只构造一个临时数据框,对其进行排序,然后绘制:

In [109]: pd.DataFrame({'Labels': labels, 
                        'Values': values}).sort_values(['Labels']).plot(kind='bar',
                                  x='Labels',
                                  y='Values',
                                  width=1.0)

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将数组从最低到最高排序 FORTRAN

如何将该Python程序的结果从最低到最高排序?

使用pandas.plot自定义python直方图中x轴的值

如何排序JSON数据-从最低到最高排序返回结果

使用Java中的方法按比例从最低到最高的数字对数组进行排序

是否可以将x轴刻度线与matplotlib直方图中的相应条对齐?

按从最低到最高的顺序对整数进行排序

更改Pandas / Matplotlib直方图中标签的x轴顺序?

R ggplot-将所有离散x值显示在直方图中的轴上

如何使用共享的第三列(从最低到最高)在第四列中对ID的组合进行排序?

MySql:高级排序第一行到最高然后最低到最高?

在水平 matplotlib 直方图上显示 x 轴值

matplotlib直方图中的非均匀轴

将最高值更改为最低值python

按tf-idf对TfidfVectorizer输出进行排序(从最低到最高,反之亦然)

从最低到最高排序一个数组(“ 1:40”,“ 2:30”,“ 6:33”)?

将文件中的最高到最低排序

Python使用matplotlib在x轴上显示特定值

python matplotlib:直方图中的标签

如何在直方图中找到最高峰的x值?

将分数降低到最低

单个链表,节点插入问题从最低到最高节点值

matplotlib中的直方图,x轴上的时间

matplotlib中的直方图的X轴乱序

Python Matplotlib X轴值分组

使用Matplotlib动画更新x轴值

matplotlib 轴值未排序

在matplotlib python中从x轴查找与y轴对应的值

在不使用sort()的情况下,将编号从最高到最低排序