创建一个简单的时间线图。

huangapple go评论65阅读模式
英文:

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的annotatetext完成的操作代码:

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&#39;$t_0 = 2019$&#39;, r&#39;$t_0 = 2023$&#39;, r&#39;$t_0 = 2048$&#39;, r&#39;$t_0 = 2059$&#39;, r&#39;$t_0 = 2060$&#39;, r&#39;$t_0 = 2066$&#39;]
tickheight = 0.1
ax.annotate(&quot;&quot;, (4, 1), (5, 1), arrowprops={&#39;arrowstyle&#39;:&#39;&lt;-&#39;, &#39;shrinkA&#39;: 0, &#39;shrinkB&#39;: 0})
ax.plot([4, 4], [1 - tickheight, 1 + tickheight], c=&#39;k&#39;, lw=1)
ax.plot([5, 5], [1 - tickheight, 1 + tickheight], c=&#39;k&#39;, lw=1)
ax.text(4.5, 1.1, &#39;outflow&#39;, ha=&#39;center&#39;, va=&#39;baseline&#39;)
ax.annotate(&quot;&quot;, (2, 2), (4, 2), arrowprops={&#39;arrowstyle&#39;:&#39;&lt;-&#39;, &#39;shrinkA&#39;: 0, &#39;shrinkB&#39;: 0})
ax.plot([2, 2], [2 - tickheight, 2 + tickheight], c=&#39;k&#39;, lw=1)
ax.plot([4, 4], [2 - tickheight, 2 + tickheight], c=&#39;k&#39;, lw=1)
ax.text(3, 2.1, &#39;cancelling&#39;, ha=&#39;center&#39;, va=&#39;baseline&#39;)
ax.annotate(&quot;&quot;, (1, 3), (3, 3), arrowprops={&#39;arrowstyle&#39;:&#39;&lt;-&#39;, &#39;shrinkA&#39;: 0, &#39;shrinkB&#39;: 0})
ax.plot([1, 1], [3 - tickheight, 3 + tickheight], c=&#39;k&#39;, lw=1)
ax.plot([3, 3], [3 - tickheight, 3 + tickheight], c=&#39;k&#39;, lw=1)
ax.text(2, 3.1, &#39;inflow&#39;, ha=&#39;center&#39;, va=&#39;baseline&#39;)
ax.spines[&#39;top&#39;].set_visible(False)
ax.spines[&#39;left&#39;].set_visible(False)
ax.spines[&#39;right&#39;].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:

创建一个简单的时间线图。

huangapple
  • 本文由 发表于 2023年5月8日 00:26:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195055.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定