How to create a historical timeline with Python

房东的猫 提交于 2021-02-18 18:20:26

问题


So I've seen a few answers on here that helped a bit, but my dataset is larger than the ones that have been answered previously. To give a sense of what I'm working with, here's a link to the full dataset. I've included a picture of one attempted solution, which was found at this link: Example Picture.

The issue is that 1. This is difficult to read and 2. I don't know how to flatten it out so that it looks like a traditional timeline. The issue becomes more apparent when I try and work with larger segments, such as this one, which is basically unreadable: It's basically unreadable. Here's the code I used to produce both of these (I just modified the included code in order to change which section of the overall dataset was used).

event = Xia['EnglishName']
begin = Xia['Start']
end = Xia['Finish']
length = Xia['Length']

plt.figure(figsize=(12,6))
plt.barh(range(len(begin)), (end-begin), .3, left=begin)
plt.tick_params(axis='both', which='major', labelsize=15)
plt.tick_params(axis='both', which='minor', labelsize=20)
plt.title('Xia Dynasty', fontsize = '25')
plt.xlabel('Year', fontsize = '20')
plt.yticks(range(len(begin)), "")
plt.xlim(-2250, -1750)
plt.ylim(-1,18)
for i in range(18):
    plt.text(begin.iloc[i] + length.iloc[i]/2, i+.25, event.iloc[i], ha='center', fontsize = '12') 

This code semi-works, but I'd prefer if the bars were either closer together or differently colored and all on the same y-value. I appreciate any and all help. I've been trying to figure this out for about two weeks now and am hitting a brick wall.


回答1:


Here is the code to replicate the original plot, something like this is expected in the question, would allow more time to answer the problem (as opposed to re-creating it).

import pandas as pd
import matplotlib.pyplot as plt

xia = pd.DataFrame([['Da Yu', -2207, -2197], 
                    ['Qi', -2197, -2188], 
                    ['Tai Kang', -2188, -2159]], 
                    columns=['EnglishName', 'Start', 'Finish']) 
event = xia['EnglishName']
begin = xia['Start']
end = xia['Finish']
length =  xia['Finish'] - xia['Start']


plt.figure(figsize=(12,6))
plt.barh(range(len(begin)), (end-begin), .3, left=begin)
plt.tick_params(axis='both', which='major', labelsize=15)
plt.tick_params(axis='both', which='minor', labelsize=20)
plt.title('Xia Dynasty', fontsize = '25')
plt.xlabel('Year', fontsize = '20')
plt.yticks(range(len(begin)), "")
plt.xlim(-2250, -1750)
plt.ylim(-1,18)
for i in range(3):
    plt.text(begin.iloc[i] + length.iloc[i]/2, 
             i+.25, event.iloc[i], 
             ha='center', fontsize = '12')

Grievances (to settle what to do next):

The issue is that 1. This is difficult to read and 2. I don't know how to flatten it out so that it looks like a traditional timeline. The issue becomes more apparent when I try and work with larger segments

I'd prefer if the bars were either closer together or differently colored and all on the same y-value.

The designs are rather difficult to specifiy in words. If you put on a single line (eg with this plt.barh([1 for _ in begin], (end-begin)-0.5, .3, left=begin)) the text would be overlapping even more unreadable.

Here is a small code to reproduce the horizontal timeline:

plt.figure(figsize=(4,2))
plt.ylim(0.5, 1.5)
plt.yticks(range(len(begin)), "")
# 0.25 is a stub, it controls for white separator
plt.barh([1 for _ in begin], (end-begin)-0.25, .3, left=begin)

But where would you expect to place the names?

If you want different colors, need some rule about what the colors are. A programmer would have said you need a better specification for the task.




回答2:


Something I did similar charting for a little sitcom succession diagram. The code is a bit naive (placed on github), but on encountering your question I was surprised this is still a problem for people doing similar visualisation. I was hoping there might be specialised library for historic charts.



来源:https://stackoverflow.com/questions/50883054/how-to-create-a-historical-timeline-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!