简体   繁体   English

将时间序列数据绘制为堆积条 plot

[英]Plotting time series data as a stacked bar plot

I have some time series data with unequal time intervals.我有一些时间间隔不相等的时间序列数据。 At each time point, I have some value split across a number of different categories.在每个时间点,我都有一些不同类别的价值。 I would like to plot this data with the width of the bars representing the size of the time interval, and the bar being stacked to represent the value of each different category.我想 plot 这个数据用条形的宽度代表时间间隔的大小,而条形被堆叠来代表每个不同类别的值。

When plotted, the tops of the bars should look like a step function of the sum (across categories) of the data.绘制时,条形的顶部应该看起来像数据总和(跨类别)的一步 function。

Edit: added mwe.编辑:添加了 mwe。 For example, if I have 3 categories and 5 time points, my data would consist of a vector t and a matrix x as follows例如,如果我有 3 个类别和 5 个时间点,我的数据将由一个向量 t 和一个矩阵 x 组成,如下所示

t = [0, 0.1, 0.2, 0.5, 1]
x = [[1, 1, 1, 2, 3], [1, 0, 1, 3, 5], [2, 3, 4, 4, 2]]

Edit: This is in Python.编辑:这是在 Python 中。 I'd be happy with an answer using pandas, matplotlib, or any other.我会很高兴使用 pandas、matplotlib 或任何其他答案。

You can import your lists into a pandas dataframe to create a - you mentioned it already - step function:您可以将列表导入 pandas dataframe 以创建 - 您已经提到过 - 步骤 function:

import matplotlib.pyplot as plt
import pandas as pd

t = [0, 0.1, 0.2, 0.5, 1]
x = [[1, 1, 1, 2, 3], [1, 0, 1, 3, 5], [2, 3, 4, 4, 2]]

df = pd.DataFrame(x).T
df.index = t
df.columns = list("ABC")
df = df.cumsum(axis=1)

df.plot(drawstyle="steps-mid")
plt.xticks(df.index)
plt.ylim(0)

plt.show ()

Output: Output:
在此处输入图像描述

If you wanted to have filled areas, you might want to use fill_between instead:如果你想填充区域,你可能想使用 fill_between 代替:

import matplotlib.pyplot as plt
import pandas as pd

t = [0, 0.1, 0.2, 0.5, 1]
x = [[1, 1, 1, 2, 3], [1, 0, 1, 3, 5], [2, 3, 4, 4, 2]]

df = pd.DataFrame(x).T
df.index = t
df.columns = list("ABC")
df = df.cumsum(axis=1)

for y2, y1 in zip(df.columns[::-1], df.columns[1::-1]):
    plt.fill_between(df.index, df[y1], df[y2], step="mid", alpha=0.7, label=y2)
plt.fill_between(df.index, 0, df[y1], step="mid", alpha=0.7, label=y1)
plt.xticks(df.index)
plt.legend()

plt.show ()

Output: Output: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 python 中使用 matplotlib 对时间序列数据进行 plot 堆积条形图? - How to plot a stacked bar chart on time series data using matplotlib in python? 将分类变量绘制为堆积条形图 - Plotting categorical variable as stacked bar plot 绘制时间序列数据 - Plotting a time series data 在 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? 如何使用 plotly 低级接口 plot 一维数据(系列)的堆叠条形图? - How to plot a stacked bar graph for 1d data(series) using plotly low level interface? 在Python中使用时间序列数据创建水平条形图 - Creating Horizontal Bar Plot With Time-Series Data in Python 在时间序列图中绘制 2 个彩色标签 - Plotting 2 colored labels in time series plot 用熊猫重新采样的时间序列数据填充matplotlib中的条形图空白 - Fill bar plot gaps in matplotlib with pandas resampled time series data 穿越时空绘制电视剧:散点plot - Plotting TV series through time: scatter plot 随着时间的推移聚集堆积条形图python - Clustered Stacked bar plot over time python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM