Search code examples
pythonmatplotlibplotlyhistogram

How to access the color attribute of a matplotlib histogram using gcf() or gca()?


I have a single figure, fig, in which I plot a histogram and a line. I can access the line color by using plt.gca().get_lines()[0].set_color("Green"). How do I do the same thing, but for the histogram?


Solution

  • Does this work as you want?

    plt.gca().get_children()[0].set_color("green")
    

    Here is the full code I used and the result screenshot.

    import matplotlib.pyplot as plt
    import numpy as np
    data = np.random.randn(100)
    plt.hist(data)
    plt.gca().get_children()[0].set_color("green")
    plt.show()
    

    enter image description here