KansaiRobot
KansaiRobot

Reputation: 10032

matplotlib plot for probabilities

What is an adequate plot type for probabilities? For example if I have the following data:

t p1 p2 p3 p4
1 0 0 0 1
2 0.1 0 0 0.9
3 0.2 0 0 0.8
4 0.2 0.3 0 0.5

You can see that the values (other than t) sum 1.

I seem to remember there is some kind of bar graph that sum 1 but with the divisions of different colors.

Something like plot

Upvotes: 0

Views: 265

Answers (1)

a_guest
a_guest

Reputation: 36329

You can use pandas to load the data and then create the following plot with matplotlib:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd


df = pd.read_csv('test.csv', index_col=0)

samples = df.index.astype(str)
bottom = np.zeros(len(df))
width = 0.5

fig, ax = plt.subplots()
ax.set(xlabel='Samples', ylabel='Probability')
for label, probabilities in df.transpose().iterrows():
    ax.bar(samples, probabilities, width, bottom=bottom, label=label)
    bottom += probabilities
ax.legend(title='Classes')
plt.show()

Example plot

Upvotes: 1

Related Questions