Reputation: 2140
According to the explanation why this question is different from this link
this link get the height from the diagram as far as I understood, but in my case I do not have this column numpatients6month
in the diagram at all, I just have that on the data frame.
So I have a bar chart. It contains two bar for each x-axis
in which each bar read from different data frame.
this is the code I am plotting the bar chart.
import seaborn as sns
import matplotlib.pyplot as plt
plt.rcParams['axes.prop_cycle'] = ("cycler('color', 'rg')")
dffinal['CI-noCI']='Cognitive Impairement'
nocidffinal['CI-noCI']='Non Cognitive Impairement'
res=pd.concat([dffinal,nocidffinal])
sns.barplot(x='6month',y='final-formula',data=res,hue='CI-noCI').set_title(fs)
plt.xticks(fontsize=8, rotation=45)
plt.show()
as you see there is two data frame
. I plot dffinal
with color green
and nocidffinal
with color red
.
This is the result of plot:
Some more explanation: dffinal
is based on (6month, final-formula)
nocidffinal
is also based on(6month,final-formula)
.
this is my nocidffinal
data frame:
6month final-formula numPatients6month
137797.0 1 0.035934 974
267492.0 2 0.021705 645
269542.0 3 0.022107 769
271950.0 4 0.020000 650
276638.0 5 0.015588 834
187719.0 6 0.019461 668
218512.0 7 0.011407 789
199830.0 8 0.008863 677
269469.0 9 0.003807 788
293390.0 10 0.009669 724
254783.0 11 0.012195 738
300974.0 12 0.009695 722
and dffinal
:
6month final-formula numPatients6month
166047.0 1 0.077941 680
82972.0 2 0.057208 437
107227.0 3 0.057348 558
111330.0 4 0.048387 434
95591.0 5 0.033708 534
95809.0 6 0.036117 443
98662.0 7 0.035524 563
192668.0 8 0.029979 467
89460.0 9 0.009709 515
192585.0 10 0.021654 508
184325.0 11 0.017274 521
85068.0 12 0.010438 479
As you see there is column numPatients6month
in this dataframeS which I would like to show on top of each bar.
I do NOT
want to change the barchart and group it based on this column, rather I want to just show this number as extra information to the user on top of each bar.
thanks for your time :)
Upvotes: 1
Views: 1553
Reputation: 2835
If you get your numPatients6month
columns in one iterable and in order they appear in chart then using the other stackoverflow answer (also in the docs here) you can place the text on top correctly.
I used code below (adapted from this SO answer). It combines multiple columns one row after another (i.e. will get all your numPatients6month
columns in the chart order)
vals = pd.concat([nocidffinal.numPatients6month, dffinal.numPatients6month], axis=1)
vals = vals.stack().reset_index(level=[0,1], drop=True)
This is my full code
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['axes.prop_cycle'] = ("cycler('color', 'rg')")
dffinal['CI-noCI']='Cognitive Impairement'
nocidffinal['CI-noCI']='Non Cognitive Impairement'
res=pd.concat([dffinal,nocidffinal])
# Copied to clipboard from SO question above
# Comment out if you already have your dataframes
nocidffinal = pd.read_clipboard().reset_index()
dffinal = pd.read_clipboard().reset_index()
# This will merge columns in order of the chart
vals = pd.concat([nocidffinal.numPatients6month, dffinal.numPatients6month], axis=1)
vals = vals.stack().reset_index(level=[0,1], drop=True)
# Plot the chart
ax = sns.barplot(x='6month', y='final-formula', data=res, hue='CI-noCI')
_ = plt.xticks(fontsize=8, rotation=45)
# Add the values on top of each correct bar
for idx, p in enumerate(ax.patches):
height = p.get_height()
ax.text(p.get_x()+p.get_width()/2.,
height + height*.01,
vals[idx],
ha="center")
Upvotes: 2