在直方图上绘制数据点

Plot data points on top of Histogram

我的直方图如下:

我还有一些数据点,我想在直方图上绘制一些值。

例如:

a点的RMSE = 0.99

b点的RMSE = 1.5

所以这两个点应该出现在直方图上,每个点应该有不同的颜色。

编辑:

这是我绘制直方图的代码:

bins = [0.6, 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2, 2.2, 2.4]
plt.hist(rms, bins=bins, rwidth= 1.2)
plt.xlabel('RMSE')
plt.ylabel('count')

plt.show()

如何向其中添加存储在某个变量中的新数据点。

  • 在兴趣点添加垂直线作为标记。
  • 另见 and How to draw vertical lines on a given plot in matplotlib
  • 测试于 python 3.8.11pandas 1.3.2matplotlib 3.4.3seaborn 0.11.2

导入和示例数据

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

tips = sns.load_dataset("tips")

情节

  • 这是用 plt.hist 绘制的,但也适用于 pandas.DataFrame.plotseaborn.histplot
    • tips.tip.plot(kind='hist', color='turquoise', ec='blue')
    • sns.histplot(data=tips, x='tip', bins=10, color='turquoise', ec='blue')
plt.figure(figsize=(10, 5))
plt.hist(x='tip', density=False, color='turquoise', ec='blue', data=tips)
plt.ylim(0, 80)
plt.xticks(range(11))

# add lines together
plt.vlines([2.6, 4.8], ymin=0, ymax=80, color='k', label='RMSE')

# add lines separately
plt.axvline(x=6, color='magenta', label='RMSE 1')
plt.axvline(x=8, color='gold', label='RMSE 2')

plt.legend()