How to get all bars on matplotlib histogram?

It is easy to get all the lines in a line chart by calling the get_lines() function. I can't seem to find an equivalent function for barchart that returns all Rectangle instances in AxesSubplot . Suggestions?

+7
python matplotlib
source share
1 answer

If you want all the bars, just write down the output from the charting method. Its list contains the lines:

 import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt fig, ax = plt.subplots() x = np.arange(5) y = np.random.rand(5) bars = ax.bar(x, y, color='grey') bars[3].set_color('g') 

enter image description here

If you need the entire Rectangle object in the axes, but it can be more than just bars, use:

 bars = [rect for rect in ax.get_children() if isinstance(rect, mpl.patches.Rectangle)] 
+11
source share

All Articles