以直方图绘制平均线(matplotlib) [英] Drawing average line in histogram (matplotlib)

查看:130
本文介绍了以直方图绘制平均线(matplotlib)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python中的matplotlib绘制直方图,并想绘制一条表示数据集平均值的线,以虚线覆盖在直方图上(或者也可以使用其他颜色).关于如何绘制直方图上的线的任何想法?

I am drawing a histogram using matplotlib in python, and would like to draw a line representing the average of the dataset, overlaid on the histogram as a dotted line (or maybe some other color would do too). Any ideas on how to draw a line overlaid on the histogram?

我正在使用plot()命令,但不确定如何绘制垂直线(即,我应为y轴赋予什么值?

I am using the plot() command, but not sure how to draw a vertical line (i.e. what value should I give for the y-axis?

谢谢!

推荐答案

您可以使用plotvlines绘制一条垂直线,但是要从y轴的底部到顶部绘制一条垂直线, axvline可能是最简单的函数.这是一个示例:

You can use plot or vlines to draw a vertical line, but to draw a vertical line from the bottom to the top of the y axis, axvline is the probably the simplest function to use. Here's an example:

In [80]: import numpy as np

In [81]: import matplotlib.pyplot as plt

In [82]: np.random.seed(6789)

In [83]: x = np.random.gamma(4, 0.5, 1000)

In [84]: result = plt.hist(x, bins=20, color='c', edgecolor='k', alpha=0.65)

In [85]: plt.axvline(x.mean(), color='k', linestyle='dashed', linewidth=1)
Out[85]: <matplotlib.lines.Line2D at 0x119758828>

结果:

这篇关于以直方图绘制平均线(matplotlib)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆