Unit-2 Lab
Unit-2 Lab
df = generateData(50)
df.head(10)
df.to_csv(r'stock.csv')
1. Load the dataset and prepare the dataset. We will learn more how we can prepare the data in the chapter 4, data
transformation. For this exercise, all the data are pre-processed.
2. Import the matplotlib library. It can be done simply by:
```
import matplotlib.pyplot as plt
```
3. Plot the graph.
```
plt.plot(df)
```
4. Display on the screen.
```
plt.show()
```
"""
# Step 1: Set up the data. Remember range stoping parameter is exclusive. Meaning if you generate range from (1,
13), the last item 13 is not included.
months = list(range(1, 13))
sold_quantity = [round(random.uniform(100, 200)) for x in range(1, 13)]
# Step 5: This step can be optinal depending upon if you are interested in displaying the data vaue on the head of the
bar.
# It visually gives more meaning to show actual number of sold iteams on the bar itself.
for rectangle in plot:
height = rectangle.get_height()
axis.text(rectangle.get_x() + rectangle.get_width() /2., 1.002 * height, '%d' % int(height), ha='center', va = 'bottom')
# Step 1: Set up the data. Remember range stoping parameter is exclusive. Meaning if you generate range from (1,
13), the last item 13 is not included.
months = list(range(1, 13))
sold_quantity = [round(random.uniform(100, 200)) for x in range(1, 13)]
# Step 3: In the X-axis, we would like to display the name of the months.
plt.yticks(months, calendar.month_name[1:13], rotation=20)
# Step 5: This step can be optinal depending upon if you are interested in displaying the data vaue on the head of the
bar.
# It visually gives more meaning to show actual number of sold iteams on the bar itself.
for rectangle in plot:
width = rectangle.get_width()
axis.text(width + 2.5, rectangle.get_y() + 0.38, '%d' % int(width), ha='center', va = 'bottom')
# infants(4-11)
for i in range(4, 12):
min_recommended = 12
max_recommended = 15
may_be_appropriate_min = 10
may_be_appropriate_max = 11
min_not_recommended = 10
max_not_recommended = 18
sleep.append([i, min_recommended, max_recommended, may_be_appropriate_min, may_be_appropriate_max,
min_not_recommended, max_not_recommended])
# toddlers(12-24)
for i in range(12, 25):
min_recommended = 11
max_recommended = 14
may_be_appropriate_min = 9
may_be_appropriate_max = 10
min_not_recommended = 9
max_not_recommended = 16
sleep.append([i, min_recommended, max_recommended, may_be_appropriate_min, may_be_appropriate_max,
min_not_recommended, max_not_recommended])
# preschoolers(36-60)
for i in range(36, 61):
min_recommended = 10
max_recommended = 13
may_be_appropriate_min = 8
may_be_appropriate_max = 9
min_not_recommended = 8
max_not_recommended = 14
sleep.append([i, min_recommended, max_recommended, may_be_appropriate_min, may_be_appropriate_max,
min_not_recommended, max_not_recommended])
# school-aged-children(72-156)
for i in range(72, 157):
min_recommended = 9
max_recommended = 11
may_be_appropriate_min = 7
may_be_appropriate_max = 8
min_not_recommended = 7
max_not_recommended = 12
sleep.append([i, min_recommended, max_recommended, may_be_appropriate_min, may_be_appropriate_max,
min_not_recommended, max_not_recommended])
# teenagers(168-204)
for i in range(168, 204):
min_recommended = 8
max_recommended = 10
may_be_appropriate_min = 7
may_be_appropriate_max = 11
min_not_recommended = 7
max_not_recommended = 11
sleep.append([i, min_recommended, max_recommended, may_be_appropriate_min, may_be_appropriate_max,
min_not_recommended, max_not_recommended])
# young-adults(216-300)
for i in range(216, 301):
min_recommended = 7
max_recommended = 9
may_be_appropriate_min = 6
may_be_appropriate_max = 11
min_not_recommended = 6
max_not_recommended = 11
sleep.append([i, min_recommended, max_recommended, may_be_appropriate_min, may_be_appropriate_max,
min_not_recommended, max_not_recommended])
# adults(312-768)
for i in range(312, 769):
min_recommended = 7
max_recommended = 9
may_be_appropriate_min = 6
may_be_appropriate_max = 10
min_not_recommended = 6
max_not_recommended = 10
sleep.append([i, min_recommended, max_recommended, may_be_appropriate_min, may_be_appropriate_max,
min_not_recommended, max_not_recommended])
# older-adults(>=780)
for i in range(769, 780):
min_recommended = 7
max_recommended = 8
may_be_appropriate_min = 5
may_be_appropriate_max = 6
min_not_recommended = 5
max_not_recommended = 9
sleep.append([i, min_recommended, max_recommended, may_be_appropriate_min, may_be_appropriate_max,
min_not_recommended, max_not_recommended])
# Line plot
plt.plot(sleepDf['age']/12., sleepDf['min_recommended'], 'g--')
plt.plot(sleepDf['age']/12., sleepDf['max_recommended'], 'r--')
plt.xlabel('Age of person in Years')
plt.ylabel('Total hours of sleep required')
plt.show()
# Use style froms seaborn. Try to comment the next line and see the difference in graph
sns.set()
df = sns.load_dataset('iris')
sns.set()
plt.plot([],[], color='sandybrown', label='houseLoanMortage')
plt.plot([],[], color='tan', label='utilitiesBills')
plt.plot([],[], color='bisque', label='transportation')
plt.plot([],[], color='darkcyan', label='carMortage')
plt.stackplot(months, houseLoanMortage, utilitiesBills, transportation, carMortage, colors=['sandybrown', 'tan',
'bisque', 'darkcyan'])
plt.legend()
plt.title('Household Expenses')
plt.xlabel('Months of the year')
plt.ylabel('Cost')
plt.show()
# Load the first sheet of the JSON file into a data frame
pokemon = pd.read_csv(url, index_col='type')
pokemon
"""Do you know you can directly use Pandas library to create pie chart? Check the one liner below."""
"""This is why Python is said to be commedian. Do you know why? Because, it has a lot of one liners. Pretty true right?
# Table Chart
"""
# Available watt
columns = ['4.5W', '6.0W', '7.0W','8.5W','9.5W','13.5W','15W']
unitsSold = [
[65, 141, 88, 111, 104, 71, 99],
[85, 142, 89, 112, 103, 73, 98],
[75, 143, 90, 113, 89, 75, 93],
[65, 144, 91, 114, 90, 77, 92],
[55, 145, 92, 115, 88, 79, 93],
]
y_offset = np.zeros(len(columns))
fig, ax = plt.subplots()
cell_text = []
n_rows = len(unitsSold)
for row in range(n_rows):
plot = plt.bar(index, unitsSold[row], bar_width, bottom=y_offset,
color=colors[row])
y_offset = y_offset + unitsSold[row]
cell_text.append(['%1.1f' % (x) for x in y_offset])
i=0
# Each iteration of this for loop, labels each bar with corresponding value for the given year
for rect in plot:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2, y_offset[i],'%d'
# % int(y_offset[i]),
ha='center', va='bottom')
i = i+1
# However, after your final examination, this is the grade you got
actualGrade = [75, 89, 89, 80, 80, 75]
# 5. Get the grid lines to align with each of the subject names.
(lines,labels) = plt.thetagrids(range(0,360, int(360/len(subjects))),
(subjects))
# 6. We use plot method to plot the graph. And fill the area under it.
plt.plot(theta, plannedGrade)
plt.fill(theta, plannedGrade, 'b', alpha=0.2)
"""# Histogram"""
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize = (10,6))
plt.figure(figsize = (10,6))
carDF = pd.read_csv('https://raw.githubusercontent.com/PacktPublishing/hands-on-exploratory-data-analysis-with-
python/master/Chapter%202/cardata.csv')
# 5. Annotate Title
ax.set_title('Lollipop Chart for Highway Mileage using car dataset', fontdict={'size':22})