python - 在直方图顶部绘制数据点
问题描述
我的直方图如下:
我还有一些数据点,我想在直方图顶部绘制一些值。
例如:
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()
如何将存储在某个变量中的新数据点添加到其中。
解决方案
- 在兴趣点添加垂直线作为标记。
- 另请参阅使用 matplotlib 绘制水平线和如何在 matplotlib 中的给定绘图上绘制垂直线
- 在
python 3.8.11
,pandas 1.3.2
,matplotlib 3.4.3
,中测试seaborn 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.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()
推荐阅读
- mysql - MySQL Server 8.0 - 没有 bin 文件
- reactjs - React useEffect 不重新渲染功能组件
- plotly - JavaScript 配置,在“悬停时显示最接近的数据”和“悬停时比较数据”之间进行更改
- c# - 函数不计算某些数字的二进制间隙
- java - 通过高级客户端写入弹性搜索 - 在文档上设置时间戳
- regex - Oracle通过正则表达式选择所有匹配的事件到一行中的一列,并带有分隔符
- c++ - TensorFlow C 中的自定义操作
- python - Boto3 - 仅在自指定时间戳后修改时才下载文件
- python - 按列从另一个 DataFrame 创建一个 DataFrame
- unit-testing - 如何使用 Figwheel 运行 Clojurescript 单元测试?