Added Matplotlib and Comprehensions files

This commit is contained in:
Joe James
2018-06-24 19:50:15 -07:00
committed by GitHub
parent 52ac50282f
commit 031df3f3bd
3 changed files with 568 additions and 0 deletions

File diff suppressed because one or more lines are too long

90
Matplotlib/pyplot.py Normal file
View File

@@ -0,0 +1,90 @@
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# 1. simple plot with 4 numbers
plt.plot([1, 3, 2, 4])
plt.show()
# 2. points have x and y values; add title and axis labels
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.title('Test Plot', fontsize=8, color='g')
plt.xlabel('number n')
plt.ylabel('n^2')
plt.show()
# 3. change figure size. plot red dots; set axis scales x: 0-6 and y: 0-20
plt.figure(figsize=(1,5)) # 1 inch wide x 5 inches tall
plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro') # red-o
plt.axis([0, 6, 0, 20]) # [xmin, xmax, ymin, ymax]
plt.annotate('square it', (3,6))
plt.show()
# 4. bar chart with four bars
plt.clf() # clear figure
x = np.arange(4)
y = [8.8, 5.2, 3.6, 5.9]
plt.xticks(x, ('Ankit', 'Hans', 'Joe', 'Flaco'))
plt.bar(x, y)
# plt.bar(x, y, color='y')
# plt.bar(x, y, color=['lime', 'r', 'k', 'tan'])
plt.show()
# 5. two sets of 10 random dots plotted
d = {'Red O' : np.random.rand(10),
'Grn X' : np.random.rand(10)}
df = pd.DataFrame(d)
df.plot(style=['ro','gx'])
plt.show()
# 6. time series - six months of random floats
ts = pd.Series(np.random.randn(180), index=pd.date_range('1/1/2018', periods=180))
df = pd.DataFrame(np.random.randn(180, 3), index=ts.index, columns=list('ABC'))
df.cumsum().plot()
plt.show()
# 7. random dots in a scatter
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
sizes = (30 * np.random.rand(N))**2 # 0 to 15 point radii
plt.scatter(x, y, s=sizes, c=colors, alpha=0.5)
plt.show()
# 8. load csv file and show multiple chart types
df = pd.read_csv('Fremont_weather.txt')
print(df)
plt.bar(df['month'], df['record_high'], color='r')
plt.bar(df['month'], df['record_low'], color='c')
plt.plot(df['month'], df['avg_high'], color='k')
plt.plot(df['month'], df['avg_low'], color='b')
plt.legend() # or plt.figlegend for legend outside the plot area
plt.show()
# 9. subplots
fig = plt.figure()
fig.suptitle('My SubPlots')
fig.add_subplot(221) #top left
plt.plot([np.log(n) for n in range(1,10)])
fig.add_subplot(222, facecolor='y') #top right
fig.add_subplot(223) #bottom left
fig.add_subplot(224) #bottom right
plt.show()
fig, plots = plt.subplots(2, sharex=True)
fig.suptitle('Sharing X axis')
x = range(0,200,5)
y = [n**0.8 for n in x]
plots[0].plot(x, y, color='r')
plots[1].scatter(x, y)
# 10. save figure to image file
plt.figure(figsize=(4,3), dpi=100)
plt.plot([245, 170, 148, 239, 161, 196, 112, 258])
plt.axis([0, 7, 0, 300])
plt.title('Flight Data')
plt.xlabel('Speed')
# plt.savefig('Flights.png')
plt.show()

42
list_comprehensions.py Normal file
View File

@@ -0,0 +1,42 @@
# list comprehensions
# basic format: new_list = [transform sequence [filter] ]
import random
under_10 = [x for x in range(10)]
print('under_10: ' + str(under_10))
squares = [x**2 for x in under_10]
print('squares: ' + str(squares))
odds = [x for x in range(10) if x%2 == 1]
print('odds: ' + str(odds))
ten_x = [x * 10 for x in range(10)]
print('ten_x: ' + str(ten_x))
# get all numbers from a string
s = 'I love 2 go t0 the store 7 times a w3ek.'
nums = [x for x in s if x.isnumeric()]
print('nums: ' + ''.join(nums))
# get index of a list item
names = ['Cosmo', 'Pedro', 'Anu', 'Ray']
idx = [k for k, v in enumerate(names) if v == 'Anu']
print('index = ' + str(idx[0]))
# delete an item from a list
letters = [x for x in 'ABCDEF']
random.shuffle(letters)
letrs = [a for a in letters if a != 'C']
print(letters, letrs)
# if-else condition in a comprehension (must come before iteration)
nums = [5, 3, 10, 18, 6, 7]
new_list = [x if x%2 == 0 else 10*x for x in nums]
print('new list: ' + str(new_list))
# nested loop iteration for 2D list
a = [[1,2],[3,4]]
new_list = [x for b in a for x in b]
print(new_list)