简体   繁体   English

如何在python中创建堆叠线图?

[英]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.

相关问题 如何在 y 轴不基于计数的 Python 中创建堆叠条形图 plot - How can I create a Stacked Bar plot in Python where the y axis is NOT based on counts 如何对 Python 中堆叠的 plot 图形的值进行排序? - How can I sort values for stacked plot graph in Python? 如何创建27.000行的抖动图? - How can I create a jitter plot with 27.000 lines? 堆积线图上的平滑线python matplotlib - Smooth lines on stacked line plot python matplotlib 在 python 中绘制条形 plot 时,如何在 3 列的 pandas 数据框中将其堆叠为 2 列而不是为一列堆叠? - While plotting bar plot in python, how can I make it stacked for 2 columns and not stacked for one column in a pandas data frame of 3 columns? 我如何在 Python 的 for 循环中的同一张图上 plot 多条线? - How can I plot multiple lines on the same graph in a for loop in Python? 如何在一个 plot 中堆叠 dataframe 列中的 plot 多行? [Python] - How to plot many lines from stacked dataframe column in one plot? [python] 如何使用 Matplotlib 在 Python 中自动订购堆叠条 plot? - How can I automate the ordering of a Stacked Bar plot in Python using Matplotlib? 如何在 plot 中获得多行? - How can I get multiple lines in plot? 如何在一个范围内绘制多条线? - How can I plot multiple lines in a range?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM