Bar plot

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:

  1. Create a Pandas DataFrame df
  2. Plot the bar chart with df.plot(kind='bar')
  3. Set the labels of xticks
  4. Adjust the other plot properties
  5. 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()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.124.135