Bar Plots – AICorr.com



Bar plots

This page covers a matplotlib bar plots tutorial.

A bar plot is a type of graph that represents categorical data with rectangular bars. The lengths or heights of the bars correspond to the values of the categories they represent. Bar plots are common for visualising the distribution, comparison, or frequency of categorical variables.

The x-axis typically represents the categories or groups being compared. The y-axis represents the frequency, count, proportion, or other numerical measure associated with each category. These charts can be horizontal or vertical, depending on how you want to present your data.

Why use bar plots?

With bar plots, you can easily compare the values of different categories by looking at the lengths or heights of the bars. Also, bar plots can show the distribution of categorical data, such as the frequency of different categories within a dataset. Furthermore, they can highlight differences or trends between categories. As such, making it easy to identify which categories have higher or lower values. In addition, bar plots are straightforward and intuitive, which makes them effective for communicating findings to a broad audience. And finally, they provide a concise summary of categorical data. This creates ease of understanding the overall structure of the data.

    Bar plots thrive in various fields, such as statistics, economics, social sciences, and data visualisation. They are used for data analysis, reporting, and communication purposes. They are especially useful when working with categorical or discrete data.

    Implementation

    The implementation of bar plots is easy and straightforward. Matplotlib uses the method bar().

    1. Import Matplotlib library
    2. Add or create data
    3. Plot the bar chart
    4. Add additional changes
    5. Show the chart

    For instance, we create a sample bar plot below.

    import matplotlib.pyplot as plt
    
    # Sample data
    categories = ['A', 'B', 'C', 'D']
    values = [23, 45, 56, 78]
    
    # Bar plot
    plt.bar(categories, values)
    
    # labels and title
    plt.xlabel('Categories')
    plt.ylabel('Values')
    plt.title('Bar Plot Example')
    
    # Show plot
    plt.show()
    matplotlib-bar-plot

    The default method of bar() creates a vertical graph. This means that the bars are placed vertically. To change to horizontal bar plot, we use the barh() function. So, the only change is adding the letter “h” to the original method.

    Let’s look at the coding.

    import matplotlib.pyplot as plt
    
    # Sample data
    categories = ['A', 'B', 'C', 'D']
    values = [23, 45, 56, 78]
    
    # Bar plot
    plt.barh(categories, values)
    
    # labels and title
    plt.xlabel('Categories')
    plt.ylabel('Values')
    plt.title('Bar Plot Example')
    
    # Show plot
    plt.show()
    matplotlib-bar-plot-horizontal

    Now, we can add further changes to the graph. We change the colour and size of the bars. The tools are: color, width (vertical), and height (horizontal). Both width and height methods change the size of the bars. Width applies to vertical bar graphs and height to horizontal graphs.

    Let’s dive into the implementation of both types.

    import matplotlib.pyplot as plt
    
    # Sample data
    categories = ['A', 'B', 'C', 'D']
    values = [23, 45, 56, 78]
    
    # Bar plot
    plt.bar(categories, values, color="#008000", width=0.4)
    
    # labels and title
    plt.xlabel('Categories')
    plt.ylabel('Values')
    plt.title('Bar Plot Example')
    
    # Show plot
    plt.show()
    matplotlib-bar-plot-vertical-color-width
    import matplotlib.pyplot as plt
    
    # Sample data
    categories = ['A', 'B', 'C', 'D']
    values = [23, 45, 56, 78]
    
    # Bar plot
    plt.barh(categories, values, color="purple", height=0.25)
    
    # labels and title
    plt.xlabel('Categories')
    plt.ylabel('Values')
    plt.title('Bar Plot Example')
    
    # Show plot
    plt.show()
    matplotlib-plot-horizontal-color-height

    This is an original matplotlib bar plots tutorial. Created by aicorr.com.

    Next: Histograms

We will be happy to hear your thoughts

Leave a reply

0
Your Cart is empty!

It looks like you haven't added any items to your cart yet.

Browse Products
Powered by Caddy
Shopping cart