Plot line in histogram python

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?

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?

thanks!

asked Apr 23, 2013 at 23:35

user308827user308827

18.8k74 gold badges236 silver badges386 bronze badges

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]: 

Result:

Plot line in histogram python

answered Apr 23, 2013 at 23:56

Warren WeckesserWarren Weckesser

105k19 gold badges176 silver badges197 bronze badges

1

This is old topic and minor addition, but one thing I have often liked is to also plot mean value beside the line:

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(6789)
x = np.random.gamma(4, 0.5, 1000)
result = plt.hist(x, bins=20, color='c', edgecolor='k', alpha=0.65)
plt.axvline(x.mean(), color='k', linestyle='dashed', linewidth=1)

min_ylim, max_ylim = plt.ylim()
plt.text(x.mean()*1.1, max_ylim*0.9, 'Mean: {:.2f}'.format(x.mean()))

Which produces following result:

Plot line in histogram python

Plot line in histogram python

rysqui

2,6492 gold badges18 silver badges25 bronze badges

answered Oct 24, 2018 at 4:41

Plot line in histogram python

I would look at the largest value in your data set (i.e. the histogram bin values) multiply that value by a number greater than 1 (say 1.5) and use that to define the y axis value. This way it will appear above your histogram regardless of the values within the histogram.

answered Apr 23, 2013 at 23:38

smitecsmitec

3,0291 gold badge15 silver badges12 bronze badges

1

Not the answer you're looking for? Browse other questions tagged python matplotlib axis or ask your own question.


To plot a line graph from histogram data in matplotlib, we use numpy histogram method to compute the histogram of a set of data.

Steps

  • Add a subplot to the current figure, nrows=2, ncols=1 and index=1.

  • Use numpy histogram method to get the histogram of a set of data.

  • Plot the histogram using hist() method with edgecolor=black.

  • At index 2, use the computed data (from numpy histogram). To plot them, we can use plot() method.

  • To display the figure, use show() method.

Example

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
plt.subplot(211)
data = np.array(np.random.rand(100))
y, binEdges = np.histogram(data, bins=100)
plt.hist(data, bins=100, edgecolor='black')
plt.subplot(212)
bincenters = 0.5 * (binEdges[1:] + binEdges[:-1])
plt.plot(bincenters, y, '-', c='black')
plt.show()

Output

Plot line in histogram python

Plot line in histogram python

Updated on 11-May-2021 12:19:44

  • Related Questions & Answers
  • How to plot a 2D histogram in Matplotlib?
  • Plot a Line Graph for Pandas Dataframe with Matplotlib?
  • How to plot hexbin histogram in Matplotlib?
  • How to center labels in a Matplotlib histogram plot?
  • How to plot collections.Counter histogram using Matplotlib?
  • How to extract data from a Matplotlib plot?
  • Plot a histogram with colors taken from colormap in Matplotlib
  • How to plot a histogram using Matplotlib in Python with a list of data?
  • How to make a histogram from a list of data in Matplotlib?
  • How to plot a bar graph in Matplotlib from a Pandas series?
  • Plotting a histogram from pre-counted data in Matplotlib
  • How to show a bar and line graph on the same plot in Matplotlib?
  • How to vary the line color with data index for line graph in matplotlib?
  • How to animate a line plot in Matplotlib?
  • How to plot a high resolution graph in Matplotlib?

How do you draw a line on a histogram in Python?

Add a subplot to the current figure, nrows=2, ncols=1 and index=1..
Use numpy histogram method to get the histogram of a set of data..
Plot the histogram using hist() method with edgecolor=black..
At index 2, use the computed data (from numpy histogram). ... .
To display the figure, use show() method..

How do you plot a vertical line on a histogram in Python?

Plot vertical line on histogram matplotlib..
x: specify position on the x-axis to plot the line..
ymin and ymax: specify the starting and ending range of the line..
color: specify the color of the line..
linestyle: specify the style or type of the line..

How do you turn a line graph into a histogram?

Click on the chart plot area of your histogram and select “layout” from the toolbar on top of your screen. On the far right, you will see an option that says “Trendline.” Click on the drop down menu with that option. It will show 4 different styles of trend lines as well as “more trendline options” to choose from.

How do I plot a vertical line in Matplotlib?

Making a single vertical line.
x: Position on X axis to plot the line, It accepts integers..
xmin and xmax: scalar, optional, default: 0/1. It plots the line in the given range..
color: color for the line, It accepts a string. eg 'r' or 'b' ..
linestyle: Specifies the type of line, It accepts a string. eg '-', '–', '-..