跟上节课的内容略有重合,除了做试题之前另外做了练习。
在我的感觉看来,一般来说用Python绘图最多的应该是用在较大的数据量以及需要进行处理后来绘图,或者再需要进行自动化绘图的情况。不然,一般情况下还是Excel用着更省力气。
作业试题:
L1 PROBLEM 2
Keyword Assignment Exercise
# 体会下面参数的用法
In [11]: def lotsOfParameters2(a=1,b=2,c=3,d=4,e=5):
...: print (a)
...: print (b)
...: print (c)
...: print (d)
...: print (e)
In [15]: lotsOfParameters2(1, c=2, 3)
File "<ipython-input-15-7d260b8eda02>", line 1
lotsOfParameters2(1, c=2, 3)
^
SyntaxError: positional argument follows keyword argument
In [17]: lotsOfParameters2(1, e=20, b=3, a=10)
Traceback (most recent call last):
File "<ipython-input-17-903cd0ac892b>", line 1, in <module>
lotsOfParameters2(1, e=20, b=3, a=10)
TypeError: lotsOfParameters2() got multiple values for argument 'a'
# 体会下面参数的用法
In [22]: def lotsOfParameters3(a,b,c=3,d=4,e=5):
...: print (a)
...: print (b)
...: print (c)
...: print (d)
...: print (e)
...:
In [23]: lotsOfParameters3()
Traceback (most recent call last):
File "<ipython-input-23-2a493647fa93>", line 1, in <module>
lotsOfParameters3()
TypeError: lotsOfParameters3() missing 2 required positional arguments: 'a' and 'b'
In [25]: lotsOfParameters3(1, c=2)
Traceback (most recent call last):
File "<ipython-input-25-f0523e266aa6>", line 1, in <module>
lotsOfParameters3(1, c=2)
TypeError: lotsOfParameters3() missing 1 required positional argument: 'b'
In [26]: lotsOfParameters3(1, c=2, 3)
File "<ipython-input-26-f5cd3e9a449c>", line 1
lotsOfParameters3(1, c=2, 3)
^
SyntaxError: positional argument follows keyword argument
绘图练习-根据泰坦尼克号绘制性别占比饼图:
In [47]: pwd
Out[47]: 'C:\\Users\\fengyihe\\pandas_exercises-master\\07_Visualization\\Titanic_Desaster'
In [48]: import numpy as np
...: import matplotlib.pyplot as plt
...: from pandas import Series,DataFrame
...:
In [49]: import pandas as pd
In [55]: titanic.head()
Out[55]:
PassengerId Survived Pclass \
0 1 0 3
1 2 1 1
2 3 1 3
3 4 1 1
4 5 0 3
Name Sex Age SibSp \
0 Braund, Mr. Owen Harris male 22.0 1
1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1
2 Heikkinen, Miss. Laina female 26.0 0
3 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1
4 Allen, Mr. William Henry male 35.0 0
Parch Ticket Fare Cabin Embarked
0 0 A/5 21171 7.2500 NaN S
1 0 PC 17599 71.2833 C85 C
2 0 STON/O2. 3101282 7.9250 NaN S
3 0 113803 53.1000 C123 S
4 0 373450 8.0500 NaN S
In [56]: titanic.set_index("PassengerId").head()
Out[56]:
Survived Pclass \
PassengerId
1 0 3
2 1 1
3 1 3
4 1 1
5 0 3
Name Sex Age \
PassengerId
1 Braund, Mr. Owen Harris male 22.0
2 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0
3 Heikkinen, Miss. Laina female 26.0
4 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0
5 Allen, Mr. William Henry male 35.0
SibSp Parch Ticket Fare Cabin Embarked
PassengerId
1 1 0 A/5 21171 7.2500 NaN S
2 1 0 PC 17599 71.2833 C85 C
3 0 0 STON/O2. 3101282 7.9250 NaN S
4 1 0 113803 53.1000 C123 S
5 0 0 373450 8.0500 NaN S
In [57]: # sum the instances of males and females
...: males = (titanic['Sex'] == 'male').sum()
...: females = (titanic['Sex'] == 'female').sum()
...:
In [58]: # put them into a list called proportions
...: proportions = [males, females]
In [61]: # Create a pie chart
...: plt.pie(
...: # using proportions
...: proportions,
...: # with the labels being offices names
...: labels = ['Males','Females'],
...:
...: # with no shadows
...: shadow = False,
...:
...: # with colors
...: colors = ['blue','red'],
...:
...: # with one slide exploded out
...: explode = (0.15, 0),
...:
...: # with the start angle at 90%
...: startangle = 90,
...:
...: # with the percent listed as a fraction
...: autopct = '%1.1f%%'
...: )
...:
...: # View the plot drop above
...: plt.axis('equal')
...:
...: # Set labels
...: plt.title("Sex Proportion")
...:
...: # View the plot
...: plt.tight_layout()
...: plt.show()
...:

In [63]: import seaborn as sns
...: lm = sns.lmplot(x = 'Age', y = 'Fare', data = titanic, hue = 'Sex', fit_reg = False)
...: lm.set(title = 'Fare x Age')
...: axes = lm.axes
...: axes[0,0].set_ylim(-5,)
...: axes[0,0].set_xlim(-5,85)
...:
Out[63]: (-5, 85)

In [64]: titanic.Survived.sum()
Out[64]: 342
In [65]: df = titanic.Fare.sort_values(ascending = False)
...: binsVal = np.arange(0,600,10)
...: binsVal
...:
Out[65]: array([ 0, 10, 20, ..., 570, 580, 590])
In [66]: plt.hist(df, bins = binsVal)
...: plt.xlabel('Fare')
...: plt.ylabel('Frequency')
...: plt.title('Fare Payed Histogram')
...: plt.show()
...:

网友评论