【问题标题】:sort x-axis values in matplotlib histogram from lowest to highest value using python使用python将matplotlib直方图中的x轴值从最低到最高排序
【发布时间】:2018-02-25 19:29:20
【问题描述】:

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

根据这些数据:

{"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

【问题讨论】:

    标签: python matplotlib histogram data-visualization


    【解决方案1】:

    我认为既然您已经在使用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)
    

    【讨论】:

    • 但在真实数据集上,预处理很重要——因为它比玩具示例更大更复杂,因此——之后它不再是 JSON 格式。数据通过预处理管道后,有什么方法可以实现这一点?
    • 在这种情况下,您可以考虑构建一个临时数据框,按标签排序然后绘图。见编辑。
    猜你喜欢
    • 2019-10-10
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    • 2018-12-21
    相关资源
    最近更新 更多