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? Show
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
Result: 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:
Which produces following result:
rysqui 2,6492 gold badges18 silver badges25 bronze badges answered Oct 24, 2018 at 4:41
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
Exampleimport 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
Updated on 11-May-2021 12:19:44
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 '-', '–', '-.. |