Pandas DataFrame plot.barh() Method

5/5 - (1 vote)

Preparation

Before any data manipulation can occur, three (3) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The Matplotlib library displays a visual graph of a plotted dataset.
  • The Scipy library allows users to manipulate and visualize the data.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install matplotlib

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install scipy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


Feel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import matplotlib.pyplot as plt
import scipy

DataFrame Horizontal Bar

The pandas.DataFrame.plot.barh() method is a Horizontal Bar representing data with rectangular bars. The lengths (height) of these bars define the values they represent.

The syntax for this method is as follows:

DataFrame.plot.barh(x=None, y=None, **kwargs)
ParameterDescription
xThis parameter determines the coordinates for the x-axis. Default is the index.
yThis parameter determines the coordinates for the y axis. Default is columns.
colorThis parameter can be a string, an array, or a dictionary to signify color(s).
– A single color can be specified by name, RGB or RGBA
– A color sequence specified by name, RGB, or RGBA.
– A dict of the form (col name/color) so each column is colored differently.
**kwargsAdditional keywords are outlined above in the plot() method.

Rivers Clothing would like a Horizontal Bar chart of its sales based on sizes sold over the past six (6) months.

custom_colors = {'Tops': '#8A2BE2', 'Pants': '#6495ED', 'Coats': '#E6E6FA'}

df = pd.DataFrame({'Tops':   [40, 12, 10, 26, 36],
                   'Pants':  [19, 8, 30, 21, 38],
                   'Coats':  [10, 10, 42, 17, 37]}, 
                   index=['XS', 'S', 'M', 'L', 'XL'])
ax = plt.gca()
df.plot.barh(color=custom_colors, ax=ax)
plt.title('Rivers Clothing - Sold')
plt.xlabel('Sizes')
plt.ylabel('Sold')
plt.show()
  • Line  [1] creates a list of color selections for the three (3) bars and saves them to custom_colors.
  • Line [2] Gets the Current Access (gca()) and saves it to ax.
  • Line [3] creates the Horizontal Bar chart using custom_colors and the ax variable created above.
  • Line [4-6] sets the title and labels.
  • Line [7] outputs the Horizontal Bar chart on-screen.

Output

The buttons on the bottom left can be used to further manipulate the chart.

πŸ’‘Β Note: Another way to create this chart is with the plot() method and the kind parameter set to the 'barh' option.

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.