Bar plots are useful for comparing absolute levels of discrete data series. They are created by the function plt.bar(labels,heights) in Matplotlib.
Let's look at the example of the market capitalization of today's much hyped cryptocurrencies. The five top cryptocurrencies in terms of market capitalization are shown here:
import matplotlib.pyplot as plt
# Data retrieved from https://coinmarketcap.com on Jan 8, 2018
# Prepare the data series
cc = ['BTC','XRP','ETH','BCH','ADA']
cap = [282034,131378,107393,49999,26137]
# Plot the bar chart
plt.bar(cc,cap)
plt.title('Market capitalization of five top cryptocurrencies in Jan 2018')
plt.xlabel('Crytocurrency')
plt.ylabel('Market capitalization (million USD)')
plt.show()
We can see from the following figure that instead of following the input order, Matplotlib outputs a figure of bars with labels sorted alphabetically:
To create bar plots with bars in the designated order, we can make use of Pandas and its Matplotlib integration. The procedure is as follows:
- Create a Pandas DataFrame df
- Plot the bar chart with df.plot(kind='bar')
- Set the labels of xticks
- Adjust the other plot properties
- Show the plot with plt.show()
Please note that, by default, df.plot() includes a legend. We need to specify legend=False to turn it off.
Here is an example to reorder the bar plot in the previous output figure:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'cc':cc,'cap':cap}, legend=False)
ax = df.plot(kind='bar')
ax.set_xticklabels(df['cc'])
plt.title('Market capitalization of five top cryptocurrencies in Jan 2018')
plt.xlabel('Crytocurrency')
plt.ylabel('Market capitalization (million USD)')
plt.show()