【发布时间】:2016-01-16 15:29:02
【问题描述】:
我正在尝试绘制一个 Pandas DataFrame,并添加一条线来显示平均值和中位数。正如您在下面看到的,我为平均值添加了一条红线,但它没有显示出来。
如果我尝试在 5 处画一条绿线,它会在 x=190 处显示。所以显然 x 值被视为 0, 1, 2, ... 而不是 160, 165, 170, ...
如何绘制线条以使其 x 值与 x 轴的值匹配?
来自 Jupyter:
完整代码:
%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()
【问题讨论】:
-
您可以发布您正在绘制的数据样本吗?
-
完整来源,包括数据现已添加。
标签: python pandas matplotlib