UP | HOME

Exercise 20 solutions

Table of Contents


Python

e20.py

# ipython --pylab

# part 1
y = [10,8,6,4,6,8,6,2]
bar(range(8), height=y)
xlabel(()) # empty to get rid of x labels

# part 2
clf()
bar(range(8), height=y)
xticks(arange(8)+0.5, range(8)) # location, label

MATLAB / Octave

e20.m

% part 1
y = [10,8,6,4,6,8,6,2];
bar(y)
box off
set(gca,'xtick',[]) % to get rid of x labels

% part 2
bar(y)
box off

R

e20.R

# part 1
y <- c(10,8,6,4,6,8,6,2)
barplot(y)

# part 2
y <- c(10,8,6,4,6,8,6,2)
mp <- barplot(y) # mp is the midpoints of each bar in x
axis(1, at=mp, labels=1:8) # adds the x axis plus labels

Paul Gribble | fall 2014
This work is licensed under a Creative Commons Attribution 4.0 International License
Creative Commons License