如何对用 Python matplotlib 编写的历史时间线进行排序? [英] How to sort a historical timeline written with Python matplotlib?

查看:24
本文介绍了如何对用 Python matplotlib 编写的历史时间线进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了显示一个非常简单的古代历史时间线(受 Wolfram Alpha 时间线的启发),我稍微修改了一个在 S.O.(

In order to display a very simple timeline of Ancient History (inspired by Wolfram Alpha timelines), I have slightly modified a small python program found on S.O. (How to draw a bar timeline with matplotlib?) :

import matplotlib.pyplot as plt
import numpy as np

event = np.array(['Antiquity','Egypt','W.R.Empire','E.R.Empire','Writing','C.Colomb','Middle Ages'])
begin = np.array([-3400,-3150,285,330,-3400,1492,476])
end = np.array([476,30,476,1453,-3300,1493,1492])

plt.barh(range(len(begin)), end-begin, left=begin, align='center')

plt.yticks(range(len(begin)), event)

plt.show()

How can I sort (ascending) the timeline by the beginning date ? The reason is that I wish to enter the data as they come (Minoans, Elam, etc...) without having to rearrange the arrays each time, which would be tedious.

This is not homework. I am simply a Python newbie, and I can't figure how to answer my own question…

解决方案

You may want to sort your values.

import matplotlib.pyplot as plt
import numpy as np

event = np.array(['Antiquity','Egypt','W.R.Empire','E.R.Empire','Writing','Middle Ages'])
begin = np.array([-3400,-3150,285,330,-3400,476])
end = np.array([476,30,476,1453,-3300,1493])

beg_sort = np.sort(begin)
end_sort = end[np.argsort(begin)]
evt_sort = event[np.argsort(begin)]

plt.barh(range(len(beg_sort)), end_sort-beg_sort, left=beg_sort, align='center')

plt.yticks(range(len(beg_sort)), evt_sort)

plt.show()

这篇关于如何对用 Python matplotlib 编写的历史时间线进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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