英文:
How to create a simple timeline plot
问题
我正在尝试在Python中创建一个时间轴。然而,我以前从未做过这个,搜索答案至今没有帮助我太多。
基本上,我正在尝试重新创建类似于下面发布的图像的东西。
有人有任何相关的资源可以提供,可以帮助解决这个问题吗?或者,有人知道如何编写类似于图像中显示的内容吗?
英文:
I am trying to create a timeline in python. However, I've never done this before and searching for answers haven't helped me much so far.
Basically, I am trying to recreate something similar to the image posted below.
Does anyone have any relevant sources they could provide that would help with this? Alternatively, does anyone know how to code something similar to what is shown in the image?
答案1
得分: 2
以下是可以使用matplotlib的annotate
和text
完成的操作代码:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(7, 3))
ax = fig.add_subplot()
xvalues = [0, 1, 2, 3, 4, 5]
xlabels = [r'$t_0 = 2019$', r'$t_0 = 2023$', r'$t_0 = 2048$', r'$t_0 = 2059$', r'$t_0 = 2060$', r'$t_0 = 2066$']
tickheight = 0.1
ax.annotate("", (4, 1), (5, 1), arrowprops={'arrowstyle':'<-', 'shrinkA': 0, 'shrinkB': 0})
ax.plot([4, 4], [1 - tickheight, 1 + tickheight], c='k', lw=1)
ax.plot([5, 5], [1 - tickheight, 1 + tickheight], c='k', lw=1)
ax.text(4.5, 1.1, 'outflow', ha='center', va='baseline')
ax.annotate("", (2, 2), (4, 2), arrowprops={'arrowstyle':'<-', 'shrinkA': 0, 'shrinkB': 0})
ax.plot([2, 2], [2 - tickheight, 2 + tickheight], c='k', lw=1)
ax.plot([4, 4], [2 - tickheight, 2 + tickheight], c='k', lw=1)
ax.text(3, 2.1, 'cancelling', ha='center', va='baseline')
ax.annotate("", (1, 3), (3, 3), arrowprops={'arrowstyle':'<-', 'shrinkA': 0, 'shrinkB': 0})
ax.plot([1, 1], [3 - tickheight, 3 + tickheight], c='k', lw=1)
ax.plot([3, 3], [3 - tickheight, 3 + tickheight], c='k', lw=1)
ax.text(2, 3.1, 'inflow', ha='center', va='baseline')
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.set_xticks(xvalues)
ax.set_xticklabels(xlabels)
plt.xlim(-0.1, 5.1)
plt.ylim(0, 3.5)
plt.yticks([])
plt.tight_layout()
plt.show()
英文:
Here is what can be done with matplotlib annotate
+ text
:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(7, 3))
ax = fig.add_subplot()
xvalues = [0, 1, 2, 3, 4, 5]
xlabels = [r'$t_0 = 2019$', r'$t_0 = 2023$', r'$t_0 = 2048$', r'$t_0 = 2059$', r'$t_0 = 2060$', r'$t_0 = 2066$']
tickheight = 0.1
ax.annotate("", (4, 1), (5, 1), arrowprops={'arrowstyle':'<-', 'shrinkA': 0, 'shrinkB': 0})
ax.plot([4, 4], [1 - tickheight, 1 + tickheight], c='k', lw=1)
ax.plot([5, 5], [1 - tickheight, 1 + tickheight], c='k', lw=1)
ax.text(4.5, 1.1, 'outflow', ha='center', va='baseline')
ax.annotate("", (2, 2), (4, 2), arrowprops={'arrowstyle':'<-', 'shrinkA': 0, 'shrinkB': 0})
ax.plot([2, 2], [2 - tickheight, 2 + tickheight], c='k', lw=1)
ax.plot([4, 4], [2 - tickheight, 2 + tickheight], c='k', lw=1)
ax.text(3, 2.1, 'cancelling', ha='center', va='baseline')
ax.annotate("", (1, 3), (3, 3), arrowprops={'arrowstyle':'<-', 'shrinkA': 0, 'shrinkB': 0})
ax.plot([1, 1], [3 - tickheight, 3 + tickheight], c='k', lw=1)
ax.plot([3, 3], [3 - tickheight, 3 + tickheight], c='k', lw=1)
ax.text(2, 3.1, 'inflow', ha='center', va='baseline')
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.set_xticks(xvalues)
ax.set_xticklabels(xlabels)
plt.xlim(-0.1, 5.1)
plt.ylim(0, 3.5)
plt.yticks([])
plt.tight_layout()
plt.show()
Result looks like this:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论