Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Matplotlib Histograms



Creating histograms with Matplotlib is straightforward and provides a way to visualize the distribution of a dataset. A histogram is a type of bar plot that represents the frequency distribution of a dataset.

Basic Histogram

To create a basic histogram, you can use the hist() function. Here’s an example:



        import matplotlib.pyplot as plt
        import numpy as np
        
        # Creating data
        data = np.random.randn(1000)  # Generate 1000 random numbers from a normal distribution
        
        # Creating a figure and an axes
        fig, ax = plt.subplots()
        
        # Plotting a histogram
        ax.hist(data, bins=30)
        
        # Customizing the plot
        ax.set(title='Histogram',
               xlabel='Value',
               ylabel='Frequency')
        
        # Showing the plot
        plt.show()
        
      

Customizing Histograms

You can customize histograms in various ways, including changing the number of bins, adding colors, and more.

Specifying the Number of Bins

The bins parameter controls the number of bins (bars) in the histogram. You can specify it as an integer or as a sequence defining the bin edges.



        # Specifying the number of bins
        ax.hist(data, bins=50)  # 50 bins
      

Adding Colors

You can change the color of the bars using the color parameter.



        # Adding colors
        ax.hist(data, bins=30, color='skyblue')
      

Adding Edge Colors

The edgecolor parameter adds color to the edges of the bars.



        # Adding edge colors
        ax.hist(data, bins=30, color='skyblue', edgecolor='black')
      

Density Plot

The density parameter normalizes the histogram so that the area under the histogram equals 1.



        # Density plot
        ax.hist(data, bins=30, density=True, color='skyblue', edgecolor='black')
       

Complete Example with Customizations

Here’s a complete example with several customizations:



        import matplotlib.pyplot as plt
        import numpy as np
        
        # Creating data
        data = np.random.randn(1000)  # Generate 1000 random numbers from a normal distribution
        
        # Creating a figure and an axes
        fig, ax = plt.subplots()
        
        # Plotting a histogram with customizations
        ax.hist(data, bins=30, color='skyblue', edgecolor='black', alpha=0.7, density=True)
        
        # Adding a line for the PDF
        mean = np.mean(data)
        std = np.std(data)
        x = np.linspace(min(data), max(data), 100)
        p = 1/(std * np.sqrt(2 * np.pi)) * np.exp(-0.5 * ((x - mean) / std)**2)
        ax.plot(x, p, 'k', linewidth=2)
        
        # Customizing the plot
        ax.set(title='Histogram with Density Plot',
               xlabel='Value',
               ylabel='Density')
        
        # Showing the plot
        plt.show()
        
       

Plotting Multiple Histograms

You can plot multiple histograms on the same axes by calling the hist() function multiple times or by passing multiple datasets to hist().

Multiple Histograms Example



        import matplotlib.pyplot as plt
        import numpy as np
        
        # Creating data
        data1 = np.random.randn(1000)  # Generate 1000 random numbers from a normal distribution
        data2 = np.random.randn(1000) + 2  # Generate 1000 random numbers from a normal distribution, shifted
        
        # Creating a figure and an axes
        fig, ax = plt.subplots()
        
        # Plotting multiple histograms
        ax.hist(data1, bins=30, color='skyblue', edgecolor='black', alpha=0.7, label='Data 1')
        ax.hist(data2, bins=30, color='salmon', edgecolor='black', alpha=0.7, label='Data 2')
        
        # Adding a legend
        ax.legend()
        
        # Customizing the plot
        ax.set(title='Multiple Histograms',
               xlabel='Value',
               ylabel='Frequency')
        
        # Showing the plot
        plt.show()
        
       

Stacked Histograms

For stacked histograms, you can use the stacked parameter.

Stacked Histograms Example


          
        import matplotlib.pyplot as plt
        import numpy as np
        
        # Creating data
        data1 = np.random.randn(1000)
        data2 = np.random.randn(1000) + 2
        
        # Creating a figure and an axes
        fig, ax = plt.subplots()
        
        # Plotting stacked histograms
        ax.hist([data1, data2], bins=30, color=['skyblue', 'salmon'], edgecolor='black', alpha=0.7, stacked=True, label=['Data 1', 'Data 2'])
        
        # Adding a legend
        ax.legend()
        
        # Customizing the plot
        ax.set(title='Stacked Histograms',
               xlabel='Value',
               ylabel='Frequency')
        
        # Showing the plot
        plt.show()
        
       

This should provide you with a good overview of how to create and customize histograms using Matplotlib. Feel free to experiment with different parameters and options to best visualize your data. If you have any specific questions or need further examples, let me know!





Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

Zava

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

Zava