python - Mean line on top of bar plot with pandas and matplotlib -


i'm trying plot pandas dataframe, , add line show mean , median. can see below, i'm adding red line mean, doesn't show.

if try draw green line @ 5, shows @ x=190. apparently x values treated 0, 1, 2, ... rather 160, 165, 170, ...

how can draw lines x values match of x axis?

from jupyter:

dataframe plot

full code:

%matplotlib inline  pandas import series import matplotlib.pyplot plt  heights = series(     [165, 170, 195, 190, 170,       170, 185, 160, 170, 165,       185, 195, 185, 195, 200,       195, 185, 180, 185, 195],     name='heights' ) freq = heights.value_counts().sort_index()   freq_frame = freq.to_frame()  mean = heights.mean() median = heights.median()  freq_frame.plot.bar(legend=false)  plt.xlabel('height (cm)') plt.ylabel('count')  plt.axvline(mean, color='r', linestyle='--') plt.axvline(5, color='g', linestyle='--')  plt.show() 

use plt.bar(freq_frame.index,freq_frame['heights']) plot bar plot. bars @ freq_frame.index positions. pandas in-build bar function not allow specifying positions of bars, far can tell.

%matplotlib inline  pandas import series import matplotlib.pyplot plt  heights = series(     [165, 170, 195, 190, 170,       170, 185, 160, 170, 165,       185, 195, 185, 195, 200,       195, 185, 180, 185, 195],     name='heights' ) freq = heights.value_counts().sort_index()  freq_frame = freq.to_frame()  mean = heights.mean() median = heights.median()  plt.bar(freq_frame.index,freq_frame['heights'],         width=3,align='center')  plt.xlabel('height (cm)') plt.ylabel('count')  plt.axvline(mean, color='r', linestyle='--') plt.axvline(median, color='g', linestyle='--')  plt.show() 

bar plot


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -