I'm trying to plot a Pandas DataFrame, and add a line to show the mean and median. As you can see below, I'm adding a red line for the mean, but it doesn't show.
If I try to draw a green line at 5, it shows at x=190. So apparently the x values are treated as 0, 1, 2, ... rather than 160, 165, 170, ...
How can I draw lines so that their x values match those of the x axis?
From Jupyter:
Full code:
%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()
Use plt.bar(freq_frame.index,freq_frame['Heights'])
to plot your bar plot. Then the bars will be at freq_frame.index
positions. Pandas in-build bar function does not allow for specifying positions of the bars, as far as I can tell.
%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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With