How to create a matplotlib histogram with a threshold line?

I would like to know how to create a matplotlib histogram with a threshold line, part of the columns above the threshold line should be red, and parts below the threshold line should be green. Please provide me a simple example, I did not find anything on the Internet.

+7
python matplotlib charts
source share
1 answer

Make it a grouped histogram, as in this example , but divide your data into parts above your threshold and parts below. Example:

import numpy as np import matplotlib.pyplot as plt # some example data threshold = 43.0 values = np.array([30., 87.3, 99.9, 3.33, 50.0]) x = range(len(values)) # split it up above_threshold = np.maximum(values - threshold, 0) below_threshold = np.minimum(values, threshold) # and plot it fig, ax = plt.subplots() ax.bar(x, below_threshold, 0.35, color="g") ax.bar(x, above_threshold, 0.35, color="r", bottom=below_threshold) # horizontal line indicating the threshold ax.plot([0., 4.5], [threshold, threshold], "k--") fig.savefig("look-ma_a-threshold-plot.png") 

Example plot showing the result of the code

+8
source share

All Articles