简体   繁体   English

在 python 中使用不同时间索引在同一 x 轴上绘制多个直方图

[英]Plotting multiple histogram on the same x-axis with different time indices in python

I have three lists of probabilities of three events (s, c, i) that I have obtained over time 1-1000 , ie, pr_s, pr_c, pr_i .我有三个事件的概率列表(s,c,i),我在1-1000时间内获得,即pr_s, pr_c, pr_i At time t , only one event is true.在时间t ,只有一个事件是真的。 I want to plot the values of all three events (pr_s, pr_c, pr_i) with different coloring on time-indexed x-axis but I am not sure how to do that.我想 plot 在时间索引 x 轴上使用不同颜色的所有三个事件(pr_s, pr_c, pr_i)的值,但我不知道该怎么做。 I want to know at time slot t , which event occurred and with what probability.我想知道在时间段t发生了哪个事件以及发生的概率。

As an examples, let's say the events occur from time t to t+5 as follow:例如,假设事件从时间tt+5发生如下:

pr_c(t) = 0.4; 
pr_i(t+1) = 0.3; 
pr_c(t+2) = 0.8; 
pr_s(t+3) = 0.65; 
pr_i(t+4) = 0.6; 
pr_s(t+5) = 0.9;

I would like to plot these probabilities on y-axis with time as x-axis.我想 plot 这些概率在 y 轴上以时间为 x 轴。 For now, I have a list of probabilities as they occur.现在,我有一个发生概率的列表。 For instance, pr_i = [0.3, 0.6] in above example that occured at time t+1 and t+4 .例如,上面示例中的pr_i = [0.3, 0.6]发生在时间t+1t+4

EDIT: It probably will require me to use a loop and if else statement for plotting but I am not sure if I can do that and how编辑:它可能需要我使用循环和 if else 语句进行绘图,但我不确定我是否可以这样做以及如何

It is still not entirely clear to me how you want to provide the connection between t and the events pr_s, pr_c, pr_i but here are two solutions.我仍然不完全清楚您希望如何提供t与事件pr_s, pr_c, pr_i之间的连接,但这里有两种解决方案。 One purely with base Python and matplotlib, the other with pandas/seaborn as higher libraries.一个纯粹使用基础 Python 和 matplotlib,另一个使用 pandas/seaborn 作为高级库。

from matplotlib import pyplot as plt

pr_i = [0.3, 0.6]
pr_c = [0.4, 0.8]
pr_s = [0.65, 0.9]

t = ["c", "i", "c", "s", "i", "s"]

event_dict = {"c": pr_c, "i": pr_i, "s": pr_s}

fig, (ax1, ax2) = plt.subplots(2, figsize=(6, 10))

for key in event_dict.keys():
    ind = [i for i, x in enumerate(t) if x==key]
    ax1.bar(ind, event_dict[key], label=key)
    
ax1.legend()
ax1.set_title("List solution")


import pandas as pd
import seaborn as sns

df = pd.DataFrame({"t": t, "vals": 0})

for key in event_dict.keys():
    df.loc[df.t==key, "vals"] = event_dict[key]

sns.barplot(x=df.index, y=df.vals, hue=df.t, dodge=False, ax=ax2, palette="bright")
ax2.set_title("Pandas/seaborn solution")

plt.show()

Sample output:样品 output:

![在此处输入图像描述

maybe this will help.Sounds like you need a line plot possibly.也许这会有所帮助。听起来你可能需要一条线 plot 。 You need to look up documentation for the rest.您需要查找 rest 的文档。 But this code will put the data into a dataframe then you will be able to plot like a time series.Seaborn a lot of features that might help you.但是此代码会将数据放入 dataframe 然后您将能够像时间序列一样 plot 。Seaborn 很多功能可能会对您有所帮助。

https://matplotlib.org/
https://stackoverflow.com/questions/28931224/adding-value-labels-on-a-matplotlib-bar-chart

import pandas as pd
import matplotlib.pyplot as plt

pr_s = [10,21,33]
pr_c = [20,11,16]
pr_i = [30,31,42]

df = pd.DataFrame(np.reshape(list(zip(pr_s, pr_c, pr_i)),(-1,3)))

plt.xticks(range(0, len(df.columns)))

plt.legend(loc='upper left')
plt.plot(df,marker='o')
plt.xlabel('Time')
plt.ylabel('Probabilities')

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM