0

I have three lists of probabilities of three events (s, c, i) that I have obtained over time 1-1000, i.e., pr_s, pr_c, pr_i. At time t, only one event is true. 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. I want to know at time slot t, which event occurred and with what probability.

As an examples, let's say the events occur from time t to t+5 as follow:

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. 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.

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

  • Try this code https://stackoverflow.com/questions/6871201/plot-two-histograms-on-single-chart-with-matplotlib – Sam Dec 12 '20 at 18:08
  • This is not what I am looking for. It is simple as both histogram have same x-axis and at every index, both histograms produce some value. In my case, only one of the above events can occur at one index of x-axis to maybe there is a way to keep the index value and plot on that time only – M. Awais Jadoon Dec 12 '20 at 18:19
  • @Mr.T, I have edited the question again. Let me know if its ok and understandable – M. Awais Jadoon Dec 13 '20 at 14:06
  • Thank you for editing the question. Better now but I still don't know how the order is established. In your `pr_i` example, how do you determine that this refers to `t+1` and `t+4`? And will `t` be provided to the script, or should the plot just be index-based (i.e., vs. 0 to 5 in your example)? P.S.: Just to make sure - these are indeed lists, no pandas or numpy involved, yes? – Mr. T Dec 13 '20 at 14:10
  • Yes, these are just the lists. I can track `t` and add it to the list so it is provided too. The plot should be index-based as you said on x-axis from 0 to 5. The order of an event happening is random, so we only know at which `t` a particular event happened – M. Awais Jadoon Dec 13 '20 at 14:53
  • But how do you want to provide `t` to matplotlib? Is there a list of events like `t=["c", "i", "c", "s", "i", "s"]`? Or are there corresponding lists like `t_i = [1, 4]`? We don't know your data structure. – Mr. T Dec 13 '20 at 15:10
  • There is a list of events like `t=["c", "i", "c", "s", "i", "s"]`. I can track events like this and corresponding time too. I can also modify the structure myself because I am producing this data myself if there is any better way to plot it. – M. Awais Jadoon Dec 13 '20 at 15:20
  • The list would contain `[t_index, event, pr_event]` – M. Awais Jadoon Dec 13 '20 at 16:01

2 Answers2

1

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. One purely with base Python and matplotlib, the other with pandas/seaborn as higher libraries.

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:

![enter image description here

Mr. T
  • 11,960
  • 10
  • 32
  • 54
0

maybe this will help.Sounds like you need a line plot possibly. You need to look up documentation for the 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.

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')
Jean S
  • 13
  • 5
  • Hi, thank you I could get some sense from this answer but, `pr_s`, `pr_c`, `pr_i` are the probabilities and they could be as follow: ```pr_s = [0.5,0.6,0.8] pr_c = [0.4,0.58,0.2] pr_i = [0.5,0.2,0.3]``` The x-axis is time but at time `pr_s` occurs, `pr_i` and `pr_c` cannot occur and vice versa. I am having problems how to plot these probabilities on x-axis exactly at time they occur at. In the solution you have proposed, all of them are occuring at 3 same points on x-axis. I hope my explanation is ok! – M. Awais Jadoon Dec 12 '20 at 21:44