Pandas crosstab plot – how to show values

Question:

I have the following dataset:

df = pd.DataFrame({'id':[1,2,3,4,5,6,7,8,9,10,11,12],
                   'city':['Pau','Pau','Pau','Pau','Pau','Pau','Lyon','Dax','Dax','Lyon','Lyon','Lyon'],
                   'type':['A','A','A','A','B','B','B','A','B','A','B','B'],
                   'val':[100,90,95,95,90,75,100,70,75,90,95,85]})

And I want to create a line plot where to show the percentage of type per grouped city, and to see the numerical value on each point on the line.

I have tried this:

pd.crosstab(df['city'],df['type'],normalize = 'index').plot(marker = 'x)
plt.show()

enter image description here

But I can’t figure how to put the data labels. I attached a pic of my intended plot:
enter image description here

Please, any help or guidance will be greatly appreciated.

Asked By: Alexis

||

Answers:

you could add them by using matplotlib’s plt.text

i think it’s best to separate the plot from the data-calculation…
it makes everything is much clearer!

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'id':[1,2,3,4,5,6,7,8,9,10,11,12],
                   'city':['Pau','Pau','Pau','Pau','Pau','Pau','Lyon','Dax','Dax','Lyon','Lyon','Lyon'],
                   'type':['A','A','A','A','B','B','B','A','B','A','B','B'],
                   'val':[100,90,95,95,90,75,100,70,75,90,95,85]})

ct = pd.crosstab(df['city'], df['type'], normalize='index')

ax = ct.plot(figsize=(9, 6))

for i, (x, y) in enumerate(ct.iterrows()):
    ax.text(x=i, y=y.A, s=f"{round(y.A*100)}%")
    ax.text(x=i, y=y.B, s=f"{round(y.B*100)}%")

enter image description here

Answered By: raphael
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.