Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot timeline branches graphs in Python

I am trying to find out the name of these classes of graphs/charts and also what is the best way to plot them using Python and its associated libraries?

An example of the image is

Example of a hairy graph

like image 563
tsailun Avatar asked Sep 02 '25 10:09

tsailun


1 Answers

You just need to plot all your data using matplotlib making sure every following list starts where the first one ended in following year, here is an example:

import datetime
import matplotlib.pyplot as plt
import numpy as np


x = np.array([datetime.datetime(i, 1, 1) for i in range(2012,2018)])
y = np.array([1,10,14,23,22,15])

x2 = np.array([datetime.datetime(i, 1, 1) for i in range(2013,2018)])
y2 = np.array([10,54,39,62,45])

x3 = np.array([datetime.datetime(i, 1, 1) for i in range(2014,2018)])
y3 = np.array([14,43,27,65])

x4 = np.array([datetime.datetime(i, 1, 1) for i in range(2015,2018)])
y4 = np.array([23,39,85])

plt.plot(x,y)
plt.plot(x2,y2)
plt.plot(x3,y3)
plt.plot(x4,y4)

plt.tight_layout()

fig = plt.gcf()
fig.show()

Graphic produced

Notice y2 starts with y[1], y3 starts with y[2], and so on. Also notice how x, x2, ... are gradually changing to make the plot start at a certain year.

like image 53
Vinícius Figueiredo Avatar answered Sep 04 '25 22:09

Vinícius Figueiredo