2016-01-16 10 views
8

Próbuję wykreślić Pandas DataFrame i dodać linię, aby pokazać średnią i medianę. Jak widać poniżej, dodaję czerwoną linię dla średniej, ale ona się nie pokazuje.Średnia linia na górze wykresu słupkowego z pandami i matplotlib

Jeśli spróbuję narysować zieloną linię w punkcie 5, pojawi się ona przy x = 190. Więc najwyraźniej wartości x są traktowane jako 0, 1, 2, ... zamiast 160, 165, 170, ...

Jak mogę narysować linie tak, aby ich wartości x były zgodne z osiami x?

Od Jupyter:

DataFrame plot

Pełny kod:

%matplotlib inline 

from pandas import Series 
import matplotlib.pyplot as 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() 
+0

zamieścić próbkę danych jesteś spisek? –

+0

Pełne źródło, w tym dane zostały dodane teraz. – oal

Odpowiedz

5

Zastosowanie plt.bar(freq_frame.index,freq_frame['Heights']) wykreślić pasku działki. Następnie słupki będą na pozycjach freq_frame.index. Funkcja paska postępu w paczce nie pozwala na określenie pozycji słupków, o ile mogę to stwierdzić.

%matplotlib inline 

from pandas import Series 
import matplotlib.pyplot as 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

Powiązane problemy