【问题标题】:Plot data points on top of Histogram在直方图顶部绘制数据点
【发布时间】:2020-09-27 18:13:32
【问题描述】:

我的直方图如下:

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

例如:

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

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

【问题讨论】:

    标签: python python-3.x matplotlib histogram seaborn


    【解决方案1】:

    导入和示例数据

    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    tips = sns.load_dataset("tips")
    

    情节

    • 这是使用plt.hist 绘制的,但也适用于pandas.DataFrame.plot 和seaborn.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()
    

    【讨论】:

    • 完美!非常感谢
    猜你喜欢
    • 2012-07-04
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 2019-09-19
    • 1970-01-01
    • 2017-08-03
    相关资源
    最近更新 更多