[英]How can I create a plot of stacked lines in python?
I have fluorescence values over a period of 200 time steps for 300 individual cells.我有 300 个单个细胞超过 200 个时间步长的荧光值。 I want to plot them all on the same figure stacked one on top of another.我想将它们全部绘制在一个堆叠在另一个顶部的同一个图形上。 I don't want to plot a stacked area plot.我不想绘制堆积面积图。 The image below is what I'm looking for, but essentially it's each cell's data plotted on its own individual y-axis, with all cells having the same x-axis.下面的图片是我正在寻找的,但本质上它是每个单元格的数据绘制在它自己单独的 y 轴上,所有单元格都具有相同的 x 轴。
The main plotting tools (that work with pandas) in python are matplotlib (older) and seaborn (newer and a littler fancier). python 中的主要绘图工具(与 Pandas 一起使用)是 matplotlib(较旧的)和 seaborn(较新的和更小的爱好者)。
Looking at seaborn's docs ( https://seaborn.pydata.org/tutorial/axis_grids.html ) and a recipe page ( https://python-graph-gallery.com/122-multiple-lines-chart/ ) for multi-component composite plots, you can show your fluorescence data like this:查看 seaborn 的文档( https://seaborn.pydata.org/tutorial/axis_grids.html )和配方页面( https://python-graph-gallery.com/122-multiple-lines-chart/ )分量复合图,您可以像这样显示荧光数据:
# libraries
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# Data
df=pd.DataFrame({'x': range(100),
'trace1': np.random.randn(100),
'trace2': np.random.randn(100)+10,
'trace3': np.random.randn(100)+20})
# multiple line plot
plt.plot( 'x', 'trace1', data=df, marker='', color='black', linewidth=2)
plt.plot( 'x', 'trace2', data=df, marker='', color='black', linewidth=2)
plt.plot( 'x', 'trace3', data=df, marker='', color='black', linewidth=2, label="GluK1c")
plt.legend()
I'm not in love with that hack (adding numbers to each trace y-value) since there ought to be a way to offset your y-axis values within matplotlib
but I couldn't find that option when googling.我不喜欢这种hack (为每个跟踪 y 值添加数字),因为应该有一种方法可以在matplotlib
抵消您的 y 轴值,但我在谷歌搜索时找不到该选项。
In this case, because you want minimalist black line traces (what neuroscience journals expect), matplotlib and seaborn are comparable.在这种情况下,因为你想要极简的黑线痕迹(神经科学期刊所期望的),matplotlib 和 seaborn 是可比的。
For a plethora of legend position/formatting options, see https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.legend.html有关过多的图例位置/格式选项,请参阅https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.legend.html
I wish I had these tools when I worked in a lab.我希望我在实验室工作时拥有这些工具。 Would have been so much nicer than what I did back then.会比我当时所做的要好得多。
你可以这样做,例如:
your_df.plot(subplots=True, layout=(10,30));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.