In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
from matplotlib.gridspec import GridSpec
In [2]:
%matplotlib inline
In [3]:
matplotlib.rcParams.update({'font.size': 14, 'font.family': 'sans', 'text.usetex': True})

E-book usage

In [5]:
# Device Usage
data1 = [30, 28, 7, 5]
x_labels = ["Phone", "Laptop", "Tablet", "E-book\n reader"]
spec1 = GridSpec(1, 7).new_subplotspec((0,0), colspan=4)
spec2 = GridSpec(1, 7).new_subplotspec((0,4), colspan=3)

# Plot the figure.
fig = plt.figure(figsize=(10, 7))
fig.subplots_adjust(wspace=0.02)
ax = fig.add_subplot(spec1)
ax.set_title('What students used\n to read e-books')
ax.set_ylabel('Number of students')
ax.set_xlabel('Devices')
# ax.set_ylim(0, 43)
ax.bar(np.arange(4), data1, color='gray', edgecolor='black', hatch='\\')
ax.set_xticks(np.arange(4))
ax.set_xticklabels(x_labels)

ax2 = fig.add_subplot(spec2, sharey=ax)
plt.setp(ax2.get_yticklabels(), visible=False)
ax2.set_title('Where students got\n their e-books')
ax2.set_xlabel('Sources')
ax2.set_xticks(np.arange(4))
ax2.set_xticklabels(["Online\n stores", "Free sources", "Library"])
data2 = [9, 38, 6]
ax2.bar(np.arange(3), data2, color='w', edgecolor='black', hatch='.')

ax.set_ylim(0,43)

ax_second_y = ax2.twinx()
ax_second_y.set_ylim(0,100)
ax_second_y.set_ylabel('Percentage')

rects = ax.patches
# Make some labels.
labels = ["%d (%.0f\%%)" %(d, d*100/43) for d in data1]

for rect, label in zip(rects, labels):
    height = rect.get_height()
    ax.text(rect.get_x() + rect.get_width() / 2, height + 1, label,
            ha='center', va='bottom')

rects = ax2.patches
labels = ["%d (%.0f\%%)" %(d, d*100/43) for d in data2]
for rect, label in zip(rects, labels):
    height = rect.get_height()
    ax2.text(rect.get_x() + rect.get_width() / 2, height + 1, label,
            ha='center', va='bottom')
In [6]:
# E-book types
data = [23, 5, 13, 9, 19, 6]
x_labels = ["Fiction", "Non-fiction", "Science", "Literature", "Course\n books", "Self-help"]
fig = plt.figure(figsize = (8, 8))
ax = fig.add_subplot(111)
ax.bar(np.arange(6)*2, data, color="gray", hatch="/", edgecolor="black")
ax.set_xticks(np.arange(6)*2)
ax.set_xticklabels(x_labels)
ax.set_ylim(0, 43)
ax.set_title("What types of e-book students read")
ax.set_xlabel("Types")
ax.set_ylabel("Number of students")
rects = ax.patches
labels = ["%d" % d for d in data]
for rect, label in zip(rects, labels):
    height = rect.get_height()
    ax.text(rect.get_x() + rect.get_width() / 2, rect.get_y() + height + 1, label,
            ha='center', va='bottom')
In [8]:
def make_autopct(values):
    def my_autopct(pct):
        total = sum(values)
        val = int(round(pct*total/100.0))
        return '{v:d} ({p:.0f}\%)'.format(p=pct,v=val)
    return my_autopct
In [9]:
data = [13, 16, 14, 1]
data_labels = ["Regularly", "Use as\n required", "Rarely", "Never\n%d (%.0f\\%%)" % (data[3], data[3]/44*100)]
hatches = [" ", "/", ".", "+"]
explode = [0.1, 0, 0, 0]
fig = plt.figure(figsize = (8, 8))
ax = fig.add_subplot(111)
ax.set_title("How frequently students used e-books")
wedges, __, autotexts = ax.pie(data, shadow=True, labels=data_labels, explode=explode, colors=['white' for i in range(4)], autopct=make_autopct(data))
for i in range(len(wedges)):
    w = wedges[i]
    w.set_linewidth(2)
    w.set_edgecolor('black')
    w.set_hatch(hatches[i])
for autotext in autotexts:
    autotext.set_backgroundcolor('white')
autotexts[3].set_position((autotexts[3].get_position()[0]+1, autotexts[3].get_position()[1]))
autotexts[3].set_color('white')
In [10]:
data = [5, 15, 18, 5]
data_labels = ["Excellent", "Good", "It varies", "Poor"]
hatches = [" ", "/", ".", "+"]
explode = [0.1, 0.05, 0, 0]
fig = plt.figure(figsize = (8, 8))
ax = fig.add_subplot(111)
ax.set_title("How satisfied students felt when reading e-books")
wedges, __, autotexts = ax.pie(data, shadow=True, labels=data_labels, explode=explode, colors=['white' for i in range(4)], autopct=make_autopct(data))
for i in range(len(wedges)):
    w = wedges[i]
    w.set_linewidth(2)
    w.set_edgecolor('black')
    w.set_hatch(hatches[i])
for autotext in autotexts:
    autotext.set_backgroundcolor('white')

Paper book usage

In [11]:
data = [22, 17, 5]
data_labels = ["Regularly", "Use as required", "Rarely"]
hatches = [" ", "/", "."]
explode = [0, 0, 0]
fig = plt.figure(figsize = (8, 8))
ax = fig.add_subplot(111)
ax.set_title("How frequently students read paper books")
wedges, __, autotexts = ax.pie(data, shadow=True, labels=data_labels, explode=explode, colors=['white' for i in range(4)], autopct=make_autopct(data))
for i in range(len(wedges)):
    w = wedges[i]
    w.set_linewidth(2)
    w.set_edgecolor('black')
    w.set_hatch(hatches[i])
for autotext in autotexts:
    autotext.set_backgroundcolor('white')
In [12]:
data = [15, 21, 7, 1]
data_labels = ["Excellent", "Good", "It varies", "Poor\n%d (%.0f\\%%)" % (data[3], data[3]/44*100)]
hatches = [" ", "/", ".", "+"]
explode = [0.1, 0.05, 0, 0]
fig = plt.figure(figsize = (8, 8))
ax = fig.add_subplot(111)
ax.set_title("How satisfied students felt when reading paper books")
wedges, __, autotexts = ax.pie(data, shadow=True, labels=data_labels, explode=explode, colors=['white' for i in range(4)], autopct=make_autopct(data))
for i in range(len(wedges)):
    w = wedges[i]
    w.set_linewidth(2)
    w.set_edgecolor('black')
    w.set_hatch(hatches[i])
for autotext in autotexts:
    autotext.set_backgroundcolor('white')
autotexts[3].set_position((autotexts[3].get_position()[0]+1, autotexts[3].get_position()[1]))
autotexts[3].set_color('white')

Comparison between the two book type

In [13]:
from operator import add
labels = ["Ease of\n reading", "Ease of\n annotation", "Ease of\n bookmarking", "Ease of\n navigation", "Comprehension\n Concentration", "Property\n possession", "Availabilty\n Portability", "Cost", "Space\n saving", "Environmentally\n friendly"]
data = [[3, 10, 11, 7, 13], [1, 7, 17, 6, 13], [7, 10, 10, 8, 9], [9, 8, 11, 9, 7], [2, 3, 14, 12, 13], [2, 6, 16, 6, 14], [14, 10, 10, 5, 5], [14, 9, 8, 8, 5], [28, 7, 5, 2, 2], [21, 9, 9, 3, 2]]
data_labels = []
# for row in data:
#     data_labels.append([])
#     for d in data:
        
hatches = ['/', ' ', 'x', ' ', '\\']
colors = ['white', 'white', 'white', 'gray', 'white']
fig = plt.figure(figsize = (20, 10))
ax = fig.add_subplot(111)
s = [0 for i in range(10)]
bars = []
for i in range(5):
    tmp = [data[j][i] for j in range(10)]
    bars.append(ax.bar(np.arange(10), tmp, width=.25, bottom=s, hatch=hatches[i], edgecolor='black', color=colors[i]))
    s = list(map(add, s, tmp))
ax.set_xticks(np.arange(10))
ax.set_xticklabels(labels)
# ax.set_ylim(0, 43)
ax.set_title("Students' evaluation of book qualities")
ax.set_xlabel("Features")
ax.set_ylabel("Number of students")
rects = ax.patches
flatten_data = [data[j][i] for i in range(5) for j in range(10)]
# labels = ["%d" % d for d in data]
for rect, label in zip(rects, flatten_data):
    height = rect.get_height()
    ax.text(rect.get_x() + rect.get_width() + 0.1, rect.get_y() + height / 2, label,
            ha='center', va='bottom')
fig.legend((bars[0], bars[1], bars[2], bars[3], bars[4]), ("Definitely e-books", "Probably e-books", "Can't decide", "Probably paper books", "Definitely paper books"), 'upper center')
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width, box.height*0.9])
ax.xaxis.set_label_coords(0.5, -0.1)
In [14]:
data = [11, 33]
data_labels = ["E-books", "Paper books"]
hatches = [" ", "/"]
explode = [0, 0]
fig = plt.figure(figsize = (8, 8))
ax = fig.add_subplot(111)
ax.set_title("Which type of book is better in general?")
wedges, __, autotexts = ax.pie(data, shadow=True, labels=data_labels, explode=explode, colors=['white' for i in range(2)], autopct=make_autopct(data))
for i in range(len(wedges)):
    w = wedges[i]
    w.set_linewidth(2)
    w.set_edgecolor('black')
    w.set_hatch(hatches[i])
for autotext in autotexts:
    autotext.set_backgroundcolor('white')