首页 > 解决方案 > 在直方图顶部绘制数据点

问题描述

我的直方图如下:

在此处输入图像描述

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

例如:

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()

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

标签: pythonpython-3.xmatplotlibhistogramseaborn

解决方案


导入和示例数据

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()

在此处输入图像描述


推荐阅读