How do you add a border to a histogram in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Prerequisites: Matplotlib

    In this article, we will see how can we can add a border around histogram bars in our graph using matplotlib, Here we will take two different examples to showcase our graph.

    Approach:

    • Import required module.
    • Create data.
    • Add border around histogram bars.
    • Normally plot the data.
    • Display plot.

    Below is the Implementation:

    Example 1:

    In this example, we will Pass an edgecolor = ‘Black’ value as the edge color parameter to plt.hist() to change the bar border color.

    Python3

    from matplotlib import pyplot as plt

    import numpy as np

    fig,ax = plt.subplots(1,1)

    a = np.array([22, 87, 5, 43, 56, 73, 55,

                  54, 11, 20, 51, 5, 79, 31, 27])

    b = (0, 25, 50, 75, 100)

    ax.hist(a, b, edgecolor = "black")

    ax.set_title("histogram - Geekforgeeks")

    ax.set_xlabel('Litracy Level(%)')

    ax.set_ylabel('Population(million)')

    plt.show()

    Output:

    How do you add a border to a histogram in python?

    Example 2:

    In this example, we will Pass a two edge color, edgecolor = ‘Black’ and ‘red’ value as the edge color parameter to plt.hist() to change the bar border color.

    Python3

    from matplotlib import pyplot as plt

    import numpy as np

    fig,ax = plt.subplots(1, 1)

    a = np.array([20, 73, 55, 31, 51, 5, 79, 5,

                  43, 22, 87, 54, 11, 56, 27])

    b = np.array([0, 25, 50, 75, 100])

    ax.hist(a, edgecolor = "black", color = 'pink')

    ax.hist(b, edgecolor = "red", color = 'gray')

    ax.set_title("Histogram - Geekforgeeks")

    plt.show()

    Output:

    How do you add a border to a histogram in python?


    I have plotted a histogram in Jupyter (Python 2) and was expecting to see the outlines of my bars but this is not the case.

    How do you add a border to a histogram in python?

    I'm using the following code:

    import matplotlib.pyplot as plt
    from numpy.random import normal
    gaussian_numbers = normal(size=1000)
    plt.hist(gaussian_numbers)
    plt.title("Gaussian Histogram")
    plt.xlabel("Value")
    plt.ylabel("Frequency")
    plt.show()
    

    DavidG

    22.7k14 gold badges84 silver badges79 bronze badges

    asked Mar 11, 2017 at 22:58

    2

    It looks like either your linewidth was set to zero or your edgecolor was set to 'none'. Matplotlib changed the defaults for these in 2.0. Try using:

    plt.hist(gaussian_numbers, edgecolor='black', linewidth=1.2)
    

    How do you add a border to a histogram in python?

    answered Mar 11, 2017 at 23:09

    How do you add a border to a histogram in python?

    JamesJames

    30.5k4 gold badges45 silver badges66 bronze badges

    4

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

    How do you add a border to a histogram?

    Re: Histogram Bar Outline If so, two-finger click on a bar and select "Format data series". Then press on the "bucket" and "Border" and select the format you want.

    How do I add a space to a histogram in Python?

    The space between bars can be added by using rwidth parameter inside the “plt. hist()” function. This value specifies the width of the bar with respect to its default width and the value of rwidth cannot be greater than 1.

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

    Plot vertical line segment matplotlib. 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..

    Which argument is used to change the color of the border of bars in histogram?

    The edgecolor argument allows you to color the borders of barplots.