I have a dataframe and want to make a line graph of the data but am confused on how to do so. My graph will be "Line A
M
(males)" with haps
('G', 'A', 'A', 'C', 'C')
over time.
X axis is year, Y axis is amount of that haps
per year, graphed using the numbers from 0
.
In each of the graphs there will be lines for the different haplotypes of that group, I am using one group to provide a simple example.
What should I be using to create the graphs, assuming there will be multiple Lines
and haps
in the actual dataset?
df
Line | Sex | haps | 0 | generation |
---|---|---|---|---|
A | M | ('G', 'A', 'A', 'C', 'C') | 25 | 2001 |
A | M | ('G', 'A', 'A', 'C', 'C') | 25 | 2002 |
A | M | ('G', 'A', 'A', 'C', 'C') | 5 | 2003 |
A | M | ('G', 'A', 'A', 'C', 'C') | 9 | 2004 |
A | M | ('G', 'A', 'A', 'C', 'C') | 15 | 2005 |
A | M | ('G', 'A', 'A', 'C', 'C') | 27 | 2006 |
I have tried sns.lineplot(data=clean, x='generation', y=0, hue='Line', marker='o')
and I get one graph with all of the Lines
but I want to seperate them out and not sure how to do so.
CodePudding user response:
Here's a suggestion.
Sample dataframe (values etc. might be nonsense):
from random import randint
columns = ['Line', 'Sex', 'haps', '0', 'generation']
haps = [('G', 'A', 'A', 'C', 'C'), ('G', 'A', 'A', 'A', 'C')]
data = [
[line, sex, hap, randint(5, 30), year]
for line in ['A', 'B'] for sex in ['M', 'F']
for hap in haps for year in range(2001, 2007)
]
Now you could do the following to show all in one plot:
import matplotlib.pyplot as plt
df_plt = df.pivot(index='generation', columns=['Line', 'Sex', 'haps'], values='0')
df_plt.plot()
plt.show()
# plt.savefig('plot_all.png')
Result is something like:
Or in groups of line
-sex
:
import matplotlib.pyplot as plt
for (line, sex), sdf in df.groupby(['Line', 'Sex'], as_index=False):
sdf_plt = sdf.pivot(index='generation', columns=['Sex', 'haps'], values='0')
sdf_plt.plot(title=f'Line: {line} - Sex: {sex}')
plt.show()
# plt.savefig(f'plot_{line}_{sex}.png')
Result is several plots like:
Is that what you are looking for?