Title: Histograms In MatPlotLib
Slug: matplotlib_histogram
Summary: Histograms In MatPlotLib
Date: 2016-05-01 12:00
Category: Python
Tags: Data Visualization
Authors: Chris Albon
Based on: Sebastian Raschka.
In [89]:
%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import math
# Set ipython's max row display
pd.set_option('display.max_row', 1000)
# Set iPython's max column width to 50
pd.set_option('display.max_columns', 50)
In [3]:
df = pd.read_csv('https://www.dropbox.com/s/52cb7kcflr8qm2u/5kings_battles_v1.csv?dl=1')
df.head()
Out[3]:
In [87]:
# Make two variables of the attacker and defender size, but leaving out
# cases when there are over 10000 attackers
data1 = df['attacker_size'][df['attacker_size'] < 90000]
data2 = df['defender_size'][df['attacker_size'] < 90000]
# Create bins of 2000 each
bins = np.arange(data1.min(), data2.max(), 2000) # fixed bin size
# Plot a histogram of attacker size
plt.hist(data1,
bins=bins,
alpha=0.5,
color='#EDD834',
label='Attacker')
# Plot a histogram of defender size
plt.hist(data2,
bins=bins,
alpha=0.5,
color='#887E43',
label='Defender')
# Set the x and y boundaries of the figure
plt.ylim([0, 10])
# Set the title and labels
plt.title('Histogram of Attacker and Defender Size')
plt.xlabel('Number of troops')
plt.ylabel('Number of battles')
plt.legend(loc='upper right')
plt.show()
In [108]:
# Make two variables of the attacker and defender size, but leaving out
# cases when there are over 10000 attackers
data1 = df['attacker_size'][df['attacker_size'] < 90000]
data2 = df['defender_size'][df['attacker_size'] < 90000]
# Create 10 bins with the minimum
# being the smallest value of data1 and data2
bins = np.linspace(min(data1 + data2),
# the max being the highest value
max(data1 + data2),
# and divided into 10 bins
10)
# Plot a histogram of attacker size
plt.hist(data1,
# with bins defined as
bins=bins,
# with alpha
alpha=0.5,
# with color
color='#EDD834',
# labelled attacker
label='Attacker')
# Plot a histogram of defender size
plt.hist(data2,
# with bins defined as
bins=bins,
# with alpha
alpha=0.5,
# with color
color='#887E43',
# labeled defender
label='Defender')
# Set the x and y boundaries of the figure
plt.ylim([0, 10])
# Set the title and labels
plt.title('Histogram of Attacker and Defender Size')
plt.xlabel('Number of troops')
plt.ylabel('Number of battles')
plt.legend(loc='upper right')
plt.show()