Home > Mobile >  Plot variant abundance vs. time (e,g, of mutant strains) in python
Plot variant abundance vs. time (e,g, of mutant strains) in python

Time:05-07

Given [absolute] numbers for each of a number of variants over time, I would like to produce a plot like the following: enter image description here

Is there a function for this in Python (preferably, but not necesarily, in matplotlib)? In general, do these sort of plots have a name? I had a hard time googling it even though Iv'e seen them many times in the past.

CodePudding user response:

Density Plot from seaborn package seems to be your case

import seaborn as sns
sns.kdeplot(...)

Stacked Chart from pygal

import pygal
pygal.StackedLine(...)

StackPlot from matplotlib

import matplotlib.pyplot as plt
plt.stackplot(...)

CodePudding user response:

This type of plot is called a "time stack plot" matplotlib has a built in function for building time stack plots. The syntax is:

matplotlib.pyplot.stackplot(x, y1, y2, ..., yn, colors=None, ...)

In your case x would be time (e.g. in months), y1,...,yn would be the abundance of each strain, and colors denotes a color map for y1,...,yn. (There are other optional arguments as well.) Here is an example of use:

import matplotlib.pyplot as plt

days = [1,2,3,4,5]

sleep = [6,7,5,8,6]
eat =   [2,2,1,2,1]
work =  [5,7,10,8,6]
exercise=  [3,3,0,1,3]


plt.plot([],[],color='green', label='sleep', linewidth=3)
plt.plot([],[],color='blue', label='eat', linewidth=3)
plt.plot([],[],color='red', label='work', linewidth=3)
plt.plot([],[],color='black', label='play', linewidth=3)

plt.stackplot(days, sleep, eat, work, exercise, colors=['green','blue','red','black'])

plt.xlabel('days')
plt.ylabel('activities')
plt.title('5 DAY ROUTINE')
plt.legend()
plt.show()

Which produces this plot: Example Plot

See this page for more info: https://www.educative.io/edpresso/how-to-create-a-stack-plot-using-matplotlib

  • Related